UNPKG

ng-packagr

Version:

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

58 lines • 3.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generatePackageExports = generatePackageExports; exports.generateWatchVersion = generateWatchVersion; const node_path_1 = require("node:path"); const log_1 = __importDefault(require("./log")); const path_1 = require("./path"); /** * Generates the `package.json` package exports following APF v13. * This is supposed to match with: https://github.com/angular/angular/blob/e0667efa6eada64d1fb8b143840689090fc82e52/packages/bazel/src/ng_package/packager.ts#L415. */ function generatePackageExports({ destinationPath, packageJson }, entryPoints) { const exports = packageJson.exports ? JSON.parse(JSON.stringify(packageJson.exports)) : {}; const insertMappingOrError = (subpath, mapping) => { exports[subpath] ??= {}; if (typeof exports[subpath] === 'string') { log_1.default.warn(`Found an existing subpath export for "${subpath}". The export would be overridden by ng-packagr. Please unset it.`); exports[subpath] = {}; } const subpathExport = exports[subpath]; // Go through all conditions that should be inserted. If the condition is already // manually set of the subpath export, we throw an error. In general, we allow for // additional conditions to be set. These will always precede the generated ones. for (const conditionName of Object.keys(mapping)) { if (subpathExport[conditionName] !== undefined) { log_1.default.warn(`Found a conflicting export condition for "${subpath}". The "${conditionName}" ` + `condition would be overridden by ng-packagr. Please unset it.`); // Must delete existing subpath export to ensure that order matches mapping delete subpathExport[conditionName]; } // **Note**: The order of the conditions is preserved even though we are setting // the conditions once at a time (the latest assignment will be at the end). subpathExport[conditionName] = mapping[conditionName]; } }; const relativeUnixFromDestPath = (filePath) => './' + (0, path_1.ensureUnixPath)((0, node_path_1.relative)(destinationPath, filePath)); insertMappingOrError('./package.json', { default: './package.json' }); const entryPointsSorted = entryPoints.sort((a, b) => a.url.localeCompare(b.url)); for (const entryPoint of entryPointsSorted) { const { destinationFiles, isSecondaryEntryPoint } = entryPoint.data.entryPoint; const subpath = isSecondaryEntryPoint ? `./${destinationFiles.directory}` : '.'; insertMappingOrError(subpath, { types: relativeUnixFromDestPath(destinationFiles.declarationsBundled), default: relativeUnixFromDestPath(destinationFiles.fesm2022), }); } return exports; } /** * Generates a new version for the package `package.json` when runing in watch mode. */ function generateWatchVersion() { return `0.0.0-watch+${Date.now()}`; } //# sourceMappingURL=package-json.js.map