UNPKG

2.46 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.buildOptimizerLoaderPath = void 0;
11const webpack_1 = require("webpack");
12const build_optimizer_1 = require("./build-optimizer");
13exports.buildOptimizerLoaderPath = __filename;
14const alwaysProcess = (path) => path.endsWith('.ts') || path.endsWith('.tsx');
15function buildOptimizerLoader(content, previousSourceMap) {
16 this.cacheable();
17 const skipBuildOptimizer = this._module && this._module.factoryMeta && this._module.factoryMeta.skipBuildOptimizer;
18 if (!alwaysProcess(this.resourcePath) && skipBuildOptimizer) {
19 // Skip loading processing this file with Build Optimizer if we determined in
20 // BuildOptimizerWebpackPlugin that we shouldn't.
21 this.callback(null, content, previousSourceMap);
22 return;
23 }
24 const options = (this.getOptions() || {});
25 const boOutput = build_optimizer_1.buildOptimizer({
26 content,
27 originalFilePath: this.resourcePath,
28 inputFilePath: this.resourcePath,
29 outputFilePath: this.resourcePath,
30 emitSourceMap: options.sourceMap,
31 isSideEffectFree: this._module && this._module.factoryMeta && this._module.factoryMeta.sideEffectFree,
32 });
33 if (boOutput.emitSkipped || boOutput.content === null) {
34 this.callback(null, content, previousSourceMap);
35 return;
36 }
37 const intermediateSourceMap = boOutput.sourceMap;
38 let newContent = boOutput.content;
39 let newSourceMap;
40 if (options.sourceMap && intermediateSourceMap) {
41 // Webpack doesn't need sourceMappingURL since we pass them on explicitely.
42 newContent = newContent.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
43 if (previousSourceMap) {
44 // Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
45 newSourceMap = new webpack_1.sources.SourceMapSource(newContent, this.resourcePath, intermediateSourceMap, content, previousSourceMap, true).map();
46 }
47 else {
48 // Otherwise just return our generated sourcemap.
49 newSourceMap = intermediateSourceMap;
50 }
51 }
52 this.callback(null, newContent, newSourceMap);
53}
54exports.default = buildOptimizerLoader;