-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathglobal-styles.ts
91 lines (83 loc) · 2.52 KB
/
global-styles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import type { BuildOptions } from 'esbuild';
import assert from 'node:assert';
import { NormalizedBrowserOptions } from '../../builders/browser-esbuild/options';
import { LoadResultCache } from './load-result-cache';
import { createStylesheetBundleOptions } from './stylesheets/bundle-options';
import { createVirtualModulePlugin } from './virtual-module-plugin';
export function createGlobalStylesBundleOptions(
options: NormalizedBrowserOptions,
target: string[],
browsers: string[],
initial: boolean,
cache?: LoadResultCache,
): BuildOptions | undefined {
const {
workspaceRoot,
optimizationOptions,
sourcemapOptions,
outputNames,
globalStyles,
preserveSymlinks,
externalDependencies,
stylePreprocessorOptions,
tailwindConfiguration,
} = options;
const namespace = 'angular:styles/global';
const entryPoints: Record<string, string> = {};
let found = false;
for (const style of globalStyles) {
if (style.initial === initial) {
found = true;
entryPoints[style.name] = `${namespace};${style.name}`;
}
}
// Skip if there are no entry points for the style loading type
if (found === false) {
return;
}
const buildOptions = createStylesheetBundleOptions(
{
workspaceRoot,
optimization: !!optimizationOptions.styles.minify,
sourcemap: !!sourcemapOptions.styles,
preserveSymlinks,
target,
externalDependencies,
outputNames: initial
? outputNames
: {
...outputNames,
bundles: '[name]',
},
includePaths: stylePreprocessorOptions?.includePaths,
browsers,
tailwindConfiguration,
},
cache,
);
buildOptions.legalComments = options.extractLicenses ? 'none' : 'eof';
buildOptions.entryPoints = entryPoints;
buildOptions.plugins.unshift(
createVirtualModulePlugin({
namespace,
transformPath: (path) => path.split(';', 2)[1],
loadContent: (args) => {
const files = globalStyles.find(({ name }) => name === args.path)?.files;
assert(files, `global style name should always be found [${args.path}]`);
return {
contents: files.map((file) => `@import '${file.replace(/\\/g, '/')}';`).join('\n'),
loader: 'css',
resolveDir: workspaceRoot,
};
},
}),
);
return buildOptions;
}