UNPKG

2.94 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 */
9var __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}));
16var __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});
21var __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};
28Object.defineProperty(exports, "__esModule", { value: true });
29/**
30 * @fileoverview This adapts the buildOptimizer to run over each file as it is
31 * processed by Rollup. We must do this since buildOptimizer expects to see the
32 * ESModules in the input sources, and therefore cannot run on the rollup output
33 */
34const path = __importStar(require("path"));
35const build_optimizer_1 = require("./build-optimizer");
36const DEBUG = false;
37function optimizer(options) {
38 // Normalize paths for comparison.
39 if (options.sideEffectFreeModules) {
40 options.sideEffectFreeModules = options.sideEffectFreeModules.map((p) => p.replace(/\\/g, '/'));
41 }
42 return {
43 name: 'build-optimizer',
44 transform: (content, id) => {
45 const normalizedId = id.replace(/\\/g, '/');
46 const isSideEffectFree = options.sideEffectFreeModules &&
47 options.sideEffectFreeModules.some((m) => normalizedId.indexOf(m) >= 0);
48 const isAngularCoreFile = options.angularCoreModules &&
49 options.angularCoreModules.some((m) => normalizedId.indexOf(m) >= 0);
50 const { content: code, sourceMap: map } = build_optimizer_1.buildOptimizer({
51 content,
52 inputFilePath: id,
53 emitSourceMap: true,
54 isSideEffectFree,
55 isAngularCoreFile,
56 });
57 if (!code) {
58 if (DEBUG) {
59 // eslint-disable-next-line no-console
60 console.error('no transforms produced by buildOptimizer for ' + path.relative(process.cwd(), id));
61 }
62 return null;
63 }
64 if (!map) {
65 throw new Error('no sourcemap produced by buildOptimizer');
66 }
67 return { code, map };
68 },
69 };
70}
71exports.default = optimizer;