1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
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
|
25 |
|
26 | .filter((f) => fileRegExp.test(f) && !/(module|spec)\.ts$/.test(f))
|
27 |
|
28 | .sort();
|
29 | if (siblingModules.length === 0) {
|
30 |
|
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 |
|
49 | const originalContent = host.readText(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 (0, 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 = (0, workspace_1.buildDefaultPath)(project);
|
70 | }
|
71 | const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
|
72 | options.name = parsedPath.name;
|
73 | options.path = parsedPath.path;
|
74 | const templateSourceWorkerCode = (0, schematics_1.apply)((0, schematics_1.url)('./files/worker'), [
|
75 | (0, schematics_1.applyTemplates)({ ...options, ...schematics_1.strings }),
|
76 | (0, schematics_1.move)(parsedPath.path),
|
77 | ]);
|
78 | const root = project.root || '';
|
79 | const templateSourceWorkerConfig = (0, schematics_1.apply)((0, schematics_1.url)('./files/worker-tsconfig'), [
|
80 | (0, schematics_1.applyTemplates)({
|
81 | ...options,
|
82 | relativePathToWorkspaceRoot: (0, paths_1.relativePathToWorkspaceRoot)(root),
|
83 | }),
|
84 | (0, schematics_1.move)(root),
|
85 | ]);
|
86 | return (0, schematics_1.chain)([
|
87 |
|
88 | (0, workspace_1.updateWorkspace)((workspace) => {
|
89 |
|
90 | const project = workspace.projects.get(options.project);
|
91 | const buildTarget = project.targets.get('build');
|
92 | const testTarget = project.targets.get('test');
|
93 | if (!buildTarget) {
|
94 | throw new Error(`Build target is not defined for this project.`);
|
95 | }
|
96 | const workerConfigPath = (0, core_1.join)((0, core_1.normalize)(root), 'tsconfig.worker.json');
|
97 | (buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
|
98 | if (testTarget) {
|
99 | (testTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath;
|
100 | }
|
101 | }),
|
102 |
|
103 | options.snippet ? addSnippet(options) : (0, schematics_1.noop)(),
|
104 |
|
105 | (0, schematics_1.mergeWith)(templateSourceWorkerCode),
|
106 | (0, schematics_1.mergeWith)(templateSourceWorkerConfig),
|
107 | ]);
|
108 | };
|
109 | }
|
110 | exports.default = default_1;
|