Comparing version
@@ -165,3 +165,4 @@ #!/usr/bin/env node | ||
} | ||
const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.default.extname(filename).slice(1)); | ||
// 'index.server.js' -> 'index' | ||
const getFileBasename = (str)=>str.split('.')[0]; | ||
const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs'; | ||
@@ -187,2 +188,5 @@ const getMainFieldExportType = (pkg)=>{ | ||
} | ||
function isBinExportPath(exportPath) { | ||
return exportPath === BINARY_TAG || exportPath.startsWith(BINARY_TAG + '/'); | ||
} | ||
@@ -194,4 +198,5 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exportToDist) { | ||
const composedTypes = new Set(exportTypes); | ||
composedTypes.add(exportKey.startsWith('.') ? 'default' : exportKey); | ||
const exportInfo = exportToDist.get(currentPath); | ||
const exportType = exportKey.startsWith('.') ? 'default' : exportKey; | ||
composedTypes.add(exportType); | ||
const exportInfo = exportToDist.get(mapExportFullPath(currentPath)); | ||
const exportCondition = Array.from(composedTypes).join('.'); | ||
@@ -203,5 +208,5 @@ if (!exportInfo) { | ||
]; | ||
exportToDist.set(currentPath, [ | ||
addToExportDistMap(exportToDist, currentPath, [ | ||
outputConditionPair | ||
]); | ||
], runtimeExportConventions.has(exportType) ? exportType : undefined); | ||
} else { | ||
@@ -232,2 +237,13 @@ exportInfo.push([ | ||
} | ||
const mapExportFullPath = (exportPath)=>exportPath === '.' ? './index' : exportPath; | ||
function addToExportDistMap(exportToDist, exportPath, outputConditionPairs, specialExportType) { | ||
const fullPath = mapExportFullPath(exportPath); | ||
// + (specialExportType ? '.' + specialExportType : '') | ||
const existingExportInfo = exportToDist.get(fullPath); | ||
if (!existingExportInfo) { | ||
exportToDist.set(fullPath, outputConditionPairs); | ||
} else { | ||
existingExportInfo.push(...outputConditionPairs); | ||
} | ||
} | ||
/** | ||
@@ -237,2 +253,7 @@ * parseExports - parse package.exports field and other fields like main,module to a map | ||
* map from export path to output path and export conditions | ||
* | ||
* exportToDist: { | ||
* './index': { development: ..., default: ... } | ||
* './index.react-server': { development: ..., default: ... } | ||
* } | ||
*/ function parseExports(pkg) { | ||
@@ -252,3 +273,3 @@ var _pkg_exports; | ||
]; | ||
exportToDist.set(currentPath, [ | ||
addToExportDistMap(exportToDist, currentPath, [ | ||
outputConditionPair | ||
@@ -275,3 +296,3 @@ ]); | ||
]; | ||
exportToDist.set(BINARY_TAG, [ | ||
addToExportDistMap(exportToDist, BINARY_TAG, [ | ||
outputConditionPair | ||
@@ -288,3 +309,3 @@ ]); | ||
]; | ||
exportToDist.set(exportPath, [ | ||
addToExportDistMap(exportToDist, exportPath, [ | ||
outputConditionPair | ||
@@ -299,5 +320,3 @@ ]); | ||
const typesEntryPath = pkg.types; | ||
const existingExportInfo = exportToDist.get('.'); | ||
exportToDist.set('.', [ | ||
...existingExportInfo || [], | ||
addToExportDistMap(exportToDist, './index', [ | ||
Boolean(mainExportPath) && [ | ||
@@ -468,3 +487,3 @@ mainExportPath, | ||
var version = "5.0.0"; | ||
var version = "5.0.1"; | ||
@@ -484,3 +503,3 @@ function relativify(path) { | ||
// index.development.ts -> ./index.development | ||
function sourceFilenameToExportPath(filename) { | ||
function sourceFilenameToExportFullPath(filename) { | ||
const baseName = baseNameWithoutExtension(filename); | ||
@@ -490,3 +509,3 @@ let exportPath = baseName; | ||
} | ||
// ./index -> import|require|default | ||
// ./index -> default | ||
// ./index.development -> development | ||
@@ -497,5 +516,5 @@ // ./index.react-server -> react-server | ||
const exportTypes = exportPath.split('.').slice(2); | ||
return getExportTypeFromExportTypes(exportTypes); | ||
return getExportTypeFromExportTypesArray(exportTypes); | ||
} | ||
function getSpecialExportTypeFromExportPath(composedExportType) { | ||
function getSpecialExportTypeFromComposedExportPath(composedExportType) { | ||
const exportTypes = composedExportType.split('.'); | ||
@@ -509,3 +528,3 @@ for (const exportType of exportTypes){ | ||
} | ||
function getExportTypeFromExportTypes(types) { | ||
function getExportTypeFromExportTypesArray(types) { | ||
let exportType = 'default'; | ||
@@ -542,2 +561,91 @@ new Set(types).forEach((value)=>{ | ||
} | ||
async function collectSourceEntriesByExportPath(sourceFolderPath, originalSubpath, bins, exportsEntries) { | ||
const isBinaryPath = isBinExportPath(originalSubpath); | ||
const subpath = originalSubpath.replace(BINARY_TAG, 'bin'); | ||
const absoluteDirPath = path__default.default.join(sourceFolderPath, subpath); | ||
const isDirectory = fs__default.default.existsSync(absoluteDirPath) ? (await fsp__default.default.stat(absoluteDirPath)).isDirectory() : false; | ||
if (isDirectory) { | ||
if (isBinaryPath) { | ||
const binDirentList = await fsp__default.default.readdir(absoluteDirPath, { | ||
withFileTypes: true | ||
}); | ||
for (const binDirent of binDirentList){ | ||
if (binDirent.isFile()) { | ||
const binFileAbsolutePath = path__default.default.join(binDirent.path, binDirent.name); | ||
if (fs__default.default.existsSync(binFileAbsolutePath)) { | ||
bins.set(normalizeExportPath(originalSubpath), binFileAbsolutePath); | ||
} | ||
} | ||
} | ||
} else { | ||
// Search folder/index.<ext> convention entries | ||
for (const extension of availableExtensions){ | ||
const indexAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${extension}`); | ||
// Search folder/index.<special type>.<ext> convention entries | ||
for (const specialExportType of runtimeExportConventions){ | ||
const indexSpecialAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${specialExportType}.${extension}`); | ||
if (fs__default.default.existsSync(indexSpecialAbsoluteFile)) { | ||
// Add special export path | ||
// { ./<export path>.<special cond>: { <special cond>: 'index.<special cond>.<ext>' } } | ||
const exportPath = relativify(subpath); | ||
const specialExportPath = exportPath + '.' + specialExportType; | ||
const sourceFilesMap = exportsEntries.get(specialExportPath) || {}; | ||
sourceFilesMap[specialExportType] = indexSpecialAbsoluteFile; | ||
exportsEntries.set(specialExportPath, sourceFilesMap); | ||
} | ||
} | ||
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) { | ||
const exportPath = relativify(subpath); | ||
const sourceFilesMap = exportsEntries.get(exportPath) || {}; | ||
const exportType = getExportTypeFromExportPath(exportPath); | ||
sourceFilesMap[exportType] = indexAbsoluteFile; | ||
exportsEntries.set(exportPath, sourceFilesMap); | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
// subpath could be a file | ||
const dirName = path.dirname(subpath); | ||
const baseName = path.basename(subpath); | ||
// Read current file's directory | ||
const dirPath = path__default.default.join(sourceFolderPath, dirName); | ||
if (!fs__default.default.existsSync(dirPath)) { | ||
return; | ||
} | ||
const dirents = await fsp__default.default.readdir(dirPath, { | ||
withFileTypes: true | ||
}); | ||
for (const dirent of dirents){ | ||
// index.development.js -> index.development | ||
const direntBaseName = baseNameWithoutExtension(dirent.name); | ||
const ext = path.extname(dirent.name).slice(1); | ||
if (!dirent.isFile() || direntBaseName !== baseName || !availableExtensions.has(ext)) { | ||
continue; | ||
} | ||
if (isTestFile(dirent.name)) { | ||
continue; | ||
} | ||
const sourceFileAbsolutePath = path__default.default.join(dirent.path, dirent.name); | ||
if (isBinaryPath) { | ||
bins.set(originalSubpath, sourceFileAbsolutePath); | ||
} else { | ||
let sourceFilesMap = exportsEntries.get(originalSubpath) || {}; | ||
const exportType = getExportTypeFromExportPath(originalSubpath); | ||
sourceFilesMap[exportType] = sourceFileAbsolutePath; | ||
if (specialExportConventions.has(exportType)) { | ||
// e.g. ./foo/index.react-server -> ./foo/index | ||
const fallbackExportPath = sourceFilenameToExportFullPath(originalSubpath); | ||
const fallbackSourceFilesMap = exportsEntries.get(fallbackExportPath) || {}; | ||
sourceFilesMap = { | ||
...fallbackSourceFilesMap, | ||
...sourceFilesMap | ||
}; | ||
} | ||
exportsEntries.set(originalSubpath, sourceFilesMap); | ||
} | ||
} | ||
} | ||
} | ||
// For `prepare` | ||
async function collectSourceEntries(sourceFolderPath) { | ||
@@ -555,61 +663,23 @@ const bins = new Map(); | ||
}); | ||
// Collect source files for `exports` field | ||
for (const dirent of entryFileDirentList){ | ||
if (dirent.isDirectory()) { | ||
if (dirent.name === 'bin') { | ||
const binDirentList = await fsp__default.default.readdir(path__default.default.join(sourceFolderPath, dirent.name), { | ||
withFileTypes: true | ||
}); | ||
for (const binDirent of binDirentList){ | ||
if (binDirent.isFile()) { | ||
const binFileAbsolutePath = path__default.default.join(sourceFolderPath, dirent.name, binDirent.name); | ||
const binExportPath = sourceFilenameToExportPath(binDirent.name); | ||
if (fs__default.default.existsSync(binFileAbsolutePath)) { | ||
bins.set(path.posix.join(BINARY_TAG, binExportPath), binFileAbsolutePath); | ||
} | ||
} | ||
} | ||
} else { | ||
// Search folder/index.<ext> convention entries | ||
for (const extension of availableExtensions){ | ||
const indexAbsoluteFile = path__default.default.join(dirent.path, dirent.name, `index.${extension}`); | ||
// Search folder/index.<special type>.<ext> convention entries | ||
for (const specialExportType of runtimeExportConventions){ | ||
const indexSpecialAbsoluteFile = path__default.default.join(dirent.path, dirent.name, `index.${specialExportType}.${extension}`); | ||
if (fs__default.default.existsSync(indexSpecialAbsoluteFile)) { | ||
// Add special export path | ||
// { ./<export path>.<special cond>: { <special cond>: 'index.<special cond>.<ext>' } } | ||
const exportPath = sourceFilenameToExportPath(dirent.name); | ||
const specialExportPath = exportPath + '.' + specialExportType; | ||
const sourceFilesMap = exportsEntries.get(specialExportPath) || {}; | ||
sourceFilesMap[specialExportType] = indexSpecialAbsoluteFile; | ||
exportsEntries.set(specialExportPath, sourceFilesMap); | ||
} | ||
} | ||
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) { | ||
const exportPath = sourceFilenameToExportPath(dirent.name); | ||
const sourceFilesMap = exportsEntries.get(exportPath) || {}; | ||
const exportType = getExportTypeFromExportPath(exportPath); | ||
sourceFilesMap[exportType] = indexAbsoluteFile; | ||
exportsEntries.set(exportPath, sourceFilesMap); | ||
break; | ||
} | ||
} | ||
if (getFileBasename(dirent.name) === 'bin') { | ||
continue; | ||
} | ||
const exportPath = sourceFilenameToExportFullPath(dirent.name); | ||
await collectSourceEntriesByExportPath(sourceFolderPath, exportPath, bins, exportsEntries); | ||
} | ||
// Collect source files for `bin` field | ||
const binDirent = entryFileDirentList.find((dirent)=>getFileBasename(dirent.name) === 'bin'); | ||
if (binDirent) { | ||
if (binDirent.isDirectory()) { | ||
const binDirentList = await fsp__default.default.readdir(path__default.default.join(binDirent.path, binDirent.name), { | ||
withFileTypes: true | ||
}); | ||
for (const binDirent of binDirentList){ | ||
const binExportPath = path.posix.join(BINARY_TAG, getFileBasename(binDirent.name)); | ||
await collectSourceEntriesByExportPath(sourceFolderPath, binExportPath, bins, exportsEntries); | ||
} | ||
} else if (dirent.isFile()) { | ||
const isAvailableExtension = availableExtensions.has(path__default.default.extname(dirent.name).slice(1)); | ||
if (isAvailableExtension) { | ||
const exportPath = sourceFilenameToExportPath(dirent.name); | ||
const isBinFile = exportPath === './bin'; | ||
const fullPath = path__default.default.join(sourceFolderPath, dirent.name); | ||
if (isBinFile) { | ||
bins.set(BINARY_TAG, fullPath); | ||
} else { | ||
if (hasAvailableExtension(dirent.name) && !isTestFile(dirent.name)) { | ||
const sourceFilesMap = exportsEntries.get(exportPath) || {}; | ||
const exportType = getExportTypeFromExportPath(exportPath); | ||
sourceFilesMap[exportType] = fullPath; | ||
exportsEntries.set(exportPath, sourceFilesMap); | ||
} | ||
} | ||
} | ||
} else { | ||
await collectSourceEntriesByExportPath(sourceFolderPath, BINARY_TAG, bins, exportsEntries); | ||
} | ||
@@ -666,3 +736,3 @@ } | ||
let specialCondition; | ||
const specialConditionName = getSpecialExportTypeFromExportPath(exportName); | ||
const specialConditionName = getSpecialExportTypeFromComposedExportPath(exportName); | ||
const normalizedExportPath = normalizeExportPath(exportName); | ||
@@ -762,6 +832,6 @@ if (specialConditionName !== 'default') { | ||
for (const sourceFile of Object.values(sourceFilesMap)){ | ||
const [key, value] = createExportConditionPair(exportName, sourceFile, pkgJson.type); | ||
pkgExports[key] = { | ||
...value, | ||
...pkgExports[key] | ||
const [normalizedExportPath, conditions] = createExportConditionPair(exportName, sourceFile, pkgJson.type); | ||
pkgExports[normalizedExportPath] = { | ||
...conditions, | ||
...pkgExports[normalizedExportPath] | ||
}; | ||
@@ -768,0 +838,0 @@ } |
{ | ||
"name": "bunchee", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "zero config bundler for js/ts/jsx libraries", | ||
@@ -5,0 +5,0 @@ "bin": "./dist/bin/cli.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
206701
4.6%4689
4.69%