Skip to content

Commit f11bf15

Browse files
committed
feat(@angular-devkit/build-angular): support TS web workers
1 parent 3a30ec5 commit f11bf15

File tree

12 files changed

+336
-155
lines changed

12 files changed

+336
-155
lines changed

packages/angular/cli/lib/config/schema.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,6 @@
836836
"ngswConfigPath": {
837837
"type": "string",
838838
"description": "Path to ngsw-config.json."
839-
},
840-
"autoBundleWorkerModules": {
841-
"type": "boolean",
842-
"description": "Automatically bundle new Worker('..', { type:'module' })",
843-
"default": true
844839
},
845840
"skipAppShell": {
846841
"type": "boolean",
@@ -887,6 +882,10 @@
887882
"type": "boolean",
888883
"default": false,
889884
"x-deprecated": true
885+
},
886+
"experimentalWebWorkerTsConfig": {
887+
"type": "string",
888+
"description": "TypeScript configuration for Web Worker modules."
890889
}
891890
},
892891
"additionalProperties": false,

packages/angular_devkit/build_angular/src/angular-cli-files/models/build-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface BuildOptions {
5454
namedChunks?: boolean;
5555
subresourceIntegrity?: boolean;
5656
serviceWorker?: boolean;
57-
autoBundleWorkerModules?: boolean;
57+
experimentalWebWorkerTsConfig?: string;
5858
skipAppShell?: boolean;
5959
statsJson: boolean;
6060
forkTypeChecker: boolean;

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const ProgressPlugin = require('webpack/lib/ProgressPlugin');
2222
const CircularDependencyPlugin = require('circular-dependency-plugin');
2323
const TerserPlugin = require('terser-webpack-plugin');
2424
const StatsPlugin = require('stats-webpack-plugin');
25-
const WorkerPlugin = require('worker-plugin');
2625

2726

2827
// tslint:disable-next-line:no-any
@@ -128,11 +127,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
128127
});
129128
}
130129

131-
if (buildOptions.autoBundleWorkerModules) {
132-
const workerPluginInstance = new WorkerPlugin({ globalObject: false });
133-
extraPlugins.push(workerPluginInstance);
134-
}
135-
136130
// process asset entries
137131
if (buildOptions.assets) {
138132
const copyWebpackPluginPatterns = buildOptions.assets.map((asset: AssetPatternClass) => {

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './test';
1313
export * from './typescript';
1414
export * from './utils';
1515
export * from './stats';
16+
export * from './worker';

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/typescript.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,24 @@ export function getNonAotTestConfig(wco: WebpackConfigOptions, host: virtualFs.H
123123
plugins: [_createAotPlugin(wco, { tsConfigPath, skipCodeGeneration: true }, host, false)]
124124
};
125125
}
126+
127+
export function getTypescriptWorkerPlugin(wco: WebpackConfigOptions, workerTsConfigPath: string) {
128+
const { buildOptions } = wco;
129+
130+
const pluginOptions: AngularCompilerPluginOptions = {
131+
skipCodeGeneration: true,
132+
tsConfigPath: workerTsConfigPath,
133+
mainPath: undefined,
134+
platform: PLATFORM.Browser,
135+
sourceMap: buildOptions.sourceMap.scripts,
136+
forkTypeChecker: buildOptions.forkTypeChecker,
137+
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
138+
logger: wco.logger,
139+
// Run no transformers.
140+
platformTransformers: [],
141+
// Don't attempt lazy route discovery.
142+
discoverLazyRoutes: false,
143+
};
144+
145+
return new AngularCompilerPlugin(pluginOptions);
146+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { resolve } from 'path';
9+
import { Configuration } from 'webpack';
10+
import { WebpackConfigOptions } from '../build-options';
11+
import { getTypescriptWorkerPlugin } from './typescript';
12+
13+
const WorkerPlugin = require('worker-plugin');
14+
15+
16+
export function getWorkerConfig(wco: WebpackConfigOptions): Configuration {
17+
const { buildOptions } = wco;
18+
if (!buildOptions.experimentalWebWorkerTsConfig) {
19+
throw new Error('The `experimentalWebWorkerTsConfig` must be a string.');
20+
}
21+
22+
const workerTsConfigPath = resolve(wco.root, buildOptions.experimentalWebWorkerTsConfig);
23+
24+
return {
25+
plugins: [new WorkerPlugin({
26+
globalObject: false,
27+
plugins: [getTypescriptWorkerPlugin(wco, workerTsConfigPath)],
28+
})],
29+
};
30+
}

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getNonAotConfig,
2626
getStatsConfig,
2727
getStylesConfig,
28+
getWorkerConfig,
2829
} from '../angular-cli-files/models/webpack-configs';
2930
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
3031
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module';
@@ -154,6 +155,10 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
154155
webpackConfigs.push(typescriptConfigPartial);
155156
}
156157

158+
if (wco.buildOptions.experimentalWebWorkerTsConfig) {
159+
webpackConfigs.push(getWorkerConfig(wco));
160+
}
161+
157162
const webpackConfig = webpackMerge(webpackConfigs);
158163

159164
if (options.profile) {

packages/angular_devkit/build_angular/src/browser/schema.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,6 @@
257257
"description": "Generates a service worker config for production builds.",
258258
"default": false
259259
},
260-
"autoBundleWorkerModules": {
261-
"type": "boolean",
262-
"description": "Automatically bundle new Worker('..', { type:'module' })",
263-
"default": true
264-
},
265260
"ngswConfigPath": {
266261
"type": "string",
267262
"description": "Path to ngsw-config.json."
@@ -317,6 +312,10 @@
317312
"type": "boolean",
318313
"default": false,
319314
"x-deprecated": true
315+
},
316+
"experimentalWebWorkerTsConfig": {
317+
"type": "string",
318+
"description": "TypeScript configuration for Web Worker modules."
320319
}
321320
},
322321
"additionalProperties": false,

packages/angular_devkit/build_angular/test/browser/bundle-worker_spec_large.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)