Skip to content

Commit add3f82

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

File tree

13 files changed

+343
-157
lines changed

13 files changed

+343
-157
lines changed

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

+4-5
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+
"webWorkerTsConfig": {
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

+1-1
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+
webWorkerTsConfig?: string;
5858
skipAppShell?: boolean;
5959
statsJson: boolean;
6060
forkTypeChecker: boolean;

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

-6
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): Configuration {
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

+1
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

+21
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,24 @@ export function getAotConfig(wco: WebpackConfigOptions, extract = false) {
107107
plugins: [_createAotPlugin(wco, { tsConfigPath }, true, extract)]
108108
};
109109
}
110+
111+
export function getTypescriptWorkerPlugin(wco: WebpackConfigOptions, workerTsConfigPath: string) {
112+
const { buildOptions } = wco;
113+
114+
const pluginOptions: AngularCompilerPluginOptions = {
115+
skipCodeGeneration: true,
116+
tsConfigPath: workerTsConfigPath,
117+
mainPath: undefined,
118+
platform: PLATFORM.Browser,
119+
sourceMap: buildOptions.sourceMap.scripts,
120+
forkTypeChecker: buildOptions.forkTypeChecker,
121+
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
122+
logger: wco.logger,
123+
// Run no transformers.
124+
platformTransformers: [],
125+
// Don't attempt lazy route discovery.
126+
discoverLazyRoutes: false,
127+
};
128+
129+
return new AngularCompilerPlugin(pluginOptions);
130+
}
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.webWorkerTsConfig) {
19+
throw new Error('The `webWorkerTsConfig` must be a string.');
20+
}
21+
22+
const workerTsConfigPath = resolve(wco.root, buildOptions.webWorkerTsConfig);
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

+5
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.webWorkerTsConfig) {
159+
webpackConfigs.push(getWorkerConfig(wco));
160+
}
161+
157162
const webpackConfig = webpackMerge(webpackConfigs);
158163

159164
if (options.profile || process.env['NG_BUILD_PROFILING']) {

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,6 @@
259259
"description": "Generates a service worker config for production builds.",
260260
"default": false
261261
},
262-
"autoBundleWorkerModules": {
263-
"type": "boolean",
264-
"description": "Automatically bundle new Worker('..', { type:'module' })",
265-
"default": true
266-
},
267262
"ngswConfigPath": {
268263
"type": "string",
269264
"description": "Path to ngsw-config.json."
@@ -320,6 +315,10 @@
320315
"type": "boolean",
321316
"default": false,
322317
"x-deprecated": true
318+
},
319+
"webWorkerTsConfig": {
320+
"type": "string",
321+
"description": "TypeScript configuration for Web Worker modules."
323322
}
324323
},
325324
"additionalProperties": false,

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

-91
This file was deleted.

0 commit comments

Comments
 (0)