Skip to content

Commit 7934bec

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): generate unique webpack runtimes
Currently, using 2 Angular applications from the same workspace on the same page causes a conflict because both of the webpack runtime chunks naming are the same. With this change we configure the runtime chunk name to be inferred from the project name. This also results in reducing unnecessary file reads which Webpack needs to do to infer the name from the workspace package.json. For more information about this option see: https://webpack.js.org/configuration/output/#outputuniquename Closes #21957 (cherry picked from commit 1dac761)
1 parent 448c020 commit 7934bec

File tree

5 files changed

+9
-4
lines changed

5 files changed

+9
-4
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build-deploy-url_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
3333
expect(result?.success).toBeTrue();
3434
expect(result?.baseUrl).toMatch(/\/test$/);
3535
expect(response?.url).toMatch(/\/test\/runtime.js$/);
36-
expect(await response?.text()).toContain('self["webpackChunk"]');
36+
expect(await response?.text()).toContain('self["webpackChunktest"]');
3737
});
3838
});
3939
});

packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/serve-path_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
3535
expect(result?.success).toBeTrue();
3636
const baseUrl = new URL(`${result?.baseUrl}`);
3737
expect(baseUrl.pathname).toBe('/');
38-
expect(await response?.text()).toContain('self["webpackChunk"]');
38+
expect(await response?.text()).toContain('self["webpackChunktest"]');
3939
});
4040

4141
it('serves application at specified path when option is used', async () => {
@@ -49,7 +49,7 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
4949
expect(result?.success).toBeTrue();
5050
const baseUrl = new URL(`${result?.baseUrl}/`);
5151
expect(baseUrl.pathname).toBe('/test/');
52-
expect(await response?.text()).toContain('self["webpackChunk"]');
52+
expect(await response?.text()).toContain('self["webpackChunktest"]');
5353
});
5454

5555
it('does not rewrite from root when option is used', async () => {

packages/angular_devkit/build_angular/src/utils/build-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ export interface WebpackConfigOptions<T = BuildOptions> {
8989
tsConfig: ParsedConfiguration;
9090
tsConfigPath: string;
9191
scriptTarget: import('typescript').ScriptTarget;
92+
projectName: string;
9293
}

packages/angular_devkit/build_angular/src/utils/webpack-browser-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export async function generateWebpackConfig(
2929
workspaceRoot: string,
3030
projectRoot: string,
3131
sourceRoot: string | undefined,
32+
projectName: string,
3233
options: NormalizedBrowserBuilderSchema,
3334
webpackPartialGenerator: WebpackPartialGenerator,
3435
logger: logging.LoggerApi,
@@ -54,6 +55,7 @@ export async function generateWebpackConfig(
5455
buildOptions,
5556
tsConfig,
5657
tsConfigPath,
58+
projectName,
5759
scriptTarget,
5860
};
5961

@@ -164,6 +166,7 @@ export async function generateBrowserWebpackConfigFromContext(
164166
getSystemPath(workspaceRoot),
165167
getSystemPath(projectRoot),
166168
sourceRoot && getSystemPath(sourceRoot),
169+
projectName,
167170
normalizedOptions,
168171
webpackPartialGenerator,
169172
context.logger,

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { getOutputHashFormat, getWatchOptions, normalizeExtraEntryPoints } from
3333

3434
// eslint-disable-next-line max-lines-per-function
3535
export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Configuration> {
36-
const { root, projectRoot, buildOptions, tsConfig } = wco;
36+
const { root, projectRoot, buildOptions, tsConfig, projectName } = wco;
3737
const {
3838
cache,
3939
platform = 'browser',
@@ -337,6 +337,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
337337
context: root,
338338
entry: entryPoints,
339339
output: {
340+
uniqueName: projectName,
340341
hashFunction: 'xxhash64', // todo: remove in webpack 6. This is part of `futureDefaults`.
341342
clean: buildOptions.deleteOutputPath ?? true,
342343
path: path.resolve(root, buildOptions.outputPath),

0 commit comments

Comments
 (0)