Skip to content

Commit 6398428

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

File tree

13 files changed

+342
-156
lines changed

13 files changed

+342
-156
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
@@ -28,7 +28,6 @@ const ProgressPlugin = require('webpack/lib/ProgressPlugin');
2828
const CircularDependencyPlugin = require('circular-dependency-plugin');
2929
const TerserPlugin = require('terser-webpack-plugin');
3030
const StatsPlugin = require('stats-webpack-plugin');
31-
const WorkerPlugin = require('worker-plugin');
3231

3332

3433
// tslint:disable-next-line:no-any
@@ -134,11 +133,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
134133
});
135134
}
136135

137-
if (buildOptions.autoBundleWorkerModules) {
138-
const workerPluginInstance = new WorkerPlugin({ globalObject: false });
139-
extraPlugins.push(workerPluginInstance);
140-
}
141-
142136
// process asset entries
143137
if (buildOptions.assets) {
144138
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
@@ -108,3 +108,24 @@ export function getAotConfig(wco: WebpackConfigOptions, extract = false) {
108108
plugins: [_createAotPlugin(wco, { tsConfigPath }, true, extract)]
109109
};
110110
}
111+
112+
export function getTypescriptWorkerPlugin(wco: WebpackConfigOptions, workerTsConfigPath: string) {
113+
const { buildOptions } = wco;
114+
115+
const pluginOptions: AngularCompilerPluginOptions = {
116+
skipCodeGeneration: true,
117+
tsConfigPath: workerTsConfigPath,
118+
mainPath: undefined,
119+
platform: PLATFORM.Browser,
120+
sourceMap: buildOptions.sourceMap.scripts,
121+
forkTypeChecker: buildOptions.forkTypeChecker,
122+
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
123+
logger: wco.logger,
124+
// Run no transformers.
125+
platformTransformers: [],
126+
// Don't attempt lazy route discovery.
127+
discoverLazyRoutes: false,
128+
};
129+
130+
return new AngularCompilerPlugin(pluginOptions);
131+
}
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
@@ -42,6 +42,7 @@ import {
4242
getNonAotConfig,
4343
getStatsConfig,
4444
getStylesConfig,
45+
getWorkerConfig,
4546
} from '../angular-cli-files/models/webpack-configs';
4647
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
4748
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module';
@@ -161,6 +162,10 @@ export function buildWebpackConfig(
161162
webpackConfigs.push(typescriptConfigPartial);
162163
}
163164

165+
if (wco.buildOptions.webWorkerTsConfig) {
166+
webpackConfigs.push(getWorkerConfig(wco));
167+
}
168+
164169
const webpackConfig = webpackMerge(webpackConfigs);
165170

166171
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."
@@ -325,6 +320,10 @@
325320
"description": "**EXPERIMENTAL** Transform import statements for lazy routes to import factories when using View Engine. Should only be used when switching back and forth between View Engine and Ivy. See https://angular.io/guide/ivy for usage information.",
326321
"type": "boolean",
327322
"default": false
323+
},
324+
"webWorkerTsConfig": {
325+
"type": "string",
326+
"description": "TypeScript configuration for Web Worker modules."
328327
}
329328
},
330329
"additionalProperties": false,

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

-91
This file was deleted.

0 commit comments

Comments
 (0)