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 | exports.ScriptsWebpackPlugin = void 0;
|
---|
30 | const loader_utils_1 = require("loader-utils");
|
---|
31 | const path = __importStar(require("path"));
|
---|
32 | const webpack_1 = require("webpack");
|
---|
33 | const Entrypoint = require('webpack/lib/Entrypoint');
|
---|
34 | function addDependencies(compilation, scripts) {
|
---|
35 | for (const script of scripts) {
|
---|
36 | compilation.fileDependencies.add(script);
|
---|
37 | }
|
---|
38 | }
|
---|
39 | class ScriptsWebpackPlugin {
|
---|
40 | constructor(options) {
|
---|
41 | this.options = options;
|
---|
42 | }
|
---|
43 | async shouldSkip(compilation, scripts) {
|
---|
44 | if (this._lastBuildTime == undefined) {
|
---|
45 | this._lastBuildTime = Date.now();
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | for (const script of scripts) {
|
---|
49 | const scriptTime = await new Promise((resolve, reject) => {
|
---|
50 | compilation.fileSystemInfo.getFileTimestamp(script, (error, entry) => {
|
---|
51 | if (error) {
|
---|
52 | reject(error);
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | resolve(entry && typeof entry !== 'string' ? entry.safeTime : undefined);
|
---|
56 | });
|
---|
57 | });
|
---|
58 | if (!scriptTime || scriptTime > this._lastBuildTime) {
|
---|
59 | this._lastBuildTime = Date.now();
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 | _insertOutput(compilation, { filename, source }, cached = false) {
|
---|
66 | const chunk = new webpack_1.Chunk(this.options.name);
|
---|
67 | chunk.rendered = !cached;
|
---|
68 | chunk.id = this.options.name;
|
---|
69 | chunk.ids = [chunk.id];
|
---|
70 | chunk.files.add(filename);
|
---|
71 | const entrypoint = new Entrypoint(this.options.name);
|
---|
72 | entrypoint.pushChunk(chunk);
|
---|
73 | chunk.addGroup(entrypoint);
|
---|
74 | compilation.entrypoints.set(this.options.name, entrypoint);
|
---|
75 | compilation.chunks.add(chunk);
|
---|
76 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
77 | compilation.assets[filename] = source;
|
---|
78 | compilation.hooks.chunkAsset.call(chunk, filename);
|
---|
79 | }
|
---|
80 | apply(compiler) {
|
---|
81 | if (!this.options.scripts || this.options.scripts.length === 0) {
|
---|
82 | return;
|
---|
83 | }
|
---|
84 | const scripts = this.options.scripts
|
---|
85 | .filter((script) => !!script)
|
---|
86 | .map((script) => path.resolve(this.options.basePath || '', script));
|
---|
87 | compiler.hooks.thisCompilation.tap('scripts-webpack-plugin', (compilation) => {
|
---|
88 | compilation.hooks.additionalAssets.tapPromise('scripts-webpack-plugin', async () => {
|
---|
89 | if (await this.shouldSkip(compilation, scripts)) {
|
---|
90 | if (this._cachedOutput) {
|
---|
91 | this._insertOutput(compilation, this._cachedOutput, true);
|
---|
92 | }
|
---|
93 | addDependencies(compilation, scripts);
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | const sourceGetters = scripts.map((fullPath) => {
|
---|
97 | return new Promise((resolve, reject) => {
|
---|
98 | compilation.inputFileSystem.readFile(fullPath, (err, data) => {
|
---|
99 | var _a;
|
---|
100 | if (err) {
|
---|
101 | reject(err);
|
---|
102 | return;
|
---|
103 | }
|
---|
104 | const content = (_a = data === null || data === void 0 ? void 0 : data.toString()) !== null && _a !== void 0 ? _a : '';
|
---|
105 | let source;
|
---|
106 | if (this.options.sourceMap) {
|
---|
107 | // TODO: Look for source map file (for '.min' scripts, etc.)
|
---|
108 | let adjustedPath = fullPath;
|
---|
109 | if (this.options.basePath) {
|
---|
110 | adjustedPath = path.relative(this.options.basePath, fullPath);
|
---|
111 | }
|
---|
112 | source = new webpack_1.sources.OriginalSource(content, adjustedPath);
|
---|
113 | }
|
---|
114 | else {
|
---|
115 | source = new webpack_1.sources.RawSource(content);
|
---|
116 | }
|
---|
117 | resolve(source);
|
---|
118 | });
|
---|
119 | });
|
---|
120 | });
|
---|
121 | const sources = await Promise.all(sourceGetters);
|
---|
122 | const concatSource = new webpack_1.sources.ConcatSource();
|
---|
123 | sources.forEach((source) => {
|
---|
124 | concatSource.add(source);
|
---|
125 | concatSource.add('\n;');
|
---|
126 | });
|
---|
127 | const combinedSource = new webpack_1.sources.CachedSource(concatSource);
|
---|
128 | const filename = loader_utils_1.interpolateName({ resourcePath: 'scripts.js' }, this.options.filename, {
|
---|
129 | content: combinedSource.source(),
|
---|
130 | });
|
---|
131 | const output = { filename, source: combinedSource };
|
---|
132 | this._insertOutput(compilation, output);
|
---|
133 | this._cachedOutput = output;
|
---|
134 | addDependencies(compilation, scripts);
|
---|
135 | });
|
---|
136 | });
|
---|
137 | }
|
---|
138 | }
|
---|
139 | exports.ScriptsWebpackPlugin = ScriptsWebpackPlugin;
|
---|