forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup-bundling.ts
197 lines (183 loc) · 6.15 KB
/
setup-bundling.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @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.dev/license
*/
import { AngularCompilation } from '../../tools/angular/compilation';
import { ComponentStylesheetBundler } from '../../tools/esbuild/angular/component-stylesheets';
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
import {
createBrowserCodeBundleOptions,
createBrowserPolyfillBundleOptions,
createServerMainCodeBundleOptions,
createServerPolyfillBundleOptions,
createSsrEntryCodeBundleOptions,
} from '../../tools/esbuild/application-code-bundle';
import { BundlerContext } from '../../tools/esbuild/bundler-context';
import { createGlobalScriptsBundleOptions } from '../../tools/esbuild/global-scripts';
import { createGlobalStylesBundleOptions } from '../../tools/esbuild/global-styles';
import { getSupportedNodeTargets } from '../../tools/esbuild/utils';
import { NormalizedApplicationBuildOptions } from './options';
/**
* Generates one or more BundlerContext instances based on the builder provided
* configuration.
* @param options The normalized application builder options to use.
* @param browsers An string array of browserslist browsers to support.
* @param codeBundleCache An instance of the TypeScript source file cache.
* @returns An array of BundlerContext objects.
*/
export function setupBundlerContexts(
options: NormalizedApplicationBuildOptions,
target: string[],
codeBundleCache: SourceFileCache,
stylesheetBundler: ComponentStylesheetBundler,
angularCompilation: AngularCompilation,
templateUpdates: Map<string, string> | undefined,
): {
typescriptContexts: BundlerContext[];
otherContexts: BundlerContext[];
} {
const {
outputMode,
serverEntryPoint,
appShellOptions,
prerenderOptions,
ssrOptions,
workspaceRoot,
watch = false,
} = options;
const typescriptContexts = [];
const otherContexts = [];
// Browser application code
typescriptContexts.push(
new BundlerContext(
workspaceRoot,
watch,
createBrowserCodeBundleOptions(
options,
target,
codeBundleCache,
stylesheetBundler,
angularCompilation,
templateUpdates,
),
),
);
// Browser polyfills code
const browserPolyfillBundleOptions = createBrowserPolyfillBundleOptions(
options,
target,
codeBundleCache,
stylesheetBundler,
);
if (browserPolyfillBundleOptions) {
const browserPolyfillContext = new BundlerContext(
workspaceRoot,
watch,
browserPolyfillBundleOptions,
);
if (typeof browserPolyfillBundleOptions === 'function') {
otherContexts.push(browserPolyfillContext);
} else {
typescriptContexts.push(browserPolyfillContext);
}
}
// Global Stylesheets
if (options.globalStyles.length > 0) {
for (const initial of [true, false]) {
const bundleOptions = createGlobalStylesBundleOptions(options, target, initial);
if (bundleOptions) {
otherContexts.push(new BundlerContext(workspaceRoot, watch, bundleOptions, () => initial));
}
}
}
// Global Scripts
if (options.globalScripts.length > 0) {
for (const initial of [true, false]) {
const bundleOptions = createGlobalScriptsBundleOptions(options, target, initial);
if (bundleOptions) {
otherContexts.push(new BundlerContext(workspaceRoot, watch, bundleOptions, () => initial));
}
}
}
// Skip server build when none of the features are enabled.
if (serverEntryPoint && (outputMode || prerenderOptions || appShellOptions || ssrOptions)) {
const nodeTargets = [...target, ...getSupportedNodeTargets()];
typescriptContexts.push(
new BundlerContext(
workspaceRoot,
watch,
createServerMainCodeBundleOptions(options, nodeTargets, codeBundleCache, stylesheetBundler),
),
);
if (outputMode && ssrOptions?.entry) {
// New behavior introduced: 'server.ts' is now bundled separately from 'main.server.ts'.
typescriptContexts.push(
new BundlerContext(
workspaceRoot,
watch,
createSsrEntryCodeBundleOptions(options, nodeTargets, codeBundleCache, stylesheetBundler),
),
);
}
// Server polyfills code
const serverPolyfillBundleOptions = createServerPolyfillBundleOptions(
options,
nodeTargets,
codeBundleCache.loadResultCache,
);
if (serverPolyfillBundleOptions) {
otherContexts.push(new BundlerContext(workspaceRoot, watch, serverPolyfillBundleOptions));
}
}
return { typescriptContexts, otherContexts };
}
export function createComponentStyleBundler(
options: NormalizedApplicationBuildOptions,
target: string[],
): ComponentStylesheetBundler {
const {
workspaceRoot,
optimizationOptions,
sourcemapOptions,
outputNames,
externalDependencies,
preserveSymlinks,
stylePreprocessorOptions,
inlineStyleLanguage,
cacheOptions,
tailwindConfiguration,
postcssConfiguration,
publicPath,
} = options;
const incremental = !!options.watch;
return new ComponentStylesheetBundler(
{
workspaceRoot,
inlineFonts: !!optimizationOptions.fonts.inline,
optimization: !!optimizationOptions.styles.minify,
sourcemap:
// Hidden component stylesheet sourcemaps are inaccessible which is effectively
// the same as being disabled. Disabling has the advantage of avoiding the overhead
// of sourcemap processing.
sourcemapOptions.styles && !sourcemapOptions.hidden ? 'linked' : false,
sourcesContent: sourcemapOptions.sourcesContent,
outputNames,
includePaths: stylePreprocessorOptions?.includePaths,
// string[] | undefined' is not assignable to type '(Version | DeprecationOrId)[] | undefined'.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sass: stylePreprocessorOptions?.sass as any,
externalDependencies,
target,
preserveSymlinks,
tailwindConfiguration,
postcssConfiguration,
cacheOptions,
publicPath,
},
inlineStyleLanguage,
incremental,
);
}