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 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | const ts = __importStar(require("../../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
---|
30 | function* visit(directory) {
|
---|
31 | for (const path of directory.subfiles) {
|
---|
32 | if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
|
---|
33 | const entry = directory.file(path);
|
---|
34 | if (entry) {
|
---|
35 | const content = entry.content;
|
---|
36 | if (content.includes('Worker')) {
|
---|
37 | const source = ts.createSourceFile(entry.path,
|
---|
38 | // Remove UTF-8 BOM if present
|
---|
39 | // TypeScript expects the BOM to be stripped prior to parsing
|
---|
40 | content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true);
|
---|
41 | yield source;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | for (const path of directory.subdirs) {
|
---|
47 | if (path === 'node_modules' || path.startsWith('.')) {
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 | yield* visit(directory.dir(path));
|
---|
51 | }
|
---|
52 | }
|
---|
53 | function hasPropertyWithValue(node, name, value) {
|
---|
54 | if (!ts.isObjectLiteralExpression(node)) {
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | for (const property of node.properties) {
|
---|
58 | if (!ts.isPropertyAssignment(property)) {
|
---|
59 | continue;
|
---|
60 | }
|
---|
61 | if (!ts.isIdentifier(property.name) || property.name.text !== 'type') {
|
---|
62 | continue;
|
---|
63 | }
|
---|
64 | if (ts.isStringLiteralLike(property.initializer)) {
|
---|
65 | return property.initializer.text === 'module';
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | function default_1() {
|
---|
71 | return (tree) => {
|
---|
72 | for (const sourceFile of visit(tree.root)) {
|
---|
73 | let recorder;
|
---|
74 | ts.forEachChild(sourceFile, function analyze(node) {
|
---|
75 | var _a;
|
---|
76 | // Only modify code in the form of `new Worker('./app.worker', { type: 'module' })`.
|
---|
77 | // `worker-plugin` required the second argument to be an object literal with type=module
|
---|
78 | if (ts.isNewExpression(node) &&
|
---|
79 | ts.isIdentifier(node.expression) &&
|
---|
80 | node.expression.text === 'Worker' &&
|
---|
81 | ((_a = node.arguments) === null || _a === void 0 ? void 0 : _a.length) === 2 &&
|
---|
82 | ts.isStringLiteralLike(node.arguments[0]) &&
|
---|
83 | hasPropertyWithValue(node.arguments[1], 'type', 'module')) {
|
---|
84 | const valueNode = node.arguments[0];
|
---|
85 | // Webpack expects a URL constructor: https://webpack.js.org/guides/web-workers/
|
---|
86 | const fix = `new URL('${valueNode.text}', import.meta.url)`;
|
---|
87 | if (!recorder) {
|
---|
88 | recorder = tree.beginUpdate(sourceFile.fileName);
|
---|
89 | }
|
---|
90 | const index = valueNode.getStart();
|
---|
91 | const length = valueNode.getWidth();
|
---|
92 | recorder.remove(index, length).insertLeft(index, fix);
|
---|
93 | }
|
---|
94 | ts.forEachChild(node, analyze);
|
---|
95 | });
|
---|
96 | if (recorder) {
|
---|
97 | tree.commitUpdate(recorder);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | };
|
---|
101 | }
|
---|
102 | exports.default = default_1;
|
---|