Skip to content

Commit e3f4e2d

Browse files
committed
fix evanw#3821: allow node: prefix with es* targets
1 parent 9d50680 commit e3f4e2d

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Allow using the `node:` import prefix with `es*` targets ([#3821](https://github.com/evanw/esbuild/issues/3821))
6+
7+
The [`node:` prefix on imports](https://nodejs.org/api/esm.html#node-imports) is an alternate way to import built-in node modules. For example, `import fs from "fs"` can also be written `import fs from "node:fs"`. This only works with certain newer versions of node, so esbuild removes it when you target older versions of node such as with `--target=node14` so that your code still works. With the way esbuild's platform-specific feature compatibility table works, this was added by saying that only newer versions of node support this feature. However, that means that a target such as `--target=node18,es2022` removes the `node:` prefix because none of the `es*` targets are known to support this feature. This release adds the support for the `node:` flag to esbuild's internal compatibility table for `es*` to allow you to use compound targets like this:
8+
9+
```js
10+
// Original code
11+
import fs from 'node:fs'
12+
fs.open
13+
14+
// Old output (with --bundle --format=esm --platform=node --target=node18,es2022)
15+
import fs from "node";
16+
fs.open;
17+
18+
// New output (with --bundle --format=esm --platform=node --target=node18,es2022)
19+
import fs from "node:fs";
20+
fs.open;
21+
```
22+
323
## 0.23.0
424

525
**_This release deliberately contains backwards-incompatible changes._** To avoid automatically picking up releases like this, you should either be pinning the exact version of `esbuild` in your `package.json` file (recommended) or be using a version range syntax that only accepts patch upgrades such as `^0.22.0` or `~0.22.0`. See npm's documentation about [semver](https://docs.npmjs.com/cli/v6/using-npm/semver/) for more information.

compat-table/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ import('./kangax').then(kangax => {
466466
'15': { force: false },
467467
'16': { force: true },
468468
}
469+
js.NodeColonPrefixImport.ES = { 0: { force: true } }
470+
js.NodeColonPrefixRequire.ES = { 0: { force: true } }
469471

470472
// Arbitrary Module Namespace Names
471473
{

internal/compat/js_table.go

+2
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,11 @@ var jsTable = map[JSFeature]map[Engine][]versionRange{
651651
Safari: {{start: v{10, 0, 0}}},
652652
},
653653
NodeColonPrefixImport: {
654+
ES: {{start: v{0, 0, 0}}},
654655
Node: {{start: v{12, 20, 0}, end: v{13, 0, 0}}, {start: v{14, 13, 1}}},
655656
},
656657
NodeColonPrefixRequire: {
658+
ES: {{start: v{0, 0, 0}}},
657659
Node: {{start: v{14, 18, 0}, end: v{15, 0, 0}}, {start: v{16, 0, 0}}},
658660
},
659661
NullishCoalescing: {

scripts/js-api-tests.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,7 @@ import "after/alias";
30653065
stdin: { contents: `import fs from 'node:fs'; import('node:fs'); fs()` },
30663066
bundle: true,
30673067
platform: 'node',
3068-
target,
3068+
target: target.split(','),
30693069
format: 'esm',
30703070
write: false,
30713071
})
@@ -3079,6 +3079,7 @@ import "after/alias";
30793079
assert.strictEqual(await tryTargetESM('node12.99'), `// <stdin>\nimport fs from "node:fs";\nimport("node:fs");\nfs();\n`)
30803080
assert.strictEqual(await tryTargetESM('node12.20'), `// <stdin>\nimport fs from "node:fs";\nimport("node:fs");\nfs();\n`)
30813081
assert.strictEqual(await tryTargetESM('node12.19'), `// <stdin>\nimport fs from "fs";\nPromise.resolve().then(() => __toESM(__require("fs")));\nfs();\n`)
3082+
assert.strictEqual(await tryTargetESM('node18,es6'), `// <stdin>\nimport fs from "node:fs";\nimport("node:fs");\nfs();\n`)
30823083
},
30833084

30843085
async nodeColonPrefixRequire({ esbuild }) {
@@ -3087,7 +3088,7 @@ import "after/alias";
30873088
stdin: { contents: `require('node:fs'); require.resolve('node:fs')` },
30883089
bundle: true,
30893090
platform: 'node',
3090-
target,
3091+
target: target.split(','),
30913092
format: 'cjs',
30923093
write: false,
30933094
})
@@ -3101,6 +3102,7 @@ import "after/alias";
31013102
assert.strictEqual(await tryTargetESM('node14.99'), `// <stdin>\nrequire("node:fs");\nrequire.resolve("node:fs");\n`)
31023103
assert.strictEqual(await tryTargetESM('node14.18'), `// <stdin>\nrequire("node:fs");\nrequire.resolve("node:fs");\n`)
31033104
assert.strictEqual(await tryTargetESM('node14.17'), `// <stdin>\nrequire("fs");\nrequire.resolve("fs");\n`)
3105+
assert.strictEqual(await tryTargetESM('node18,es6'), `// <stdin>\nrequire("node:fs");\nrequire.resolve("node:fs");\n`)
31043106
},
31053107

31063108
async nodeColonPrefixImportTurnedIntoRequire({ esbuild }) {
@@ -3109,7 +3111,7 @@ import "after/alias";
31093111
stdin: { contents: `import fs from 'node:fs'; import('node:fs'); fs()` },
31103112
bundle: true,
31113113
platform: 'node',
3112-
target,
3114+
target: target.split(','),
31133115
format: 'cjs',
31143116
write: false,
31153117
})
@@ -3123,6 +3125,7 @@ import "after/alias";
31233125
assert.strictEqual(await tryTargetESM('node14.99'), `// <stdin>\nvar import_node_fs = __toESM(require("node:fs"));\nimport("node:fs");\n(0, import_node_fs.default)();\n`)
31243126
assert.strictEqual(await tryTargetESM('node14.18'), `// <stdin>\nvar import_node_fs = __toESM(require("node:fs"));\nimport("node:fs");\n(0, import_node_fs.default)();\n`)
31253127
assert.strictEqual(await tryTargetESM('node14.17'), `// <stdin>\nvar import_node_fs = __toESM(require("fs"));\nimport("fs");\n(0, import_node_fs.default)();\n`)
3128+
assert.strictEqual(await tryTargetESM('node18,es6'), `// <stdin>\nvar import_node_fs = __toESM(require("node:fs"));\nimport("node:fs");\n(0, import_node_fs.default)();\n`)
31263129
},
31273130

31283131
async zipFile({ esbuild, testDir }) {

0 commit comments

Comments
 (0)