Skip to content

Commit

Permalink
fix #3985: entryPoint metadata for copy loader
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 20, 2024
1 parent 0db1b82 commit 8d98f6f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
10 changes: 9 additions & 1 deletion internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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),
)
Expand Down
1 change: 1 addition & 0 deletions internal/bundler_tests/bundler_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ func TestLoaderCopyWithBundleEntryPoint(t *testing.T) {
".css": config.LoaderCSS,
".file": config.LoaderCopy,
},
NeedsMetafile: true,
},
})
}
Expand Down
74 changes: 74 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_loader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8d98f6f

Please sign in to comment.