@matheo/ng-packagr
Version:
Compile and package Angular libraries in Angular Package Format (APF)
90 lines • 3.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeBundlesTransform = void 0;
const ora = require("ora");
const transform_1 = require("../../graph/transform");
const nodes_1 = require("../nodes");
const log = require("../../utils/log");
const array_1 = require("../../utils/array");
const downlevel_plugin_1 = require("../../flatten/downlevel-plugin");
const rollup_1 = require("../../flatten/rollup");
const uglify_1 = require("../../flatten/uglify");
exports.writeBundlesTransform = transform_1.transformFromPromise(async (graph) => {
const entryPoint = graph.find(nodes_1.isEntryPointInProgress());
const { destinationFiles, entryPoint: ngEntryPoint, tsConfig } = entryPoint.data;
const cache = entryPoint.cache;
// Add UMD module IDs for dependencies
const dependencyUmdIds = entryPoint
.filter(nodes_1.isEntryPoint)
.map(ep => ep.data.entryPoint)
.reduce((prev, ep) => {
prev[ep.moduleId] = ep.umdId;
return prev;
}, {});
const { fesm2015, esm2015, umd, umdMinified } = destinationFiles;
const opts = {
sourceRoot: tsConfig.options.sourceRoot,
amd: { id: ngEntryPoint.amdId },
umdModuleIds: {
...ngEntryPoint.umdModuleIds,
...dependencyUmdIds,
},
entry: esm2015,
dependencyList: getDependencyListForGraph(graph),
};
const spinner = ora({
hideCursor: false,
discardStdin: false,
});
try {
spinner.start('Bundling to FESM2015');
cache.rollupFESMCache = await rollup_1.rollupBundleFile({
...opts,
moduleName: ngEntryPoint.moduleId,
format: 'es',
dest: fesm2015,
cache: cache.rollupFESMCache,
});
spinner.succeed();
spinner.start('Bundling to UMD');
cache.rollupUMDCache = await rollup_1.rollupBundleFile({
...opts,
moduleName: ngEntryPoint.umdId,
entry: esm2015,
format: 'umd',
dest: umd,
cache: cache.rollupUMDCache,
transform: downlevel_plugin_1.downlevelCodeWithTsc,
});
spinner.succeed();
spinner.start('Minifying UMD bundle');
await uglify_1.minifyJsFile(umd, umdMinified);
spinner.succeed();
}
catch (error) {
spinner.fail();
throw error;
}
});
/** Get all list of dependencies for the entire 'BuildGraph' */
function getDependencyListForGraph(graph) {
// We need to do this because if A dependency on bundled B
// And A has a secondary entry point A/1 we want only to bundle B if it's used.
// Also if A/1 depends on A we don't want to bundle A thus we mark this a dependency.
const dependencyList = {
dependencies: [],
bundledDependencies: [],
};
for (const entry of graph.filter(nodes_1.isEntryPoint)) {
const { bundledDependencies = [], dependencies = {}, peerDependencies = {} } = entry.data.entryPoint.packageJson;
dependencyList.bundledDependencies = array_1.unique(dependencyList.bundledDependencies.concat(bundledDependencies));
dependencyList.dependencies = array_1.unique(dependencyList.dependencies.concat(Object.keys(dependencies), Object.keys(peerDependencies), entry.data.entryPoint.moduleId));
}
if (dependencyList.bundledDependencies.length) {
log.warn(`Inlining of 'bundledDependencies' has been deprecated in version 5 and will be removed in future versions.` +
'\n' +
`List the dependency in the 'peerDependencies' section instead.`);
}
return dependencyList;
}
//# sourceMappingURL=write-bundles.transform.js.map