Skip to content

Commit

Permalink
feat: add options requireReturnsDefault and ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Jan 4, 2024
1 parent 8f5e814 commit 7ce8394
Show file tree
Hide file tree
Showing 11 changed files with 660 additions and 395 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ jobs:
test:
runs-on: ubuntu-latest
concurrency:
group: ci-${{ github.ref }}
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-
- uses: pnpm/[email protected]
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: latest
run_install: true
- run: pnpm test
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: |
pnpm install
pnpm build
pnpm test
29 changes: 19 additions & 10 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ name: Node.js Package

on:
push:
tags: "*"
tags: '*'

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.0.1
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: latest
run_install: true
- run: pnpm build
- name: Publish Package
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'
cache: pnpm
- run: |
pnpm install
pnpm build
- run: pnpm publish --provenance --access public --no-git-checks
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
run: |
pnpm config set "//registry.npmjs.org/:_authToken" "${NPM_TOKEN}"
pnpm publish --access public --no-git-checks
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"prettier.enable": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.sortImports": true,
"source.fixAll.eslint": false
"source.sortImports": "explicit",
"source.fixAll.eslint": "never"
},
"prettier.tabWidth": 2,
"prettier.endOfLine": "auto",
Expand Down
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ other bundlers from analyzing the dependencies. For example:

```js
// some commonjs library, like react-dom
var React = require("react");
var React = require('react')

// your esm code
export { render } from "react-dom";
export { render } from 'react-dom'

// after esbuild --bundle
var React = __require("react"); // <- you dislike this
("...");
export { render };
var React = __require('react') // <- you dislike this
// ...
export { render }

// with this plugin
import __import_react from "react"; // <- you want this
var React = __import_react;
("...");
export { render };
import __import_react from 'react' // <- you want this
var React = __import_react
// ...
export { render }
```

This plugin was inspired by [a comment under esbuild#1921][4]
Expand Down Expand Up @@ -54,7 +54,7 @@ require("esbuild").build({
## Options

```js
commonjs({ filter: /\.c?js$/, transform: false });
commonjs({ filter: /\.c?js$/, transform: false })
```

**filter** (default: `/\.c?js$/`)
Expand All @@ -63,15 +63,41 @@ A RegExp passed to [`onLoad()`](https://esbuild.github.io/plugins/#on-load) to
match commonjs modules, it is recommended to set a custom filter to skip files
for better performance.

**requireReturnsDefault** (default: `true`)

```ts
requireReturnsDefault: boolean | ((path: string) => boolean)
```

Controls which style of import statement to use replacing require calls in commonjs modules.

```js
// input
const foo = require('foo')

// output if requireReturnsDefault is true (default behavior)
import foo from 'foo'

// output if requireReturnsDefault is false
import * as foo from 'foo'
```

**ignore**

Do not convert require calls to these modules. Note that this will cause esbuild
to generate `__require()` wrappers and throw errors at runtime.

```ts
ignore: string[] | ((path: string) => boolean)
```

**transform** (default: `false`)

Try to transform commonjs to es modules. This trick is done with [`cjs-module-lexer`](https://github.com/nodejs/cjs-module-lexer)
to match the native (node) behavior as much as possible. Because this
transformation may cause many bugs around the interop between cjs and esm,
it can also accept a function to filter in the "safe to convert" modules by yourself.

Type:

```ts
transform: boolean | ((path: string) => {
behavior?: "node" | "babel", exports?: string[], sideEffects?: boolean
Expand All @@ -81,9 +107,9 @@ transform: boolean | ((path: string) => {
By default, if you toggle `transform` to `true`, it will convert this code:

```js
exports.__esModule = true;
exports.default = {};
exports.foo = 42;
exports.__esModule = true
exports.default = {}
exports.foo = 42
```

To this:
Expand Down Expand Up @@ -120,6 +146,10 @@ exports, which sucks obviously.

Add experimental option `transform` and `transformConfig`.

### 0.2.3

Add options `requireReturnsDefault` and `ignore`.

## License

MIT @ [hyrious](https://github.com/hyrious)
Expand Down
36 changes: 0 additions & 36 deletions build-type.ts

This file was deleted.

14 changes: 8 additions & 6 deletions fixture/react-dom.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
exports.__esModule = true;
exports.default = {};
exports.foo = 42;
exports.__esModule = true
exports.default = {}
exports.foo = 42

if (process.env.NODE_ENV === "production") {
exports.render = require("./react-dom-production").render;
if (process.env.NODE_ENV === 'production') {
exports.render = require('./react-dom-production').render
} else {
exports.render = require("./react-dom-development").render;
exports.render = require('./react-dom-development').render
}

exports.v4 = require('./uuid').v4
1 change: 1 addition & 0 deletions fixture/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v4 = () => 'uuid-v4'
21 changes: 13 additions & 8 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { build } from "esbuild";
import { commonjs } from "./index";
import { build } from 'esbuild'
import { commonjs } from './index'

build({
entryPoints: ["fixture/entry.ts"],
entryPoints: ['fixture/entry.ts'],
bundle: true,
plugins: [commonjs({ transform: true })],
plugins: [
commonjs({
transform: path => path.startsWith('react-dom'),
requireReturnsDefault: a => a !== './uuid',
}),
],
minifySyntax: true,
format: "esm",
external: ["react"],
format: 'esm',
external: ['react'],
define: {
"process.env.NODE_ENV": '"production"',
'process.env.NODE_ENV': '"production"',
},
}).catch(() => process.exit(1));
}).catch(() => process.exit(1))
Loading

0 comments on commit 7ce8394

Please sign in to comment.