import path from 'node:path'; import { createLogger, LogLevel } from '@niceties/logger'; import { resolveBuildEntries } from './build-entries.js'; import { collectPackageImportTargets } from './package-imports.js'; /** * @typedef {import('type-fest').JsonObject} JsonObject * @typedef {import('type-fest').JsonValue} JsonValue * @typedef {import('type-fest').PackageJson} PackageJson * @typedef {import('./types.js').BuildConfiguration} BuildConfiguration * @typedef {import('./types.js').PackageProcessingResult} PackageProcessingResult * @typedef {ReturnType} BuildPluginLifecycle */ /** @type {Set} */ const emptySet = new Set(); /** * @param {JsonObject} pkg * @param {BuildConfiguration} configuration * @param {BuildPluginLifecycle} pluginLifecycle * @returns {Promise} */ export async function processPackage(pkg, configuration, pluginLifecycle) { const indexId = 'index'; const { outputs, packageJson, paths } = configuration; const logger = createLogger(); /** @type {string[]} */ let executableOutputs = []; const allowEsm = outputs.formats.includes('es'); const allowCjs = outputs.formats.includes('cjs'); const allowUmd = outputs.formats.includes('umd'); if (typeof pkg !== 'object' || Array.isArray(pkg) || pkg == null) { logger.finish('expecting object on top level of package.json', LogLevel.error); process.exit(-1); } if (typeof pkg.name !== 'string' && outputs.umdEntries.length > 0) { logger.finish('expecting name to be a string in package.json', LogLevel.error); process.exit(-1); } const finalPackageType = allowEsm && !allowCjs && typeof pkg.type !== 'string' ? 'module' : pkg.type; const importTargets = await collectPackageImportTargets(pkg.imports, configuration, finalPackageType); if (!Array.isArray(pkg.files)) { pkg.files = []; } if (!pkg.files.includes(paths.outputDir)) { /** @type {string[]} */ (pkg.files).push(paths.outputDir); } if (typeof pkg.scripts !== 'object' && pkg.scripts !== null) { pkg.scripts = {}; } if (packageJson.pack && !('prepack' in /** @type {Record} */ (pkg.scripts))) { /** @type {Record} */ (pkg.scripts).prepack = 'npx -y pkgprn@^0.8.0'; } /** @type {string[]} */ const entryNames = []; if (packageJson.exports) { if (typeof pkg.exports === 'object' && pkg.exports != null && !Array.isArray(pkg.exports)) { for (const id in pkg.exports) { if (id !== './package.json' && pkg.exports[id] !== null) entryNames.push(exportIdToEntryName(id)); } } if (!entryNames.includes(indexId)) entryNames.unshift(indexId); } else { entryNames.push(indexId); } const entries = await resolveBuildEntries(entryNames, importTargets, configuration, contributions => pluginLifecycle.contributeEntries(contributions, configuration) ); const indexEntry = entries.require(indexId); if (allowEsm && !allowCjs && typeof pkg.type !== 'string') { pkg.type = 'module'; } const exportsFields = new Set(['svelte', pkg.type === 'module' ? 'require' : 'import', pkg.type === 'module' ? 'import' : 'require']); const exportsLastFields = new Set(['default']); if (allowUmd && typeof pkg.umd === 'string') { if (outputs.umdEntries.includes(indexId)) { pkg.umd = /** @type {string} */ (indexEntry.outputPaths.umd); } } if (allowCjs) { pkg.main = /** @type {string} */ (indexEntry.outputPaths.cjs); } if (allowEsm && !allowCjs) { pkg.main = /** @type {string} */ (indexEntry.outputPaths.es); } if (allowCjs && allowEsm && typeof pkg.module !== 'string') { pkg.module = /** @type {string} */ (indexEntry.outputPaths.es); } if (allowUmd && outputs.umdEntries.includes(indexId)) { pkg.unpkg = /** @type {string} */ (indexEntry.outputPaths.umd); } if (packageJson.exports) { if (typeof pkg.exports !== 'object' || pkg.exports == null || Array.isArray(pkg.exports)) { pkg.exports = {}; } const rootExport = /** @type {Record} */ (pkg.exports)['.']; if (rootExport !== null && typeof rootExport !== 'object') { /** @type {Record} */ (pkg.exports)['.'] = {}; } if (/** @type {Record} */ (pkg.exports)['./package.json'] !== null) { /** @type {Record} */ (pkg.exports)['./package.json'] = './package.json'; } if ( rootExport !== null && allowCjs && pkg.main !== /** @type {Record} */ (/** @type {Record} */ (pkg.exports)['.']).require ) { /** @type {Record} */ (/** @type {Record} */ (pkg.exports)['.']).require = /** @type {JsonValue} */ (pkg.main); } if ( rootExport !== null && pkg.module !== /** @type {Record} */ (/** @type {Record} */ (pkg.exports)['.'])?.default ) { /** @type {Record} */ (/** @type {Record} */ (pkg.exports)['.']).default = /** @type {JsonValue} */ (pkg.module); } for (const id in /** @type {object} */ (pkg.exports)) { if (id === './package.json' || /** @type {Record} */ (pkg.exports)[id] === null) continue; const basename = exportIdToEntryName(id); const entry = entries.require(basename); if (typeof (/** @type {Record} */ (pkg.exports)[id]) !== 'object') { /** @type {Record} */ (pkg.exports)[id] = {}; } const cjsFieldName = pkg.type === 'module' ? 'require' : 'default'; const esmFieldName = pkg.type === 'module' ? 'default' : 'import'; const entryExports = /** @type {Record} */ (/** @type {Record} */ (pkg.exports)[id]); if (allowEsm) { entryExports[esmFieldName] = /** @type {string} */ (entry.outputPaths.es); if (typeof entryExports.import === 'string') entryExports.import = /** @type {string} */ (entry.outputPaths.es); } if (allowCjs) { entryExports[cjsFieldName] = /** @type {string} */ (entry.outputPaths.cjs); if (typeof entryExports.require === 'string') entryExports.require = /** @type {string} */ (entry.outputPaths.cjs); } /** @type {Record} */ (pkg.exports)[id] = orderFields(exportsFields, entryExports, exportsLastFields); } } pluginLifecycle.processPackageJson(/** @type {PackageJson} */ (pkg), entries, configuration); if (packageJson.executables.mode === 'explicit') { executableOutputs = [...packageJson.executables.values]; if (executableOutputs.length > 0) { pkg.bin = /** @type {string} */ (executableOutputs[0]); } } else if (packageJson.executables.mode === 'infer' && allowCjs && entries.values.length > 0) { const executableEntries = entries.values.filter(entry => entry.origin !== 'import'); if (typeof pkg.bin === 'string') { if (executableEntries.some(entry => pkg.bin === entry.outputPaths.cjs)) { executableOutputs = [pkg.bin]; } } else if (typeof pkg.bin === 'object' && pkg.bin !== null) { executableOutputs = /** @type {string[]} */ ( Object.values(pkg.bin).filter( value => typeof value === 'string' && executableEntries.some(entry => value === entry.outputPaths.cjs) ) ); } if ( typeof pkg.directories === 'object' && pkg.directories != null && 'bin' in pkg.directories && typeof pkg.directories.bin === 'string' ) { if (path.resolve(pkg.directories.bin) === path.resolve(paths.outputDir)) { executableOutputs.push( ...executableEntries.flatMap(entry => (entry.outputPaths.cjs == null ? [] : [entry.outputPaths.cjs])) ); executableOutputs = Array.from(new Set(executableOutputs)); } } } return { entries, executableOutputs }; } /** * @param {string} id */ function exportIdToEntryName(id) { return id === '.' ? 'index' : id.replace(/^\.\//, '').replaceAll('\\', '/'); } /** * @template {object} T * @param {Set} firstFields * @param {T} exports * @param {Set} [lastFields] * @returns {T} */ function orderFields(firstFields, exports, lastFields = emptySet) { const ordered = /** @type {T} */ ({}); for (const key of firstFields) { if (/** @type {keyof T} */ (key) in exports) { /** @type {Record} */ (ordered)[key] = /** @type {Record} */ (exports)[key]; } } for (const key in exports) { if (!firstFields.has(key) && !lastFields.has(key)) { /** @type {Record} */ (ordered)[key] = /** @type {Record} */ (exports)[key]; } } for (const key of lastFields) { if (/** @type {keyof T} */ (key) in exports) { /** @type {Record} */ (ordered)[key] = /** @type {Record} */ (exports)[key]; } } return ordered; }