Skip to content

Commit

Permalink
feat(packageparser): enhance parsing of the exports field (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer authored Oct 13, 2024
1 parent 4d72e99 commit 36cc2fa
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 303 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

---

## [0.25.0]
### Added
* [#275]: support for `exports` as a string.

## [0.24.0] – 2024-10-08
### Changed
* [#321]: **BREAKING CHANGE**: updated dependencies:
Expand Down Expand Up @@ -414,6 +418,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#267]: https://github.com/Comandeer/rollup-lib-bundler/issues/267
[#268]: https://github.com/Comandeer/rollup-lib-bundler/issues/268
[#271]: https://github.com/Comandeer/rollup-lib-bundler/issues/271
[#275]: https://github.com/Comandeer/rollup-lib-bundler/issues/275
[#276]: https://github.com/Comandeer/rollup-lib-bundler/issues/276
[#277]: https://github.com/Comandeer/rollup-lib-bundler/issues/277
[#278]: https://github.com/Comandeer/rollup-lib-bundler/issues/278
Expand All @@ -427,6 +432,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#321]: https://github.com/Comandeer/rollup-lib-bundler/issues/321
[#327]: https://github.com/Comandeer/rollup-lib-bundler/issues/327

[0.25.0]: https://github.com/Comandeer/rollup-lib-bundler/compare/v0.24.0...v0.25.0
[0.24.0]: https://github.com/Comandeer/rollup-lib-bundler/compare/v0.23.0...v0.24.0
[0.23.0]: https://github.com/Comandeer/rollup-lib-bundler/compare/v0.22.1...v0.23.0
[0.22.1]: https://github.com/Comandeer/rollup-lib-bundler/compare/v0.22.0...v0.22.1
Expand Down
54 changes: 49 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ It gets `package.json` from the current working directory, parses it and get nee
* `name`, `author`, `version` and `license` to create beautiful banner comment,
* `exports.import` for saving ESM bundle.

Then the bundling happens. The default entry point for Rollup is `src/index.js`. Please note that **dist directory is purged before bundling**! So if anything should be there alongside the bundle, it should be added there _after_ the bundling.
Then the bundling happens. The default entry point for Rollup is `src/index.js`.

> [!WARNING]
> Please note that **dist directory is purged before bundling**! So if anything should be there alongside the bundle, it should be added there _after_ the bundling.
### Assumed file structure

Expand Down Expand Up @@ -65,7 +68,7 @@ Bundler search for source files with the following extensions in the following o
* `.cts`,
* `.cjs`.

### Multiple bundles
## Multiple bundles

By default, `src/index.js` is treated as the only entry point. However, using [subpath exports](https://nodejs.org/api/packages.html#subpath-exports) you can create several bundled chunks/files. Example:

Expand All @@ -87,12 +90,54 @@ In this case two source files will be bundled:
* `src/chunk.js`:
* ESM output: `dist/chunk.mjs`.

Although Node.js supports several different syntaxes for subpath exports, this bundler supports only the form presented on the example above (so each subpath an `import` property for ESM bundle).

Each subpath is translated to appropriate file in `src` directory. Basically, `./` at the beginning is translated to `src/` and the name of the subpath is translated to `<subpath>.<extension>` (e.g. `./chunk``src/chunk.js`). The only exception is the `.` subpath, which is translated to `src/index.js`.

As of version 0.19.0 the bundler also automatically omits bundling bundles inside other bundles. If there were an import of the `src/chunk.js` file inside the `src/index.js` file in the above structure, then the `dist/package.(c|m)js` file would contain an import from `dist/chunk.(c|m)js` file instead of the content of the other bundle.

### Supported `exports` syntax

The bundler supports only the subset of the `exports` syntax allowed by the Node.js:

* **`exports` as a string**:

```json
{
"exports": "./dist/package.mjs"
}
```
* **subpaths as strings**:

```json
{
"exports": {
".": "./dist/package.mjs",
"./subpath": "./dist/chunk.js"
}
}
```
* **`exports` with conditional exports**:

```json
{
"exports": {
"types": "./dist/package.d.mts",
"import": "./dist/package.mjs"
}
}
```
* **subpaths with conditional exports**:

```json
{
"exports": {
".": {
"types": "./dist/package.d.mts",
"import": "./dist/package.mjs"
}
}
}
```

## TypeScript support

Starting from v0.17.0 the bundler is able also to bundle TypeScript projects. There is no configuration needed, just replace the `.js` extension with the `.ts` one! Also ensure that there's a valid `tsconfig.json` file in the root of your project. If you want to provide different configuration for the bundler, place a `tsconfig.rlb.json` file instead.
Expand Down Expand Up @@ -229,7 +274,6 @@ VSC uses TypeScript rules to suggest imports. However, TS uses CJS rules by defa
}
```


## License

See [LICENSE](./LICENSE) file for details.
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
"sinon": "^19.0.2",
"sourcemap-validator": "^2.1.0",
"tempy": "^3.1.0",
"tsx": "^4.19.1",
"type-fest": "^4.26.1"
"tsx": "^4.19.1"
},
"dependencies": {
"@babel/core": "^7.25.7",
Expand Down
Loading

0 comments on commit 36cc2fa

Please sign in to comment.