Skip to content

Commit

Permalink
js/esbuild: Don't try to resolve packages in /assets marked as external
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 22, 2024
1 parent 4a5e940 commit a08a8b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/js/esbuild/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ func createBuildPlugins(rs *resources.Spec, assetsResolver *fsResolver, depsMana
}
}

for _, ext := range opts.Externals {
// ESBuild will do a more thorough check for packages resolved in node_modules,
// but we need to make sure that we don't try to resolve these in the /assets folder.
if ext == impPath {
return api.OnResolveResult{
Path: impPath,
External: true,
}, nil
}
}

if opts.ImportOnResolveFunc != nil {
if s := opts.ImportOnResolveFunc(impPath, args); s != "" {
return api.OnResolveResult{Path: s, Namespace: NsHugoImportResolveFunc}, nil
Expand Down
29 changes: 29 additions & 0 deletions resources/resource_transformers/js/js_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,32 @@ class A {}
}).Build()
b.AssertFileContent("public/js/main.js", "__decorateClass")
}

// Issue 13183.
func TestExternalsInAssets(t *testing.T) {
files := `
-- assets/js/util1.js --
export function hello1() {
return 'abcd';
}
-- assets/js/util2.js --
export function hello2() {
return 'efgh';
}
-- assets/js/main.js --
import { hello1 } from './util1.js';
import { hello2 } from './util2.js';
hello1();
hello2();
-- layouts/index.html --
Home.
{{ $js := resources.Get "js/main.js" | js.Build (dict "externals" (slice "./util1.js")) }}
{{ $js.Publish }}
`

b := hugolib.Test(t, files, hugolib.TestOptOsFs())

b.AssertFileContent("public/js/main.js", "efgh")
b.AssertFileContent("public/js/main.js", "! abcd")
}

0 comments on commit a08a8b0

Please sign in to comment.