Comparing version 4.0.1 to 4.1.0
@@ -57,3 +57,3 @@ #!/usr/bin/env node | ||
var version = "4.0.1"; | ||
var version = "4.1.0"; | ||
@@ -60,0 +60,0 @@ const helpMessage = ` |
@@ -196,11 +196,11 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
return { | ||
name: "string", | ||
name: 'string', | ||
transform (code, id) { | ||
if (filter(id)) { | ||
return { | ||
code: `export default ${JSON.stringify(code)}`, | ||
map: undefined | ||
code: `const data = ${JSON.stringify(code)};\nexport default data;`, | ||
map: null | ||
}; | ||
} | ||
return undefined; | ||
return null; | ||
} | ||
@@ -210,2 +210,27 @@ }; | ||
function prependDirectives() { | ||
return { | ||
name: 'prependDirective', | ||
transform: { | ||
order: 'post', | ||
handler (code, id) { | ||
var _moduleInfo_meta; | ||
const moduleInfo = this.getModuleInfo(id); | ||
if (moduleInfo == null ? void 0 : (_moduleInfo_meta = moduleInfo.meta) == null ? void 0 : _moduleInfo_meta.preserveDirectives) { | ||
const firstDirective = moduleInfo.meta.preserveDirectives.directives[0]; | ||
if (firstDirective) { | ||
const directive = firstDirective.value; | ||
const directiveCode = `'${directive}';`; | ||
return directiveCode + '\n' + code; | ||
} | ||
} | ||
return { | ||
code, | ||
map: null | ||
}; | ||
} | ||
} | ||
}; | ||
} | ||
function exit(err) { | ||
@@ -665,2 +690,3 @@ logger.error(err); | ||
preserveDirectives__default.default(), | ||
prependDirectives(), | ||
replace__default.default({ | ||
@@ -733,2 +759,18 @@ values: getBuildEnv(options.env || []), | ||
} | ||
const splitChunks = (id, ctx)=>{ | ||
const moduleInfo = ctx.getModuleInfo(id); | ||
const moduleMeta = moduleInfo == null ? void 0 : moduleInfo.meta; | ||
if (!moduleInfo || !moduleMeta) { | ||
return; | ||
} | ||
const directives = (moduleMeta.preserveDirectives || { | ||
directives: [] | ||
}).directives.map((d)=>d.replace(/^use /, '')).filter((d)=>d !== 'strict'); | ||
const moduleLayer = directives[0]; | ||
if (moduleLayer && !moduleMeta.isEntry) { | ||
const chunkName = path__default.default.basename(id, path__default.default.extname(id)); | ||
return `${chunkName}-${moduleLayer}`; | ||
} | ||
return; | ||
}; | ||
function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { tsCompilerOptions }, dts) { | ||
@@ -744,12 +786,10 @@ const { format } = options; | ||
const dtsFile = file ? file : exportCondition.export['types'] ? path.resolve(cwd, exportCondition.export['types']) : path.resolve(dtsDir, (exportCondition.name === '.' ? 'index' : exportCondition.name) + '.d.ts'); | ||
// If there's dts file, use `output.file` | ||
const dtsPathConfig = dtsFile ? { | ||
file: dtsFile | ||
} : { | ||
dir: dtsDir | ||
const dtsPathConfig = { | ||
dir: dtsFile ? path.dirname(dtsFile) : dtsDir | ||
}; | ||
const outputFile = dtsFile || file; | ||
return { | ||
name: pkg.name || name, | ||
...dts ? dtsPathConfig : { | ||
file: file | ||
dir: path.dirname(outputFile) | ||
}, | ||
@@ -762,3 +802,6 @@ format, | ||
strict: false, | ||
sourcemap: options.sourcemap | ||
sourcemap: options.sourcemap, | ||
manualChunks: splitChunks, | ||
chunkFileNames: '[name]-[hash].js', | ||
entryFileNames: path.basename(outputFile) | ||
}; | ||
@@ -765,0 +808,0 @@ } |
{ | ||
"name": "bunchee", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "zero config bundler for js/ts/jsx libraries", | ||
@@ -11,2 +11,3 @@ "bin": "./dist/bin/cli.js", | ||
"test:update": "TEST_UPDATE_SNAPSHOT=1 pnpm test", | ||
"test:post": "POST_BUILD=1 pnpm jest test/compile.test.ts test/integration.test.ts", | ||
"clean": "rm -rf ./dist", | ||
@@ -63,3 +64,3 @@ "typecheck": "tsc --noEmit", | ||
"rollup-plugin-swc3": "^0.11.0", | ||
"rollup-preserve-directives": "^1.0.1", | ||
"rollup-preserve-directives": "^1.1.0", | ||
"tslib": "^2.6.2" | ||
@@ -66,0 +67,0 @@ }, |
195
README.md
@@ -19,4 +19,6 @@ # bunchee | ||
## Installation | ||
## Quick Start | ||
### Installation | ||
```sh | ||
@@ -26,6 +28,5 @@ npm install --save-dev bunchee | ||
## Usage | ||
### Configuration | ||
Create your library | ||
Create your library entry file and package.json. | ||
```sh | ||
@@ -37,8 +38,3 @@ cd ./my-lib && mkdir src | ||
Configure module exports | ||
[exports sugar in Node.js](https://nodejs.org/api/packages.html#exports-sugar) | ||
You can use the `exports` field to support different conditions and leverage the same functionality as other bundlers, such as webpack. The exports field allows you to define multiple conditions. | ||
Then use use the [exports field in package.json](https://nodejs.org/api/packages.html#exports-sugar) to configure different conditions and leverage the same functionality as other bundlers, such as webpack. The exports field allows you to define multiple conditions. | ||
```json | ||
@@ -56,8 +52,10 @@ { | ||
Using pure ESM package? | ||
If you want to use ESM package, change the `type` field in package.json to `module`, `bunchee` will change the output format to ESM. | ||
```json | ||
{ | ||
"type": "module", | ||
"main": "./dist/index.mjs", | ||
"exports": { | ||
"import": "dist/index.mjs", | ||
"require": "dist/index.cjs" | ||
}, | ||
"scripts": { | ||
@@ -69,3 +67,4 @@ "build": "bunchee" | ||
Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using these package managers. The output format will based on the exports condition and also the file extension. Given an example: | ||
Now just run `npm run build` (or `pnpm build` / `yarn build`) if you're using these package managers, `bunchee` will find the entry files and build them. | ||
The output format will based on the exports condition and also the file extension. Given an example: | ||
@@ -75,65 +74,6 @@ - It's CommonJS for `require` and ESM for `import` based on the exports condition. | ||
## Configuration | ||
## Usage | ||
`bunchee` CLI provides few options to create different bundles or generating types. | ||
### File Conventions | ||
### CLI Options | ||
- Output (`-o <file>`): Specify output filename. | ||
- Format (`-f <format>`): Set output format (default: `'esm'`). | ||
- External (`--external <dep,>`): Specifying extra external dependencies, by default it is the list of `dependencies` and `peerDependencies` from `package.json`. Values are separate by comma. | ||
- Target (`--target <target>`): Set ECMAScript target (default: `'es2015'`). | ||
- Runtime (`--runtime <runtime>`): Set build runtime (default: `'browser'`). | ||
- Environment (`--env <env,>`): Define environment variables. (default: `NODE_ENV`, separate by comma) | ||
- Working Directory (`--cwd <cwd>`): Set current working directory where containing `package.json`. | ||
- Types (`--dts`): Generate TypeScript declaration files along with assets. | ||
- Minify (`-m`): Compress output. | ||
- Watch (`-w`): Watch for source file changes. | ||
### Basic Example | ||
```sh | ||
cd <project-root-dir> | ||
# specifying input, output and format | ||
bunchee ./src/index.js -f cjs -o ./dist/bundle.js | ||
bunchee ./src/index.js -f esm -o ./dist/bundle.esm.js | ||
# build node.js library, or change target to es2019 | ||
bunchee ./src/index.js --runtime node --target es2019 | ||
``` | ||
#### Specifying extra external dependencies | ||
If you want to mark specific dependencies as external and not include them in the bundle, use the `--external` option followed by a comma-separated list of dependency names: | ||
```sh | ||
bunchee --external=dependency1,dependency2,dependency3 | ||
``` | ||
Replace `dependency1`, `dependency2`, and `dependency3` with the names of the dependencies you want to exclude from the bundle. | ||
#### Bundling everything without external dependencies | ||
To bundle your library without external dependencies, use the `--no-external` option: | ||
```sh | ||
bunchee --no-external | ||
``` | ||
This will include all dependencies within your output bundle. | ||
### Environment Variables | ||
To pass environment variables to your bundled code, use the --env option followed by a comma-separated list of environment variable names: | ||
```bash | ||
bunchee --env=ENV1,ENV2,ENV3 | ||
``` | ||
Replace `ENV1`, `ENV2`, and `ENV3` with the names of the environment variables you want to include in your bundled code. These environment variables will be inlined during the bundling process. | ||
### Entry Files Convention | ||
While `exports` field is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command. | ||
@@ -178,3 +118,3 @@ | ||
### Use Exports Conventions | ||
### Multiple Runtime | ||
@@ -195,21 +135,4 @@ 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. | ||
You can use `index.<export-type>.<ext>` to override the input source file for specific export name. Or using `<export-path>/index.<export-type>.<ext>` also works. Such as: | ||
``` | ||
|- src/ | ||
|- index/.ts | ||
|- index.react-server.ts | ||
|- index.edge-light.ts | ||
``` | ||
This will match the export name `"react-server"` and `"edge-light"` then use the corresponding input source file to build the bundle. | ||
#### Package lint | ||
`bunchee` has support for checking the package bundles are matched with package exports configuration. | ||
### 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. | ||
@@ -255,2 +178,83 @@ | ||
### Server Components | ||
`bunchee` supports to build server components and server actions with library directives like `"use client"` or `"use server"`. It will generate the corresponding chunks for client and server that scope the client and server boundaries properly. | ||
Then when the library is integrated to an app such as Next.js, app bundler can transform the client components and server actions correctly and maximum the benefits. | ||
If you're using `"use client"` or `"use server"` in entry file, then it will be preserved on top and the dist file of that entry will become a client component. | ||
If you're using `"use client"` or `"use server"` in a file that used as a dependency for an entry, then that file containing directives be split into a separate chunk and hoist the directives to the top of the chunk. | ||
### CLI | ||
#### CLI Options | ||
`bunchee` CLI provides few options to create different bundles or generating types. | ||
- Output (`-o <file>`): Specify output filename. | ||
- Format (`-f <format>`): Set output format (default: `'esm'`). | ||
- External (`--external <dep,>`): Specifying extra external dependencies, by default it is the list of `dependencies` and `peerDependencies` from `package.json`. Values are separate by comma. | ||
- Target (`--target <target>`): Set ECMAScript target (default: `'es2015'`). | ||
- Runtime (`--runtime <runtime>`): Set build runtime (default: `'browser'`). | ||
- Environment (`--env <env,>`): Define environment variables. (default: `NODE_ENV`, separate by comma) | ||
- Working Directory (`--cwd <cwd>`): Set current working directory where containing `package.json`. | ||
- Types (`--dts`): Generate TypeScript declaration files along with assets. | ||
- Minify (`-m`): Compress output. | ||
- Watch (`-w`): Watch for source file changes. | ||
```sh | ||
cd <project-root-dir> | ||
# specifying input, output and format | ||
bunchee ./src/index.js -f cjs -o ./dist/bundle.js | ||
bunchee ./src/index.js -f esm -o ./dist/bundle.esm.js | ||
# build node.js library, or change target to es2019 | ||
bunchee ./src/index.js --runtime node --target es2019 | ||
``` | ||
#### Specifying extra external dependencies | ||
If you want to mark specific dependencies as external and not include them in the bundle, use the `--external` option followed by a comma-separated list of dependency names: | ||
```sh | ||
bunchee --external=dependency1,dependency2,dependency3 | ||
``` | ||
Replace `dependency1`, `dependency2`, and `dependency3` with the names of the dependencies you want to exclude from the bundle. | ||
#### Bundling everything without external dependencies | ||
To bundle your library without external dependencies, use the `--no-external` option: | ||
```sh | ||
bunchee --no-external | ||
``` | ||
This will include all dependencies within your output bundle. | ||
### Environment Variables | ||
To pass environment variables to your bundled code, use the --env option followed by a comma-separated list of environment variable names: | ||
```bash | ||
bunchee --env=ENV1,ENV2,ENV3 | ||
``` | ||
Replace `ENV1`, `ENV2`, and `ENV3` with the names of the environment variables you want to include in your bundled code. These environment variables will be inlined during the bundling process. | ||
You can use `index.<export-type>.<ext>` to override the input source file for specific export name. Or using `<export-path>/index.<export-type>.<ext>` also works. Such as: | ||
``` | ||
|- src/ | ||
|- index/.ts | ||
|- index.react-server.ts | ||
|- index.edge-light.ts | ||
``` | ||
This will match the export name `"react-server"` and `"edge-light"` then use the corresponding input source file to build the bundle. | ||
### Wildcard Exports | ||
@@ -401,4 +405,9 @@ | ||
#### Package lint | ||
`bunchee` has support for checking the package bundles are matched with package exports configuration. | ||
### License | ||
MIT |
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
63051
1369
404