Skip to content

Commit 0202539

Browse files
committed
js/esbuild: Don't try to resolve packages in /assets marked as external
Fixes #13183
1 parent 4a5e940 commit 0202539

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

internal/js/esbuild/resolve.go

+11
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ func createBuildPlugins(rs *resources.Spec, assetsResolver *fsResolver, depsMana
167167
}
168168
}
169169

170+
for _, ext := range opts.Externals {
171+
// ESBuild will do a more thorough check for packages resolved in node_modules,
172+
// but we need to make sure that we don't try to resolve these in the /assets folder.
173+
if ext == impPath {
174+
return api.OnResolveResult{
175+
Path: impPath,
176+
External: true,
177+
}, nil
178+
}
179+
}
180+
170181
if opts.ImportOnResolveFunc != nil {
171182
if s := opts.ImportOnResolveFunc(impPath, args); s != "" {
172183
return api.OnResolveResult{Path: s, Namespace: NsHugoImportResolveFunc}, nil

resources/resource_transformers/js/js_integration_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,32 @@ class A {}
391391
}).Build()
392392
b.AssertFileContent("public/js/main.js", "__decorateClass")
393393
}
394+
395+
// Issue 13183.
396+
func TestExternalsInAssets(t *testing.T) {
397+
files := `
398+
-- assets/js/util1.js --
399+
export function hello1() {
400+
return 'abcd';
401+
}
402+
-- assets/js/util2.js --
403+
export function hello2() {
404+
return 'efgh';
405+
}
406+
-- assets/js/main.js --
407+
import { hello1 } from './util1.js';
408+
import { hello2 } from './util2.js';
409+
410+
hello1();
411+
hello2();
412+
-- layouts/index.html --
413+
Home.
414+
{{ $js := resources.Get "js/main.js" | js.Build (dict "externals" (slice "./util1.js")) }}
415+
{{ $js.Publish }}
416+
`
417+
418+
b := hugolib.Test(t, files, hugolib.TestOptOsFs())
419+
420+
b.AssertFileContent("public/js/main.js", "efgh")
421+
b.AssertFileContent("public/js/main.js", "! abcd")
422+
}

0 commit comments

Comments
 (0)