Skip to content

Commit

Permalink
fix #2741: --packages=external + subpath imports
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 13, 2022
1 parent 8ccfdd5 commit b69ad87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Do not mark subpath imports as external with `--packages=external` ([#2741](https://github.com/evanw/esbuild/issues/2741))

Node has a feature called [subpath imports](https://nodejs.org/api/packages.html#subpath-imports) where special import paths that start with `#` are resolved using the `imports` field in the `package.json` file of the enclosing package. The intent of the newly-added `--packages=external` setting is to exclude a package's dependencies from the bundle. Since a package's subpath imports are only accessible within that package, it's wrong for them to be affected by `--packages=external`. This release changes esbuild so that `--packages=external` no longer affects subpath imports.

## 0.16.5

* Make it easy to exclude all packages from a bundle ([#1958](https://github.com/evanw/esbuild/issues/1958), [#1975](https://github.com/evanw/esbuild/issues/1975), [#2164](https://github.com/evanw/esbuild/issues/2164), [#2246](https://github.com/evanw/esbuild/issues/2246), [#2542](https://github.com/evanw/esbuild/issues/2542))
Expand Down
17 changes: 13 additions & 4 deletions internal/bundler/bundler_default_test.go
Expand Up @@ -7083,19 +7083,28 @@ NOTE: You can either keep the import assertion and only use the "default" import
func TestExternalPackages(t *testing.T) {
loader_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
"/project/entry.js": `
import 'pkg1'
import './file'
import './node_modules/pkg2/index.js'
import '#pkg3'
`,
"/file.js": `
"/project/package.json": `{
"imports": {
"#pkg3": "./libs/pkg3.js"
}
}`,
"/project/file.js": `
console.log('file')
`,
"/node_modules/pkg2/index.js": `
"/project/node_modules/pkg2/index.js": `
console.log('pkg2')
`,
"/project/libs/pkg3.js": `
console.log('pkg3')
`,
},
entryPaths: []string{"/entry.js"},
entryPaths: []string{"/project/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
Expand Down
9 changes: 6 additions & 3 deletions internal/bundler/snapshots/snapshots_loader.txt
Expand Up @@ -60,15 +60,18 @@ a:after {
================================================================================
TestExternalPackages
---------- /out.js ----------
// entry.js
// project/entry.js
import "pkg1";

// file.js
// project/file.js
console.log("file");

// node_modules/pkg2/index.js
// project/node_modules/pkg2/index.js
console.log("pkg2");

// project/libs/pkg3.js
console.log("pkg3");

================================================================================
TestIndirectRequireMessage
---------- /out/array.js ----------
Expand Down
2 changes: 1 addition & 1 deletion internal/resolver/resolver.go
Expand Up @@ -360,7 +360,7 @@ func (rr *resolver) Resolve(sourceDir string, importPath string, kind ast.Import
}

// "import 'pkg'" when all packages are external (vs. "import './pkg'")
if r.options.ExternalPackages && IsPackagePath(importPath) && !r.fs.IsAbs(importPath) {
if r.options.ExternalPackages && IsPackagePath(importPath) && !r.fs.IsAbs(importPath) && !strings.HasPrefix(importPath, "#") {
if r.debugLogs != nil {
r.debugLogs.addNote("Marking this path as external because it's a package path")
}
Expand Down

0 comments on commit b69ad87

Please sign in to comment.