Skip to content

Commit

Permalink
fix #3485: map subpath imports to node built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 19, 2023
1 parent 07e527d commit 83e8c7f
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 89 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@
}
```

* Allow package subpath imports to map to node built-ins ([#3485](https://github.com/evanw/esbuild/issues/3485))

You are now able to use a [subpath import](https://nodejs.org/api/packages.html#subpath-imports) in your package to resolve to a node built-in module. For example, with a `package.json` file like this:

```json
{
"type": "module",
"imports": {
"#stream": {
"node": "stream",
"default": "./stub.js"
}
}
}
```

You can now import from node's `stream` module like this:
```js
import * as stream from '#stream';
console.log(Object.keys(stream));
```
This will import from node's `stream` module when the platform is `node` and from `./stub.js` otherwise.

* No longer throw an error when a `Symbol` is missing ([#3453](https://github.com/evanw/esbuild/issues/3453))

Certain JavaScript syntax features use special properties on the global `Symbol` object. For example, the asynchronous iteration syntax uses `Symbol.asyncIterator`. Previously esbuild's generated code for older browsers required this symbol to be polyfilled. However, starting with this release esbuild will use [`Symbol.for()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for) to construct these symbols if they are missing instead of throwing an error about a missing polyfill. This means your code no longer needs to include a polyfill for missing symbols as long as your code also uses `Symbol.for()` for missing symbols.
Expand Down
35 changes: 35 additions & 0 deletions internal/bundler_tests/bundler_packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2873,3 +2873,38 @@ func TestPackageJsonDisabledTypeModuleIssue3367(t *testing.T) {
},
})
}

// See: https://github.com/evanw/esbuild/issues/3485
func TestPackageJsonSubpathImportNodeBuiltinIssue3485(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import fs from '#fs'
import http from '#http'
fs.readFileSync()
http.createServer()
`,
"/package.json": `
{
"imports": {
"#fs": {
"node": "fs",
"default": "./empty.js"
},
"#http": {
"node": "node:http",
"default": "./empty.js"
}
}
}
`,
"/empty.js": ``,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
Platform: config.PlatformNode,
AbsOutputFile: "/out.js",
},
})
}
9 changes: 9 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_packagejson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,15 @@ console.log("pkg3");
// tmp/pkg/@scope/pkg4/bat.js
console.log("pkg4");

================================================================================
TestPackageJsonSubpathImportNodeBuiltinIssue3485
---------- /out.js ----------
// entry.js
import fs from "fs";
import http from "node:http";
fs.readFileSync();
http.createServer();

================================================================================
TestPackageJsonTypeShouldBeTypes
---------- /Users/user/project/out.js ----------
Loading

0 comments on commit 83e8c7f

Please sign in to comment.