Comparing version 4.4.1 to 4.4.2
@@ -28,2 +28,9 @@ #!/usr/bin/env node | ||
]); | ||
const suffixedExportConventions = new Set([ | ||
'react-server', | ||
'react-native', | ||
'edge-light', | ||
'development', | ||
'production' | ||
]); | ||
const SRC = 'src'; | ||
@@ -207,2 +214,5 @@ const DIST = 'dist'; | ||
}; | ||
const joinExportAndCondition = (exportPath, condition)=>{ | ||
return (exportPath === '.' ? '' : exportPath) + '.' + condition; | ||
}; | ||
function findExport(exportPath, exportCondition, paths, packageType, currentPath) { | ||
@@ -220,8 +230,16 @@ // Skip `types` field, it cannot be the entry point | ||
const exportJsBundlePath = getFirstExportPath(fullExportCondition); | ||
// exportPath is exportType, import, require, ... | ||
// merge to currentPath | ||
paths[currentPath] = { | ||
...paths[currentPath], | ||
[exportPath]: exportJsBundlePath | ||
}; | ||
if (suffixedExportConventions.has(exportPath)) { | ||
const specialPath = joinExportAndCondition(currentPath, exportPath); | ||
paths[specialPath] = { | ||
...paths[specialPath], | ||
...exportCondition | ||
}; | ||
} else { | ||
// exportPath is exportType, import, require, ... | ||
// merge to currentPath | ||
paths[currentPath] = { | ||
...paths[currentPath], | ||
[exportPath]: exportJsBundlePath | ||
}; | ||
} | ||
} | ||
@@ -239,2 +257,21 @@ return; | ||
const exportType = subpath; | ||
if (typeof exportCondition[subpath] === 'object') { | ||
const defaultPath = exportCondition[subpath].default; | ||
if (defaultPath) { | ||
const nestedExportCondition = { | ||
[exportType]: defaultPath | ||
}; | ||
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath); | ||
} | ||
// Find special export type, such as import: { development: './dev.js', production: './prod.js' } | ||
const conditionSpecialTypes = Object.keys(exportCondition[exportType]).filter((key)=>suffixedExportConventions.has(key)); | ||
if (conditionSpecialTypes.length > 0) { | ||
for (const conditionSpecialType of conditionSpecialTypes){ | ||
const nestedExportConditionPath = { | ||
[exportType]: exportCondition[exportType][conditionSpecialType] | ||
}; | ||
findExport(conditionSpecialType, nestedExportConditionPath, paths, packageType, currentPath); | ||
} | ||
} | ||
} | ||
const defaultPath = typeof exportCondition[subpath] === 'object' ? exportCondition[subpath].default : exportCondition[subpath]; | ||
@@ -523,3 +560,3 @@ const nestedExportCondition = { | ||
var version = "4.4.1"; | ||
var version = "4.4.2"; | ||
@@ -526,0 +563,0 @@ function relativify(path) { |
@@ -232,4 +232,10 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
// Unlike path.basename, forcedly removing extension | ||
function filePathWithoutExtension(file) { | ||
return file ? file.replace(new RegExp(`${path__default.default.extname(file)}$`), '') : undefined; | ||
function filePathWithoutExtension(filePath) { | ||
if (!filePath) return; | ||
const lastDotIndex = filePath.lastIndexOf('.'); | ||
const lastSlashIndex = filePath.lastIndexOf('/'); | ||
if (lastDotIndex !== -1 && lastDotIndex > lastSlashIndex) { | ||
return filePath.slice(0, filePath.indexOf('.', lastSlashIndex + 1)); | ||
} | ||
return filePath; | ||
} | ||
@@ -371,2 +377,6 @@ const nonNullable = (n)=>Boolean(n); | ||
function relativify(path) { | ||
return path.startsWith('.') ? path : `./${path}`; | ||
} | ||
// Alias entries to import path | ||
@@ -376,3 +386,26 @@ // e.g. | ||
// aliases it as export path, such as <absolute file> -> <pkg>/<export path> | ||
function aliasEntries({ entries }) { | ||
function aliasEntries({ entry, entries, entriesAlias, format, dts }) { | ||
let currentDistPath = ''; | ||
const entryAliasWithoutSelf = { | ||
...entriesAlias, | ||
[entry]: null | ||
}; | ||
const pathToRelativeDistMap = new Map(); | ||
for (const [, exportCondition] of Object.entries(entries)){ | ||
var _Object_entries_find; | ||
const { import: importCond, require: requireCond, default: defaultCond } = exportCondition.export; | ||
const firstCond = (_Object_entries_find = Object.entries(exportCondition.export).find(([key, cond])=>key !== 'types' && cond != null)) == null ? void 0 : _Object_entries_find[1]; | ||
if (dts) { | ||
const fallbackCond = defaultCond || firstCond; | ||
// For cjs, use require() instead of import | ||
const firstDistPath = (format === 'cjs' ? requireCond : importCond) || fallbackCond; | ||
if (firstDistPath) { | ||
if (entry !== exportCondition.source) { | ||
pathToRelativeDistMap.set(exportCondition.source, firstDistPath); | ||
} else { | ||
currentDistPath = firstDistPath; | ||
} | ||
} | ||
} | ||
} | ||
return { | ||
@@ -384,8 +417,22 @@ name: 'alias', | ||
if (resolvedId != null) { | ||
const aliasedId = entries[resolvedId.id]; | ||
if (aliasedId != null) { | ||
return { | ||
id: aliasedId, | ||
external: true | ||
}; | ||
if (dts) { | ||
// For types, generate relative path to the other type files, | ||
// this will be compatible for the node10 ts module resolution. | ||
const aliasedId = pathToRelativeDistMap.get(resolvedId.id); | ||
if (aliasedId != null && aliasedId !== currentDistPath) { | ||
const ext = path__default.default.extname(aliasedId); | ||
const filePathBase = filePathWithoutExtension(path__default.default.relative(path__default.default.dirname(currentDistPath), aliasedId)); | ||
const relativePath = relativify(filePathBase + ext); | ||
return { | ||
id: relativePath, | ||
external: true | ||
}; | ||
} | ||
} else { | ||
const aliasedId = entryAliasWithoutSelf[resolvedId.id]; | ||
if (aliasedId != null) { | ||
return { | ||
id: aliasedId | ||
}; | ||
} | ||
} | ||
@@ -470,2 +517,5 @@ } | ||
}; | ||
const joinExportAndCondition = (exportPath, condition)=>{ | ||
return (exportPath === '.' ? '' : exportPath) + '.' + condition; | ||
}; | ||
function findExport(exportPath, exportCondition, paths, packageType, currentPath) { | ||
@@ -483,8 +533,16 @@ // Skip `types` field, it cannot be the entry point | ||
const exportJsBundlePath = getFirstExportPath(fullExportCondition); | ||
// exportPath is exportType, import, require, ... | ||
// merge to currentPath | ||
paths[currentPath] = { | ||
...paths[currentPath], | ||
[exportPath]: exportJsBundlePath | ||
}; | ||
if (suffixedExportConventions.has(exportPath)) { | ||
const specialPath = joinExportAndCondition(currentPath, exportPath); | ||
paths[specialPath] = { | ||
...paths[specialPath], | ||
...exportCondition | ||
}; | ||
} else { | ||
// exportPath is exportType, import, require, ... | ||
// merge to currentPath | ||
paths[currentPath] = { | ||
...paths[currentPath], | ||
[exportPath]: exportJsBundlePath | ||
}; | ||
} | ||
} | ||
@@ -502,2 +560,21 @@ return; | ||
const exportType = subpath; | ||
if (typeof exportCondition[subpath] === 'object') { | ||
const defaultPath = exportCondition[subpath].default; | ||
if (defaultPath) { | ||
const nestedExportCondition = { | ||
[exportType]: defaultPath | ||
}; | ||
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath); | ||
} | ||
// Find special export type, such as import: { development: './dev.js', production: './prod.js' } | ||
const conditionSpecialTypes = Object.keys(exportCondition[exportType]).filter((key)=>suffixedExportConventions.has(key)); | ||
if (conditionSpecialTypes.length > 0) { | ||
for (const conditionSpecialType of conditionSpecialTypes){ | ||
const nestedExportConditionPath = { | ||
[exportType]: exportCondition[exportType][conditionSpecialType] | ||
}; | ||
findExport(conditionSpecialType, nestedExportConditionPath, paths, packageType, currentPath); | ||
} | ||
} | ||
} | ||
const defaultPath = typeof exportCondition[subpath] === 'object' ? exportCondition[subpath].default : exportCondition[subpath]; | ||
@@ -665,6 +742,8 @@ const nestedExportCondition = { | ||
const isESModule = isESModulePackage(pkg.type); | ||
return !isESModule && [ | ||
const isCjsCondition = [ | ||
'require', | ||
'main' | ||
].includes(exportCondition) && ext !== 'mjs' || ext === 'cjs'; | ||
].includes(exportCondition); | ||
const isNotEsmExportName = !isEsmExportName(exportCondition, ext); | ||
return !isESModule && isNotEsmExportName && (ext !== 'mjs' || isCjsCondition) || ext === 'cjs'; | ||
} | ||
@@ -720,3 +799,3 @@ function getExportsDistFilesOfCondition(pkg, parsedExportCondition, cwd) { | ||
// return { 'process.env.<key>': '<value>' } | ||
function getBuildEnv(envs, exportConditions) { | ||
function getBuildEnv(envs, parsedExportCondition) { | ||
if (!envs.includes('NODE_ENV')) { | ||
@@ -732,4 +811,6 @@ envs.push('NODE_ENV'); | ||
}, {}); | ||
// handle .development, .production | ||
const condName = parsedExportCondition.name.startsWith('.') ? parsedExportCondition.name.slice(1) : parsedExportCondition.name; | ||
const exportConditionNames = new Set(Object.keys(parsedExportCondition.export).concat(condName)); | ||
// For development and production convention, we override the NODE_ENV value | ||
const exportConditionNames = new Set(Object.keys(exportConditions)); | ||
if (exportConditionNames.has('development')) { | ||
@@ -757,6 +838,7 @@ envVars['process.env.NODE_ENV'] = JSON.stringify('development'); | ||
} | ||
async function buildInputConfig(entry, options, buildContext, exportCondition, dts) { | ||
async function buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts) { | ||
var _bundleConfig_file; | ||
const { entries, pkg, cwd, tsOptions: { tsConfigPath, tsCompilerOptions }, pluginContext } = buildContext; | ||
const hasNoExternal = options.external === null; | ||
var _options_external; | ||
const hasNoExternal = bundleConfig.external === null; | ||
var _bundleConfig_external; | ||
const externals = hasNoExternal ? [] : [ | ||
@@ -766,3 +848,3 @@ pkg.peerDependencies, | ||
pkg.peerDependenciesMeta | ||
].filter((n)=>Boolean(n)).map((o)=>Object.keys(o)).reduce((a, b)=>a.concat(b), []).concat((_options_external = options.external) != null ? _options_external : []); | ||
].filter((n)=>Boolean(n)).map((o)=>Object.keys(o)).reduce((a, b)=>a.concat(b), []).concat((_bundleConfig_external = bundleConfig.external) != null ? _bundleConfig_external : []); | ||
for (const [exportImportPath, exportCondition] of Object.entries(entries)){ | ||
@@ -775,5 +857,5 @@ const entryFilePath = exportCondition.source; | ||
} | ||
const envValues = getBuildEnv(options.env || [], exportCondition.export); | ||
const envValues = getBuildEnv(bundleConfig.env || [], exportCondition); | ||
const { useTypeScript } = buildContext; | ||
const { runtime, target: jscTarget, minify: shouldMinify } = options; | ||
const { runtime, target: jscTarget, minify: shouldMinify } = bundleConfig; | ||
const hasSpecifiedTsTarget = Boolean(tsCompilerOptions.target && tsConfigPath); | ||
@@ -796,7 +878,7 @@ const swcParserConfig = { | ||
...swcMinifyOptions, | ||
sourceMap: options.sourcemap | ||
sourceMap: bundleConfig.sourcemap | ||
} | ||
} | ||
}, | ||
sourceMaps: options.sourcemap, | ||
sourceMaps: bundleConfig.sourcemap, | ||
inlineSourcesContent: false, | ||
@@ -807,10 +889,11 @@ isModule: true | ||
// common plugins for both dts and ts assets that need to be processed | ||
const aliasFormat = dts ? ((_bundleConfig_file = bundleConfig.file) == null ? void 0 : _bundleConfig_file.endsWith('.d.cts')) ? 'cjs' : 'esm' : bundleConfig.format; | ||
const commonPlugins = [ | ||
sizePlugin, | ||
aliasEntries({ | ||
entries: { | ||
...pluginContext.entriesAlias, | ||
// Do not alias current alias of package | ||
[entry]: null | ||
} | ||
entry, | ||
entries, | ||
entriesAlias: pluginContext.entriesAlias, | ||
format: aliasFormat, | ||
dts | ||
}) | ||
@@ -884,3 +967,3 @@ ]; | ||
commonjs__default.default({ | ||
exclude: options.external || null | ||
exclude: bundleConfig.external || null | ||
}), | ||
@@ -995,11 +1078,11 @@ json__default.default() | ||
} | ||
function buildOutputConfigs(options, exportCondition, buildContext, dts) { | ||
const { format } = options; | ||
async function buildOutputConfigs(entry, bundleConfig, exportCondition, buildContext, dts) { | ||
const { format } = bundleConfig; | ||
const { entries, pkg, exportPaths, cwd, tsOptions: { tsCompilerOptions }, pluginContext } = buildContext; | ||
// Add esm mark and interop helper if esm export is detected | ||
const useEsModuleMark = hasEsmExport(exportPaths, tsCompilerOptions); | ||
const absoluteOutputFile = path.resolve(cwd, options.file); | ||
const absoluteOutputFile = path.resolve(cwd, bundleConfig.file); | ||
const name = filePathWithoutExtension(absoluteOutputFile); | ||
var _exportCondition_export_types; | ||
const dtsFile = path.resolve(cwd, dts ? options.file : (_exportCondition_export_types = exportCondition.export.types) != null ? _exportCondition_export_types : getExportFileTypePath(options.file)); | ||
const dtsFile = path.resolve(cwd, dts ? bundleConfig.file : (_exportCondition_export_types = exportCondition.export.types) != null ? _exportCondition_export_types : getExportFileTypePath(bundleConfig.file)); | ||
const typesDir = path.dirname(dtsFile); | ||
@@ -1009,3 +1092,4 @@ const jsDir = path.dirname(absoluteOutputFile); | ||
const entryFiles = new Set(Object.values(entries).map((entry)=>entry.source)); | ||
return { | ||
const inputOptions = await buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts); | ||
const outputOptions = { | ||
name: pkg.name || name, | ||
@@ -1019,3 +1103,3 @@ dir: dts ? typesDir : jsDir, | ||
strict: false, | ||
sourcemap: options.sourcemap, | ||
sourcemap: bundleConfig.sourcemap, | ||
manualChunks: createSplitChunks(pluginContext.moduleDirectiveLayerMap, entryFiles), | ||
@@ -1028,2 +1112,6 @@ chunkFileNames: '[name]-[hash].js', | ||
}; | ||
return { | ||
input: inputOptions, | ||
output: outputOptions | ||
}; | ||
} | ||
@@ -1034,10 +1122,11 @@ async function buildEntryConfig(bundleConfig, pluginContext, dts) { | ||
for (const exportCondition of Object.values(entries)){ | ||
const rollupConfig = buildConfig(bundleConfig, exportCondition, pluginContext, dts); | ||
configs.push(rollupConfig); | ||
const rollupConfigs = await buildConfig(bundleConfig, exportCondition, pluginContext, dts); | ||
configs.push(...rollupConfigs); | ||
} | ||
return await Promise.all(configs); | ||
return configs; | ||
} | ||
async function collectEntry(// export type, e.g. react-server, edge-light those special cases required suffix | ||
exportType, options) { | ||
const { cwd, pkg, entries, entryPath, exportCondRef, entryExport } = options; | ||
const { cwd, pkg, entries, entryPath, exportCondRef, entryExport: originEntryExport } = options; | ||
let entryExport = originEntryExport; | ||
let exportCondForType = { | ||
@@ -1051,4 +1140,9 @@ ...exportCondRef | ||
}; | ||
// Basic export type, pass down the exportPaths with erasing the special ones | ||
} else if (exportType[0] === '.' && suffixedExportConventions.has(exportType.slice(1))) { | ||
// e.g. .development, .production that has both esm and cjs export | ||
exportCondForType = exportCondRef; | ||
exportType = exportType.slice(1); | ||
entryExport = entryExport.replace(exportType, ''); | ||
} else { | ||
// Basic export type, pass down the exportPaths with erasing the special ones | ||
for (const exportType of suffixedExportConventions){ | ||
@@ -1069,3 +1163,3 @@ delete exportCondForType[exportType]; | ||
source, | ||
name: entryExport, | ||
name: originEntryExport, | ||
export: exportCondForType | ||
@@ -1132,2 +1226,4 @@ }; | ||
await collectEntry(exportCondType, collectEntryOptions); | ||
} else if (entryExport === '.' + exportCondType) { | ||
await collectEntry(entryExport, collectEntryOptions); | ||
} | ||
@@ -1142,10 +1238,4 @@ } | ||
const { file } = bundleConfig; | ||
const { pkg, cwd, tsOptions } = pluginContext; | ||
const useTypescript = Boolean(tsOptions.tsConfigPath); | ||
const options = { | ||
...bundleConfig, | ||
useTypescript | ||
}; | ||
const { pkg, cwd } = pluginContext; | ||
const entry = exportCondition.source; | ||
const inputOptions = await buildInputConfig(entry, options, pluginContext, exportCondition, dts); | ||
const outputExports = getExportsDistFilesOfCondition(pkg, exportCondition, cwd); | ||
@@ -1194,8 +1284,8 @@ // If there's nothing found, give a default output | ||
resolvedFile: typeFile, | ||
format: 'es' | ||
format: 'esm' | ||
}; | ||
}); | ||
} | ||
const outputConfigs = bundleOptions.map((bundleOption)=>{ | ||
return buildOutputConfigs({ | ||
const outputConfigs = bundleOptions.map(async (bundleOption)=>{ | ||
return await buildOutputConfigs(entry, { | ||
...bundleConfig, | ||
@@ -1206,13 +1296,5 @@ file: bundleOption.resolvedFile, | ||
}); | ||
return { | ||
input: inputOptions, | ||
output: outputConfigs, | ||
exportName: exportCondition.name || '.' | ||
}; | ||
return Promise.all(outputConfigs); | ||
} | ||
function relativify(path) { | ||
return path.startsWith('.') ? path : `./${path}`; | ||
} | ||
// Example: @foo/bar -> bar | ||
@@ -1407,3 +1489,3 @@ const removeScope = (exportPath)=>exportPath.replace(/^@[^/]+\//, ''); | ||
const cwd = path.resolve(process.cwd(), _cwd || ''); | ||
assignDefault(options, 'format', 'es'); | ||
assignDefault(options, 'format', 'esm'); | ||
assignDefault(options, 'minify', false); | ||
@@ -1549,11 +1631,7 @@ assignDefault(options, 'target', 'es2015'); | ||
async function removeOutputDir(output) { | ||
const dirs = new Set(output.map(({ dir })=>dir)); | ||
for (const dir of dirs){ | ||
if (dir) await removeDir(dir); | ||
} | ||
if (output.dir) await removeDir(output.dir); | ||
} | ||
function runBundle({ input, output }) { | ||
return rollup.rollup(input).then((bundle)=>{ | ||
const writeJobs = output.map((options)=>bundle.write(options)); | ||
return Promise.all(writeJobs); | ||
return bundle.write(output); | ||
}, catchErrorHandler); | ||
@@ -1560,0 +1638,0 @@ } |
{ | ||
"name": "bunchee", | ||
"version": "4.4.1", | ||
"version": "4.4.2", | ||
"description": "zero config bundler for js/ts/jsx libraries", | ||
@@ -5,0 +5,0 @@ "bin": "./dist/bin/cli.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
116686
2535