UNPKG

ng-packagr

Version:

Compile and package Angular libraries in Angular Package Format (APF)

92 lines • 3.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rolldownBundleFile = rolldownBundleFile; const node_path_1 = require("node:path"); const rolldown_1 = require("rolldown"); const rolldown_plugin_dts_1 = require("rolldown-plugin-dts"); const log_1 = __importDefault(require("../utils/log")); const file_loader_plugin_1 = require("./file-loader-plugin"); /** Runs rolldown over the given entry file, writes a bundle file. */ async function rolldownBundleFile(opts) { log_1.default.debug(`rolldown ${opts.entry} to ${opts.dir}`); const dtsMode = opts.entry.endsWith('.d.ts'); let outExtension; let plugins; const jail = (0, node_path_1.dirname)(opts.entry); if (dtsMode) { outExtension = '.d.ts'; plugins = [ (0, file_loader_plugin_1.fileLoaderPlugin)(opts.fileCache, ['.d.ts', '/index.d.ts'], dtsMode), (0, rolldown_plugin_dts_1.dts)({ dtsInput: true, tsconfig: false, generator: 'oxc', sourcemap: opts.sourcemap }), ]; } else { outExtension = '.mjs'; plugins = [(0, file_loader_plugin_1.fileLoaderPlugin)(opts.fileCache, ['.js', '/index.js'], dtsMode)]; } // Create the bundle const bundle = await (0, rolldown_1.rolldown)({ context: 'this', external: (moduleId, parentId) => isExternalDependency(moduleId, parentId, jail), input: opts.entry, plugins, checks: { circularDependency: false, }, onwarn: warning => { switch (warning.code) { case 'CIRCULAR_DEPENDENCY': case 'UNUSED_EXTERNAL_IMPORT': case 'THIS_IS_UNDEFINED': case 'EMPTY_BUNDLE': break; default: log_1.default.warn(warning.message); break; } }, experimental: { attachDebugInfo: 'none', }, resolve: { symlinks: false, }, // Disable treeshaking when generating bundles // see: https://github.com/angular/angular/pull/32069 treeshake: false, }); // Output the bundle to disk const output = await bundle.write({ name: opts.moduleName, format: 'es', dir: opts.dir, hoistTransitiveImports: false, chunkFileNames: `${opts.entryName}-[name]-[hash]${outExtension}`, entryFileNames: opts.entryName + outExtension, banner: '', sourcemap: opts.sourcemap, comments: { legal: true, annotation: !dtsMode, jsdoc: dtsMode, }, }); // Close the bundle to let plugins clean up their external processes or services await bundle.close(); return { files: output.output, }; } function isExternalDependency(moduleId, parentId, jail) { // more information about why we don't check for 'node_modules' path // https://github.com/rollup/rollup-plugin-node-resolve/issues/110#issuecomment-350353632 if (moduleId[0] === '.' || moduleId[0] === '/' || (0, node_path_1.isAbsolute)(moduleId)) { // if it's either 'absolute', marked to embed, starts with a '.' or '/' or is the umd bundle and is tslib return !parentId || !(0, node_path_1.join)(parentId, moduleId).startsWith(jail); } return true; } //# sourceMappingURL=rolldown.js.map