Skip to content

Commit 1e3e52e

Browse files
committed
refactor(@angular-devkit/build-angular): support web worker in new architect
1 parent 3e6c590 commit 1e3e52e

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

Diff for: packages/angular_devkit/build_angular/src/browser/index2.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']) {

Diff for: packages/angular_devkit/build_angular/test/browser/web-worker_spec_large.ts

+49-53
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { DefaultTimeout, TestLogger, runTargetSpec } from '@angular-devkit/architect/testing';
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { TestLogger } from '@angular-devkit/architect/testing';
1011
import { join, virtualFs } from '@angular-devkit/core';
1112
import { debounceTime, takeWhile, tap } from 'rxjs/operators';
12-
import { browserTargetSpec, host, outputPath } from '../utils';
13+
import { browserBuild, createArchitect, host, outputPath } from '../utils';
1314

1415

1516
describe('Browser Builder Web Worker support', () => {
16-
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
17-
afterEach(done => host.restore().toPromise().then(done, done.fail));
17+
const target = { project: 'app', target: 'build' };
18+
let architect: Architect;
19+
20+
beforeEach(async () => {
21+
await host.initialize().toPromise();
22+
architect = (await createArchitect(host.root())).architect;
23+
});
24+
afterEach(async () => host.restore().toPromise());
1825

1926
const workerFiles: { [k: string]: string } = {
2027
'src/app/dep.ts': `export const foo = 'bar';`,
@@ -77,62 +84,52 @@ describe('Browser Builder Web Worker support', () => {
7784
}`,
7885
};
7986

80-
it('bundles TS worker', (done) => {
81-
const logger = new TestLogger('worker-warnings');
87+
it('bundles TS worker', async () => {
8288
host.writeMultipleFiles(workerFiles);
89+
const logger = new TestLogger('worker-warnings');
8390
const overrides = { webWorkerTsConfig: 'src/tsconfig.worker.json' };
84-
runTargetSpec(host, browserTargetSpec, overrides, DefaultTimeout, logger).pipe(
85-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
86-
tap(() => {
87-
const workerContent = virtualFs.fileBufferToString(
88-
host.scopedSync().read(join(outputPath, '0.worker.js')),
89-
);
90-
// worker bundle contains worker code.
91-
expect(workerContent).toContain('hello from worker');
92-
expect(workerContent).toContain('bar');
93-
94-
const mainContent = virtualFs.fileBufferToString(
95-
host.scopedSync().read(join(outputPath, 'main.js')),
96-
);
97-
// main bundle references worker.
98-
expect(mainContent).toContain('0.worker.js');
99-
}),
100-
// Doesn't show any warnings.
101-
tap(() => expect(logger.includes('WARNING')).toBe(false, 'Should show no warnings.')),
102-
).toPromise().then(done, done.fail);
91+
await browserBuild(architect, host, target, overrides, { logger });
92+
93+
// Worker bundle contains worker code.
94+
const workerContent = virtualFs.fileBufferToString(
95+
host.scopedSync().read(join(outputPath, '0.worker.js')));
96+
expect(workerContent).toContain('hello from worker');
97+
expect(workerContent).toContain('bar');
98+
99+
// Main bundle references worker.
100+
const mainContent = virtualFs.fileBufferToString(
101+
host.scopedSync().read(join(outputPath, 'main.js')));
102+
expect(mainContent).toContain('0.worker.js');
103+
expect(logger.includes('WARNING')).toBe(false, 'Should show no warnings.');
103104
});
104105

105-
it('minimizes and hashes worker', (done) => {
106+
it('minimizes and hashes worker', async () => {
106107
host.writeMultipleFiles(workerFiles);
107108
const overrides = {
108109
webWorkerTsConfig: 'src/tsconfig.worker.json',
109110
outputHashing: 'all',
110111
optimization: true,
111112
};
112-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
113-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
114-
tap(() => {
115-
const workerBundle = host.fileMatchExists(outputPath,
116-
/0\.[0-9a-f]{20}\.worker\.js/) as string;
117-
expect(workerBundle).toBeTruthy('workerBundle should exist');
118-
const workerContent = virtualFs.fileBufferToString(
119-
host.scopedSync().read(join(outputPath, workerBundle)),
120-
);
121-
expect(workerContent).toContain('hello from worker');
122-
expect(workerContent).toContain('bar');
123-
expect(workerContent).toContain('"hello"===t&&postMessage');
124-
125-
const mainBundle = host.fileMatchExists(outputPath, /main\.[0-9a-f]{20}\.js/) as string;
126-
expect(mainBundle).toBeTruthy('mainBundle should exist');
127-
const mainContent = virtualFs.fileBufferToString(
128-
host.scopedSync().read(join(outputPath, mainBundle)),
129-
);
130-
expect(mainContent).toContain(workerBundle);
131-
}),
132-
).toPromise().then(done, done.fail);
113+
await browserBuild(architect, host, target, overrides);
114+
115+
// Worker bundle should have hash and minified code.
116+
const workerBundle = host.fileMatchExists(outputPath, /0\.[0-9a-f]{20}\.worker\.js/) as string;
117+
expect(workerBundle).toBeTruthy('workerBundle should exist');
118+
const workerContent = virtualFs.fileBufferToString(
119+
host.scopedSync().read(join(outputPath, workerBundle)));
120+
expect(workerContent).toContain('hello from worker');
121+
expect(workerContent).toContain('bar');
122+
expect(workerContent).toContain('"hello"===t&&postMessage');
123+
124+
// Main bundle should reference hashed worker bundle.
125+
const mainBundle = host.fileMatchExists(outputPath, /main\.[0-9a-f]{20}\.js/) as string;
126+
expect(mainBundle).toBeTruthy('mainBundle should exist');
127+
const mainContent = virtualFs.fileBufferToString(
128+
host.scopedSync().read(join(outputPath, mainBundle)));
129+
expect(mainContent).toContain(workerBundle);
133130
});
134131

135-
it('rebuilds TS worker', (done) => {
132+
it('rebuilds TS worker', async () => {
136133
host.writeMultipleFiles(workerFiles);
137134
const overrides = {
138135
webWorkerTsConfig: 'src/tsconfig.worker.json',
@@ -144,7 +141,8 @@ describe('Browser Builder Web Worker support', () => {
144141
const workerPath = join(outputPath, '0.worker.js');
145142
let workerContent = '';
146143

147-
runTargetSpec(host, browserTargetSpec, overrides, DefaultTimeout * 3).pipe(
144+
const run = await architect.scheduleTarget(target, overrides);
145+
await run.output.pipe(
148146
// Wait for files to be written to disk.
149147
debounceTime(1000),
150148
tap((buildEvent) => expect(buildEvent.success).toBe(true, 'build should succeed')),
@@ -178,9 +176,7 @@ describe('Browser Builder Web Worker support', () => {
178176
}
179177
}),
180178
takeWhile(() => phase < 3),
181-
).toPromise().then(
182-
() => done(),
183-
() => done.fail(`stuck at phase ${phase} [builds: ${buildCount}]`),
184-
);
179+
).toPromise();
180+
await run.stop();
185181
});
186182
});

0 commit comments

Comments
 (0)