bunchee
Advanced tools
Comparing version 3.9.3 to 4.0.0
@@ -12,5 +12,6 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
var replace = require('@rollup/plugin-replace'); | ||
var esmShim = require('@rollup/plugin-esm-shim'); | ||
var prettyBytes = require('pretty-bytes'); | ||
var pluginutils = require('@rollup/pluginutils'); | ||
var swcPreserveDirectivePlugin = require('rollup-swc-preserve-directives'); | ||
var preserveDirectives = require('rollup-preserve-directives'); | ||
var module$1 = require('module'); | ||
@@ -25,4 +26,5 @@ | ||
var replace__default = /*#__PURE__*/_interopDefault(replace); | ||
var esmShim__default = /*#__PURE__*/_interopDefault(esmShim); | ||
var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes); | ||
var swcPreserveDirectivePlugin__default = /*#__PURE__*/_interopDefault(swcPreserveDirectivePlugin); | ||
var preserveDirectives__default = /*#__PURE__*/_interopDefault(preserveDirectives); | ||
@@ -47,2 +49,7 @@ const availableExtensions = [ | ||
const SRC = 'src'; | ||
const dtsExtensions = { | ||
js: '.d.ts', | ||
cjs: '.d.cts', | ||
mjs: '.d.mts' | ||
}; | ||
@@ -186,2 +193,21 @@ const logger = { | ||
function rawContent({ exclude }) { | ||
const filter = pluginutils.createFilter([ | ||
'**/*.data', | ||
'**/*.txt' | ||
], exclude); | ||
return { | ||
name: "string", | ||
transform (code, id) { | ||
if (filter(id)) { | ||
return { | ||
code: `export default ${JSON.stringify(code)}`, | ||
map: undefined | ||
}; | ||
} | ||
return undefined; | ||
} | ||
}; | ||
} | ||
function exit(err) { | ||
@@ -243,3 +269,4 @@ logger.error(err); | ||
// Find convention-based source file for specific export types | ||
if (availableExportConventions.includes(exportType)) { | ||
// $binary represents `pkg.bin` | ||
if (availableExportConventions.includes(exportType) && exportType !== '$binary') { | ||
const filename = await findSourceEntryFile(cwd, exportPath, exportType, ext); | ||
@@ -271,16 +298,14 @@ if (filename) return filename; | ||
return Object.entries(field).every(// Every value is string and key is not start with '.' | ||
// TODO: check key is ExportType | ||
([key, value])=>typeof value === 'string' && !key.startsWith('.')); | ||
} | ||
function constructFullExportCondition(exportCondition, packageType) { | ||
const isCommonjs = packageType === 'commonjs'; | ||
let result; | ||
const isEsmPkg = isESModulePackage(packageType); | ||
let fullExportCond; | ||
if (typeof exportCondition === 'string') { | ||
result = { | ||
[isCommonjs ? 'require' : 'import']: exportCondition | ||
fullExportCond = { | ||
[isEsmPkg ? 'import' : 'require']: exportCondition | ||
}; | ||
} else { | ||
// TODO: valid export condition, warn if it's not valid | ||
const keys = Object.keys(exportCondition); | ||
result = {}; | ||
fullExportCond = {}; | ||
keys.forEach((key)=>{ | ||
@@ -290,7 +315,7 @@ const condition = exportCondition[key]; | ||
if (key in exportCondition && condition) { | ||
result[key] = condition; | ||
fullExportCond[key] = condition; | ||
} | ||
}); | ||
} | ||
return result; | ||
return fullExportCond; | ||
} | ||
@@ -401,3 +426,3 @@ function joinRelativePath(...segments) { | ||
const packageType = pkgType != null ? pkgType : getPackageType(pkg); | ||
const isCjsPackage = packageType === 'commonjs'; | ||
const isEsmPackage = isESModulePackage(packageType); | ||
const exportsConditions = resolvedWildcardExports != null ? resolvedWildcardExports : pkg.exports; | ||
@@ -408,3 +433,3 @@ if (exportsConditions) { | ||
} | ||
if (!isCjsPackage && pkg.main && hasCjsExtension(pkg.main)) { | ||
if (isEsmPackage && pkg.main && hasCjsExtension(pkg.main)) { | ||
exit('Cannot export main field with .cjs extension in ESM package, only .mjs and .js extensions are allowed'); | ||
@@ -414,7 +439,7 @@ } | ||
const defaultMainExport = constructFullExportCondition({ | ||
[isCjsPackage ? 'require' : 'import']: pkg.main, | ||
[isEsmPackage ? 'import' : 'require']: pkg.main, | ||
module: pkg.module, | ||
types: getTypings(pkg) | ||
}, packageType); | ||
if (isCjsPackage && ((_pathsMap_ = pathsMap['.']) == null ? void 0 : _pathsMap_['require'])) { | ||
if (!isEsmPackage && ((_pathsMap_ = pathsMap['.']) == null ? void 0 : _pathsMap_['require'])) { | ||
// pathsMap's exports.require are prioritized. | ||
@@ -448,8 +473,3 @@ defaultMainExport['require'] = pathsMap['.']['require']; | ||
const ext = path.extname(filePath).slice(1); | ||
const dtsExtentions = { | ||
js: '.d.ts', | ||
cjs: '.d.cts', | ||
mjs: '.d.mts' | ||
}; | ||
const typeFile = getDistPath(`${filenameWithoutExtension(filePath) || ''}${dtsExtentions[ext]}`, cwd); | ||
const typeFile = getDistPath(`${filenameWithoutExtension(filePath) || ''}${dtsExtensions[ext]}`, cwd); | ||
if (existed.has(typeFile)) { | ||
@@ -465,3 +485,7 @@ continue; | ||
} | ||
function isESModulePackage(packageType) { | ||
return packageType === 'module'; | ||
} | ||
function constructDefaultExportCondition(value, packageType) { | ||
const isEsmPackage = isESModulePackage(packageType); | ||
let exportCondition; | ||
@@ -471,3 +495,3 @@ if (typeof value === 'string') { | ||
exportCondition = { | ||
[packageType === 'commonjs' ? 'require' : 'import']: value, | ||
[isEsmPackage ? 'import' : 'require']: value, | ||
...types && { | ||
@@ -509,5 +533,3 @@ types | ||
let format = 'esm'; | ||
if (isEsmExportName(key, ext)) { | ||
format = 'esm'; | ||
} else if (isCjsExportName(key, ext)) { | ||
if (isCjsExportName(key, ext)) { | ||
format = 'cjs'; | ||
@@ -526,7 +548,7 @@ } | ||
} | ||
if (dist.length === 0) { | ||
// TODO: Deprecate this warning and behavior in v3 | ||
if (dist.length === 0 && !pkg.bin) { | ||
console.error(`Doesn't fin any exports in ${pkg.name}, using default dist path dist/index.js`); | ||
const defaultFormat = isESModulePackage(pkg.type) ? 'esm' : 'cjs'; | ||
dist.push({ | ||
format: 'esm', | ||
format: defaultFormat, | ||
file: getDistPath('dist/index.js', cwd) | ||
@@ -652,3 +674,6 @@ }); | ||
}), | ||
swcPreserveDirectivePlugin__default.default(), | ||
rawContent({ | ||
exclude: /node_modules/ | ||
}), | ||
preserveDirectives__default.default(), | ||
replace__default.default({ | ||
@@ -679,3 +704,4 @@ values: getBuildEnv(options.env || []), | ||
...swcOptions | ||
}) | ||
}), | ||
esmShim__default.default() | ||
]).filter(isNotNull); | ||
@@ -801,2 +827,40 @@ return { | ||
}); | ||
const binaryExports = pkg.bin; | ||
if (binaryExports) { | ||
// binDistPaths: [ [ 'bin1', './dist/bin1.js'], [ 'bin2', './dist/bin2.js'] ] | ||
const binPairs = typeof binaryExports === 'string' ? [ | ||
[ | ||
'bin', | ||
binaryExports | ||
] | ||
] : Object.keys(binaryExports).map((key)=>[ | ||
path.join('bin', key), | ||
binaryExports[key] | ||
]); | ||
const isESModule = isESModulePackage(pkg.type); | ||
const binExportPaths = binPairs.reduce((acc, [binName, binDistPath])=>{ | ||
const ext = path.extname(binDistPath).slice(1); | ||
const isCjsExt = ext === 'cjs'; | ||
const isEsmExt = ext === 'mjs'; | ||
const exportType = isEsmExt ? 'import' : isCjsExt ? 'require' : isESModule ? 'import' : 'require'; | ||
acc[binName] = { | ||
[exportType]: binDistPath | ||
}; | ||
return acc; | ||
}, {}); | ||
for (const [binName] of binPairs){ | ||
const source = await getSourcePathFromExportPath(cwd, binName, '$binary'); | ||
if (!source) { | ||
logger.warn(`Cannot find source file for ${binName}`); | ||
continue; | ||
} | ||
const binEntryPath = await resolveSourceFile(cwd, source); | ||
const binEntryConfig = buildConfig(binEntryPath, pkg, binExportPaths, bundleConfig, { | ||
source: binEntryPath, | ||
name: binName, | ||
export: binExportPaths[binName] | ||
}, cwd, tsOptions, dts); | ||
configs.push(binEntryConfig); | ||
} | ||
} | ||
return (await Promise.all(configs)).filter(nonNullable); | ||
@@ -966,2 +1030,3 @@ } | ||
; | ||
const hasBin = Boolean(pkg.bin); | ||
const tsConfig = await resolveTsConfig(cwd); | ||
@@ -1012,3 +1077,4 @@ const hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath); | ||
const hasSpecifiedEntryFile = entryPath ? await fileExists(entryPath) && (await fs__default.default.stat(entryPath)).isFile() : false; | ||
if (!hasSpecifiedEntryFile && !isMultiEntries) { | ||
const hasNoEntry = !hasSpecifiedEntryFile && !isMultiEntries && !hasBin; | ||
if (hasNoEntry) { | ||
const err = new Error(`Entry file \`${entryPath}\` is not existed`); | ||
@@ -1015,0 +1081,0 @@ err.name = 'NOT_EXISTED'; |
{ | ||
"name": "bunchee", | ||
"version": "3.9.3", | ||
"version": "4.0.0", | ||
"description": "zero config bundler for js/ts/jsx libraries", | ||
"bin": { | ||
"bunchee": "./dist/cli.js" | ||
}, | ||
"bin": "./dist/bin/cli.js", | ||
"main": "./dist/index.js", | ||
@@ -15,6 +13,4 @@ "types": "./dist/index.d.ts", | ||
"typecheck": "tsc --noEmit", | ||
"prepublishOnly": "pnpm clean && pnpm build && chmod +x ./dist/cli.js && pnpm test", | ||
"build:cli": "tsx ./src/cli.ts ./src/cli.ts --runtime node -f cjs -o ./dist/cli.js", | ||
"build:main": "tsx ./src/cli.ts ./src/index.ts --runtime node -f cjs", | ||
"build": "pnpm build:main && pnpm build:cli", | ||
"prepublishOnly": "pnpm clean && pnpm build && chmod +x ./dist/bin/cli.js && pnpm test", | ||
"build": "tsx ./src/bin/index.ts --runtime node", | ||
"format": "prettier --write .", | ||
@@ -51,18 +47,19 @@ "prepare": "husky install" | ||
"dependencies": { | ||
"@rollup/plugin-commonjs": "^25.0.4", | ||
"@rollup/plugin-json": "^6.0.0", | ||
"@rollup/plugin-node-resolve": "^15.2.1", | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@rollup/plugin-wasm": "^6.1.3", | ||
"@rollup/pluginutils": "^5.0.4", | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-esm-shim": "^0.1.5", | ||
"@rollup/plugin-json": "^6.0.1", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-wasm": "^6.2.2", | ||
"@rollup/pluginutils": "^5.1.0", | ||
"@swc/core": "^1.3.99", | ||
"@swc/helpers": "^0.5.0", | ||
"@swc/helpers": "^0.5.3", | ||
"arg": "^5.0.2", | ||
"pretty-bytes": "^5.6.0", | ||
"publint": "~0.2.2", | ||
"rollup": "^3.28.1", | ||
"rollup-plugin-dts": "^6.0.1", | ||
"rollup-plugin-swc3": "^0.10.3", | ||
"rollup-swc-preserve-directives": "^0.5.0", | ||
"tslib": "^2.5.0" | ||
"publint": "~0.2.6", | ||
"rollup": "^4.6.1", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-swc3": "^0.11.0", | ||
"rollup-preserve-directives": "^1.0.1", | ||
"tslib": "^2.6.2" | ||
}, | ||
@@ -91,4 +88,4 @@ "peerDependencies": { | ||
"react": "^18.2.0", | ||
"tsx": "^3.14.0", | ||
"typescript": "^4.9.5" | ||
"tsx": "^4.6.2", | ||
"typescript": "^5.3.2" | ||
}, | ||
@@ -95,0 +92,0 @@ "lint-staged": { |
@@ -172,3 +172,3 @@ # bunchee | ||
### Special Exports Conventions | ||
### Use Exports Conventions | ||
@@ -204,4 +204,48 @@ For exports condition like `react-native`, `react-server` and `edge-light` as they're special platforms, they could have different exports or different code conditions. In this case bunchee provides an override input source file convention if you want to build them as different code bundle. | ||
### Wildcard Exports (Experimental) | ||
### Executables | ||
To build executable files with the `bin` field in package.json, `bunchee` requires you to create the `bin` directory under `src` directory. The source file matching will be same as the entry files convention. | ||
For example: | ||
```bash | ||
|- src/ | ||
|- bin/ | ||
|- index.ts | ||
``` | ||
This will match the `bin` field in package.json as: | ||
```json | ||
{ | ||
"bin": "./dist/bin.js" | ||
} | ||
``` | ||
For multiple executable files, you can create multiple files under the `bin` directory. | ||
```bash | ||
|- src/ | ||
|- bin/ | ||
|- foo.ts | ||
|- bar.ts | ||
``` | ||
This will match the `bin` field in package.json as: | ||
```json | ||
{ | ||
"bin": { | ||
"foo": "./dist/bin/a.js", | ||
"bar": "./dist/bin/b.js" | ||
} | ||
} | ||
``` | ||
> Note: For multiple `bin` files, the filename should match the key name in the `bin` field. | ||
### Wildcard Exports | ||
Bunchee implements the Node.js feature of using the asterisk `*` as a wildcard to match the exportable entry files. | ||
@@ -263,3 +307,3 @@ | ||
`bunchee`` has basic CSS support for pure CSS file imports. It will be bundled into js bundle and insert the style tag into the document head when the bundle is loaded by browser. | ||
`bunchee` has basic CSS support for pure CSS file imports. It will be bundled into js bundle and insert the style tag into the document head when the bundle is loaded by browser. | ||
@@ -280,2 +324,25 @@ ```css | ||
### Text Files | ||
If you just want to import a file as string content, you can name the extension as `.txt` or `.data` and it will be bundled as string content. | ||
For example: | ||
src/index.ts | ||
```js | ||
import data from './data.txt' | ||
export default data | ||
``` | ||
src/data.txt | ||
```txt | ||
hello world | ||
``` | ||
output | ||
``` | ||
export default "hello world" | ||
``` | ||
### TypeScript | ||
@@ -282,0 +349,0 @@ |
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
60387
1327
395
18
+ Added@rollup/plugin-esm-shim@0.1.7(transitive)
+ Added@rollup/rollup-android-arm-eabi@4.21.0(transitive)
+ Added@rollup/rollup-android-arm64@4.21.0(transitive)
+ Added@rollup/rollup-darwin-arm64@4.21.0(transitive)
+ Added@rollup/rollup-darwin-x64@4.21.0(transitive)
+ Added@rollup/rollup-linux-arm-gnueabihf@4.21.0(transitive)
+ Added@rollup/rollup-linux-arm-musleabihf@4.21.0(transitive)
+ Added@rollup/rollup-linux-arm64-gnu@4.21.0(transitive)
+ Added@rollup/rollup-linux-arm64-musl@4.21.0(transitive)
+ Added@rollup/rollup-linux-powerpc64le-gnu@4.21.0(transitive)
+ Added@rollup/rollup-linux-riscv64-gnu@4.21.0(transitive)
+ Added@rollup/rollup-linux-s390x-gnu@4.21.0(transitive)
+ Added@rollup/rollup-linux-x64-gnu@4.21.0(transitive)
+ Added@rollup/rollup-linux-x64-musl@4.21.0(transitive)
+ Added@rollup/rollup-win32-arm64-msvc@4.21.0(transitive)
+ Added@rollup/rollup-win32-ia32-msvc@4.21.0(transitive)
+ Added@rollup/rollup-win32-x64-msvc@4.21.0(transitive)
+ Addedrollup@4.21.0(transitive)
+ Addedrollup-plugin-swc3@0.11.2(transitive)
+ Addedrollup-preserve-directives@1.1.1(transitive)
- Removed@napi-rs/magic-string@0.3.4(transitive)
- Removed@napi-rs/magic-string-android-arm-eabi@0.3.4(transitive)
- Removed@napi-rs/magic-string-android-arm64@0.3.4(transitive)
- Removed@napi-rs/magic-string-darwin-arm64@0.3.4(transitive)
- Removed@napi-rs/magic-string-darwin-x64@0.3.4(transitive)
- Removed@napi-rs/magic-string-freebsd-x64@0.3.4(transitive)
- Removed@napi-rs/magic-string-linux-arm-gnueabihf@0.3.4(transitive)
- Removed@napi-rs/magic-string-linux-arm64-gnu@0.3.4(transitive)
- Removed@napi-rs/magic-string-linux-arm64-musl@0.3.4(transitive)
- Removed@napi-rs/magic-string-linux-x64-gnu@0.3.4(transitive)
- Removed@napi-rs/magic-string-linux-x64-musl@0.3.4(transitive)
- Removed@napi-rs/magic-string-win32-arm64-msvc@0.3.4(transitive)
- Removed@napi-rs/magic-string-win32-ia32-msvc@0.3.4(transitive)
- Removed@napi-rs/magic-string-win32-x64-msvc@0.3.4(transitive)
- Removedrollup@3.29.4(transitive)
- Removedrollup-plugin-swc3@0.10.4(transitive)
- Removedrollup-swc-preserve-directives@0.5.00.6.0(transitive)
Updated@rollup/plugin-json@^6.0.1
Updated@rollup/plugin-wasm@^6.2.2
Updated@rollup/pluginutils@^5.1.0
Updated@swc/helpers@^0.5.3
Updatedpublint@~0.2.6
Updatedrollup@^4.6.1
Updatedrollup-plugin-dts@^6.1.0
Updatedrollup-plugin-swc3@^0.11.0
Updatedtslib@^2.6.2