From 8d98f6f6e663f7ecc9f0496edbd8bb8314b0333a Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Thu, 19 Dec 2024 20:51:05 -0500 Subject: [PATCH] fix #3985: `entryPoint` metadata for `copy` loader --- CHANGELOG.md | 4 + internal/bundler/bundler.go | 10 ++- internal/bundler_tests/bundler_loader_test.go | 1 + .../snapshots/snapshots_loader.txt | 74 +++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fcd16945a5..7e2791debe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,10 @@ This can sometimes expose additional minification opportunities. +* Include `entryPoint` metadata for the `copy` loader ([#3985](https://github.com/evanw/esbuild/issues/3985)) + + Almost all entry points already include a `entryPoint` field in the `outputs` map in esbuild's build metadata. However, this wasn't the case for the `copy` loader as that loader is a special-case that doesn't behave like other loaders. This release adds the `entryPoint` field in this case. + * Avoid using the parent directory name for determinism ([#3998](https://github.com/evanw/esbuild/issues/3998)) To make generated code more readable, esbuild includes the name of the source file when generating certain variable names within the file. Specifically bundling a CommonJS file generates a variable to store the lazily-evaluated module initializer. However, if a file is named `index.js` (or with a different extension), esbuild will use the name of the parent directory instead for a better name (since many packages have files all named `index.js` but have unique directory names). diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index aeae8397e0b..3b398ec18c2 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -2577,11 +2577,13 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann // the entry point itself. customFilePath := "" useOutputFile := false + isEntryPoint := false if result.file.inputFile.Loader == config.LoaderCopy { if metaIndex, ok := entryPointSourceIndexToMetaIndex[uint32(sourceIndex)]; ok { template = s.options.EntryPathTemplate customFilePath = entryPointMeta[metaIndex].OutputPath useOutputFile = s.options.AbsOutputFile != "" + isEntryPoint = true } } @@ -2632,8 +2634,14 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann helpers.QuoteForJSON(result.file.inputFile.Source.PrettyPath, s.options.ASCIIOnly), len(bytes), ) + entryPointJSON := "" + if isEntryPoint { + entryPointJSON = fmt.Sprintf("\"entryPoint\": %s,\n ", + helpers.QuoteForJSON(result.file.inputFile.Source.PrettyPath, s.options.ASCIIOnly)) + } jsonMetadataChunk = fmt.Sprintf( - "{\n \"imports\": [],\n \"exports\": [],\n \"inputs\": %s,\n \"bytes\": %d\n }", + "{\n \"imports\": [],\n \"exports\": [],\n %s\"inputs\": %s,\n \"bytes\": %d\n }", + entryPointJSON, inputs, len(bytes), ) diff --git a/internal/bundler_tests/bundler_loader_test.go b/internal/bundler_tests/bundler_loader_test.go index 73e81dc4fb5..5b47f5f715b 100644 --- a/internal/bundler_tests/bundler_loader_test.go +++ b/internal/bundler_tests/bundler_loader_test.go @@ -1194,6 +1194,7 @@ func TestLoaderCopyWithBundleEntryPoint(t *testing.T) { ".css": config.LoaderCSS, ".file": config.LoaderCopy, }, + NeedsMetafile: true, }, }) } diff --git a/internal/bundler_tests/snapshots/snapshots_loader.txt b/internal/bundler_tests/snapshots/snapshots_loader.txt index 38164783a0e..d0f1699014b 100644 --- a/internal/bundler_tests/snapshots/snapshots_loader.txt +++ b/internal/bundler_tests/snapshots/snapshots_loader.txt @@ -337,6 +337,80 @@ console.log(x); body { background: url("../assets/some.file"); } +---------- metafile.json ---------- +{ + "inputs": { + "Users/user/project/assets/some.file": { + "bytes": 5, + "imports": [] + }, + "Users/user/project/src/entry.js": { + "bytes": 63, + "imports": [ + { + "path": "Users/user/project/assets/some.file", + "kind": "import-statement", + "original": "../assets/some.file" + } + ], + "format": "esm" + }, + "Users/user/project/src/entry.css": { + "bytes": 64, + "imports": [ + { + "path": "Users/user/project/assets/some.file", + "kind": "url-token", + "original": "../assets/some.file" + } + ] + } + }, + "outputs": { + "out/assets/some.file": { + "imports": [], + "exports": [], + "entryPoint": "Users/user/project/assets/some.file", + "inputs": { + "Users/user/project/assets/some.file": { + "bytesInOutput": 5 + } + }, + "bytes": 5 + }, + "out/src/entry.js": { + "imports": [ + { + "path": "out/assets/some.file", + "kind": "import-statement" + } + ], + "exports": [], + "entryPoint": "Users/user/project/src/entry.js", + "inputs": { + "Users/user/project/src/entry.js": { + "bytesInOutput": 53 + } + }, + "bytes": 88 + }, + "out/src/entry.css": { + "imports": [ + { + "path": "out/assets/some.file", + "kind": "url-token" + } + ], + "entryPoint": "Users/user/project/src/entry.css", + "inputs": { + "Users/user/project/src/entry.css": { + "bytesInOutput": 51 + } + }, + "bytes": 90 + } + } +} ================================================================================ TestLoaderCopyWithBundleFromCSS