evanw / evanw/esbuild

Add entryPoint field to args of onLoad callback

Open
#873 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Adds a entryPoint field in the args of onLoad callback:
```js
{
entryPoint: 'react-dom/server',
path: '/Volumes/private/github/package/node_modules/react-dom/server.browser.js',
namespace: 'file',
pluginData: undefined
}
```

This is useful, here is an example, transform npm module to esm(solve the #864 problem):

#### 1,This is entryPoints:
```js
const entryPoints = [
'react',
'react-dom',
'react-dom/server',
'react-router-dom/server'
]
```

#### 2, Collect the real entryPoints of npm module, it's very fast

The first build is to collect real entryPoints.

```js
const entries = {}; // real entryPoints collection

esbuild.build({
write: false,
entryPoints,
plugins: [{
name: 'collect-module-entry',
setup(build) {
build.onLoad({ filter: /.*/, namespace: 'file' }, async (args) => {

// This is important
if(entryPoints.includes(args.entryPoint)){
entries[args.entryPoint] = args.path;
}

return { contents:"export {};" }
})
}
}],
})
```

The collect result :
```js
{
"react": '/Volumes/private/github/package/node_modules/react/index.js',
"react-dom": '/Volumes/private/github/package/node_modules/react-dom/index.js',
"react-dom/server": '/Volumes/private/github/package/node_modules/react-dom/server.browser.js'
}
```

#### 3, Start the final build, transform npm module to esm
```js
const realEntries = Object.values(entries);

esbuild.build({
...
splitting:true,
format: 'esm',
entryPoints: realEntries,
plugins: [{
name: 'cjs-to-esm',
setup(build) {
build.onLoad({ filter: /.*/, namespace: 'file' }, async (args) => {

if(realEntries.includes(args.entryPoint)){
const keys = Object.keys(require(args.path))
.filter(i=>i!="default")
.join(', ')

return {
contents: `export { ${keys} } from "${args.path}";import m from "${args.path}";export default m;`,
resolveDir: process.cwd()
}
}
})
}
}],
})
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing the onLoad callback API and the entryPoints examples in the issue, then follow how entry points become resolved paths. Verify the callback args expose the requested entryPoint for the shown cases and that existing callback behavior remains unchanged; no source file or test is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
api, build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.