@angular-devkit/build-optimizer
Version:
Angular Build Optimizer
72 lines (71 loc) • 2.95 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @fileoverview This adapts the buildOptimizer to run over each file as it is
* processed by Rollup. We must do this since buildOptimizer expects to see the
* ESModules in the input sources, and therefore cannot run on the rollup output
*/
const path = __importStar(require("path"));
const build_optimizer_1 = require("./build-optimizer");
const DEBUG = false;
function optimizer(options) {
// Normalize paths for comparison.
if (options.sideEffectFreeModules) {
options.sideEffectFreeModules = options.sideEffectFreeModules.map((p) => p.replace(/\\/g, '/'));
}
return {
name: 'build-optimizer',
transform: (content, id) => {
const normalizedId = id.replace(/\\/g, '/');
const isSideEffectFree = options.sideEffectFreeModules &&
options.sideEffectFreeModules.some((m) => normalizedId.indexOf(m) >= 0);
const isAngularCoreFile = options.angularCoreModules &&
options.angularCoreModules.some((m) => normalizedId.indexOf(m) >= 0);
const { content: code, sourceMap: map } = (0, build_optimizer_1.buildOptimizer)({
content,
inputFilePath: id,
emitSourceMap: true,
isSideEffectFree,
isAngularCoreFile,
});
if (!code) {
if (DEBUG) {
// eslint-disable-next-line no-console
console.error('no transforms produced by buildOptimizer for ' + path.relative(process.cwd(), id));
}
return null;
}
if (!map) {
throw new Error('no sourcemap produced by buildOptimizer');
}
return { code, map };
},
};
}
exports.default = optimizer;
;