1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | const core_1 = require("@angular-devkit/core");
|
---|
11 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
12 | const parse_name_1 = require("../utility/parse-name");
|
---|
13 | const paths_1 = require("../utility/paths");
|
---|
14 | const workspace_1 = require("../utility/workspace");
|
---|
15 | function addSnippet(options) {
|
---|
16 | return (host, context) => {
|
---|
17 | context.logger.debug('Updating appmodule');
|
---|
18 | if (options.path === undefined) {
|
---|
19 | return;
|
---|
20 | }
|
---|
21 | const fileRegExp = new RegExp(`^${options.name}.*\.ts`);
|
---|
22 | const siblingModules = host
|
---|
23 | .getDir(options.path)
|
---|
24 | .subfiles // Find all files that start with the same name, are ts files,
|
---|
25 | // and aren't spec or module files.
|
---|
26 | .filter((f) => fileRegExp.test(f) && !/(module|spec)\.ts$/.test(f))
|
---|
27 | // Sort alphabetically for consistency.
|
---|
28 | .sort();
|
---|
29 | if (siblingModules.length === 0) {
|
---|
30 | // No module to add in.
|
---|
31 | return;
|
---|
32 | }
|
---|
33 | const siblingModulePath = `${options.path}/${siblingModules[0]}`;
|
---|
34 | const logMessage = 'console.log(`page got message: ${data}`);';
|
---|
35 | const workerCreationSnippet = core_1.tags.stripIndent `
|
---|
36 | if (typeof Worker !== 'undefined') {
|
---|
37 | // Create a new
|
---|
38 | const worker = new Worker(new URL('./${options.name}.worker', import.meta.url));
|
---|
39 | worker.onmessage = ({ data }) => {
|
---|
40 | ${logMessage}
|
---|
41 | };
|
---|
42 | worker.postMessage('hello');
|
---|
43 | } else {
|
---|
44 | // Web Workers are not supported in this environment.
|
---|
45 | // You should add a fallback so that your program still executes correctly.
|
---|
46 | }
|
---|
47 | `;
|
---|
48 | // Append the worker creation snippet.
|
---|
49 | const originalContent = host.read(siblingModulePath);
|
---|
50 | host.overwrite(siblingModulePath, originalContent + '\n' + workerCreationSnippet);
|
---|
51 | return host;
|
---|
52 | };
|
---|
53 | }
|
---|
54 | function default_1(options) {
|
---|
55 | return async (host) => {
|
---|
56 | const workspace = await workspace_1.getWorkspace(host);
|
---|
57 | if (!options.project) {
|
---|
58 | throw new schematics_1.SchematicsException('Option "project" is required.');
|
---|
59 | }
|
---|
60 | const project = workspace.projects.get(options.project);
|
---|
61 | if (!project) {
|
---|
62 | throw new schematics_1.SchematicsException(`Invalid project name (${options.project})`);
|
---|
63 | }
|
---|
64 | const projectType = project.extensions['projectType'];
|
---|
65 | if (projectType !== 'application') {
|
---|
66 | throw new schematics_1.SchematicsException(`Web Worker requires a project type of "application".`);
|
---|
67 | }
|
---|
68 | if (options.path === undefined) {
|
---|
69 | options.path = workspace_1.buildDefaultPath(project);
|
---|
70 | }
|
---|
71 | const parsedPath = parse_name_1.parseName(options.path, options.name);
|
---|
72 | options.name = parsedPath.name;
|
---|
73 | options.path = parsedPath.path;
|
---|
74 | const templateSourceWorkerCode = schematics_1.apply(schematics_1.url('./files/worker'), [
|
---|
75 | schematics_1.applyTemplates({ ...options, ...core_1.strings }),
|
---|
76 | schematics_1.move(parsedPath.path),
|
---|
77 | ]);
|
---|
78 | const root = project.root || '';
|
---|
79 | const templateSourceWorkerConfig = schematics_1.apply(schematics_1.url('./files/worker-tsconfig'), [
|
---|
80 | schematics_1.applyTemplates({
|
---|
81 | ...options,
|
---|
82 | relativePathToWorkspaceRoot: paths_1.relativePathToWorkspaceRoot(root),
|
---|
83 | }),
|
---|
84 | schematics_1.move(root),
|
---|
85 | ]);
|
---|
86 | return schematics_1.chain([
|
---|
87 | // Add project configuration.
|
---|
88 | workspace_1.updateWorkspace((workspace) => {
|
---|
89 | var _a, _b, _c, _d;
|
---|
90 | var _e, _f;
|
---|
91 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
92 | const project = workspace.projects.get(options.project);
|
---|
93 | const buildTarget = project.targets.get('build');
|
---|
94 | const testTarget = project.targets.get('test');
|
---|
95 | if (!buildTarget) {
|
---|
96 | throw new Error(`Build target is not defined for this project.`);
|
---|
97 | }
|
---|
98 | const workerConfigPath = core_1.join(core_1.normalize(root), 'tsconfig.worker.json');
|
---|
99 | (_b = (_e = ((_a = buildTarget.options) !== null && _a !== void 0 ? _a : (buildTarget.options = {}))).webWorkerTsConfig) !== null && _b !== void 0 ? _b : (_e.webWorkerTsConfig = workerConfigPath);
|
---|
100 | if (testTarget) {
|
---|
101 | (_d = (_f = ((_c = testTarget.options) !== null && _c !== void 0 ? _c : (testTarget.options = {}))).webWorkerTsConfig) !== null && _d !== void 0 ? _d : (_f.webWorkerTsConfig = workerConfigPath);
|
---|
102 | }
|
---|
103 | }),
|
---|
104 | // Create the worker in a sibling module.
|
---|
105 | options.snippet ? addSnippet(options) : schematics_1.noop(),
|
---|
106 | // Add the worker.
|
---|
107 | schematics_1.mergeWith(templateSourceWorkerCode),
|
---|
108 | schematics_1.mergeWith(templateSourceWorkerConfig),
|
---|
109 | ]);
|
---|
110 | };
|
---|
111 | }
|
---|
112 | exports.default = default_1;
|
---|