From b0a82d33a4d4157e21dc50da2768519af503f97d Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Wed, 14 Dec 2022 11:47:34 -0500 Subject: [PATCH] fix #2731: put `file` loader strings in metafile --- CHANGELOG.md | 26 +++++++++++++++++++ internal/bundler/linker.go | 5 ++++ .../bundler/snapshots/snapshots_loader.txt | 4 +++ lib/shared/types.ts | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bec1260a68e..7822e1f2b9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,31 @@ # Changelog +## Unreleased + +* Include `file` loader strings in metafile imports ([#2731](https://github.com/evanw/esbuild/issues/2731)) + + Bundling a file with the `file` loader copies that file to the output directory and imports a module with the path to the copied file in the `default` export. Previously when bundling with the `file` loader, there was no reference in the metafile from the JavaScript file containing the path string to the copied file. With this release, there will now be a reference in the metafile in the `imports` array with the kind `file-loader`: + + ```diff + { + ... + "outputs": { + "out/image-55CCFTCE.svg": { + ... + }, + "out/entry.js": { + "imports": [ + + { + + "path": "out/image-55CCFTCE.svg", + + "kind": "file-loader" + + } + ], + ... + } + } + } + ``` + ## 0.16.6 * Do not mark subpath imports as external with `--packages=external` ([#2741](https://github.com/evanw/esbuild/issues/2741)) diff --git a/internal/bundler/linker.go b/internal/bundler/linker.go index da01fc11298..7cb3f683921 100644 --- a/internal/bundler/linker.go +++ b/internal/bundler/linker.go @@ -4223,6 +4223,11 @@ func (c *linkerContext) generateCodeForFileInChunkJS( sourceIndex: partRange.sourceIndex, } + if file.InputFile.Loader == config.LoaderFile { + result.JSONMetadataImports = append(result.JSONMetadataImports, fmt.Sprintf("\n {\n \"path\": %s,\n \"kind\": \"file-loader\"\n }", + helpers.QuoteForJSON(file.InputFile.UniqueKeyForAdditionalFile, c.options.ASCIIOnly))) + } + waitGroup.Done() } diff --git a/internal/bundler/snapshots/snapshots_loader.txt b/internal/bundler/snapshots/snapshots_loader.txt index 7b2af5e90ff..acf62eaa25d 100644 --- a/internal/bundler/snapshots/snapshots_loader.txt +++ b/internal/bundler/snapshots/snapshots_loader.txt @@ -1296,6 +1296,10 @@ d { "kind": "import-statement", "external": true }, + { + "path": "out/file-NVISQQTV.file", + "kind": "file-loader" + }, { "path": "out/copy-O3Y5SCJE.copy", "kind": "import-statement" diff --git a/lib/shared/types.ts b/lib/shared/types.ts index 574aea58eb3..e065def2dc3 100644 --- a/lib/shared/types.ts +++ b/lib/shared/types.ts @@ -453,7 +453,7 @@ export interface Metafile { } imports: { path: string - kind: ImportKind + kind: ImportKind | 'file-loader' external?: boolean }[] exports: string[]