Skip to content

Commit

Permalink
fix #1466: paths with "node:" prefix are external
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 26, 2021
1 parent 6d35ea9 commit 025d64e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

Note that these old node versions are [currently in maintenance](https://nodejs.org/en/about/releases/). I recommend upgrading to a modern version of node if run-time performance is important to you.

* Paths starting with `node:` are implicitly external when bundling for node ([#1466](https://github.com/evanw/esbuild/issues/1466))

This replicates a new node feature where you can [prefix an import path with `node:`](https://nodejs.org/api/esm.html#esm_node_imports) to load a native node module by that name (such as `import fs from "node:fs/promises"`). These paths also [have special behavior](https://nodejs.org/api/modules.html#modules_core_modules):

> Core modules can also be identified using the `node:` prefix, in which case it bypasses the `require` cache. For instance, `require('node:http')` will always return the built in HTTP module, even if there is `require.cache` entry by that name.

With this release, esbuild's built-in resolver will now automatically consider all import paths starting with `node:` as external. This new behavior is only active when the current platform is set to node such as with `--platform=node`. If you need to customize this behavior, you can write a plugin to intercept these paths and treat them differently.

## 0.12.15

* Fix a bug with `var()` in CSS color lowering ([#1421](https://github.com/evanw/esbuild/issues/1421))
Expand Down
17 changes: 17 additions & 0 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,23 @@ func TestAutoExternal(t *testing.T) {
})
}

func TestAutoExternalNode(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
// These URLs should be external automatically
import fs from "node:fs/promises";
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
Platform: config.PlatformNode,
},
})
}

func TestExternalWithWildcard(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
6 changes: 6 additions & 0 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ import "https://example.com/code.js";
import "//example.com/code.js";
import "data:application/javascript;base64,ZXhwb3J0IGRlZmF1bHQgMTIz";

================================================================================
TestAutoExternalNode
---------- /out/entry.js ----------
// entry.js
import fs from "node:fs/promises";

================================================================================
TestAvoidTDZ
---------- /out.js ----------
Expand Down
5 changes: 4 additions & 1 deletion internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ func (rr *resolver) Resolve(sourceDir string, importPath string, kind ast.Import
strings.HasPrefix(importPath, "https://") ||

// "background: url(//example.com/images/image.png);"
strings.HasPrefix(importPath, "//") {
strings.HasPrefix(importPath, "//") ||

// "import fs from 'node:fs'"
(r.options.Platform == config.PlatformNode && strings.HasPrefix(importPath, "node:")) {

if r.debugLogs != nil {
r.debugLogs.addNote("Marking this path as implicitly external")
Expand Down

0 comments on commit 025d64e

Please sign in to comment.