Skip to content

Commit 8d98f6f

Browse files
committed
fix #3985: entryPoint metadata for copy loader
1 parent 0db1b82 commit 8d98f6f

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464

6565
This can sometimes expose additional minification opportunities.
6666

67+
* Include `entryPoint` metadata for the `copy` loader ([#3985](https://github.com/evanw/esbuild/issues/3985))
68+
69+
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.
70+
6771
* Avoid using the parent directory name for determinism ([#3998](https://github.com/evanw/esbuild/issues/3998))
6872

6973
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).

internal/bundler/bundler.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2577,11 +2577,13 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann
25772577
// the entry point itself.
25782578
customFilePath := ""
25792579
useOutputFile := false
2580+
isEntryPoint := false
25802581
if result.file.inputFile.Loader == config.LoaderCopy {
25812582
if metaIndex, ok := entryPointSourceIndexToMetaIndex[uint32(sourceIndex)]; ok {
25822583
template = s.options.EntryPathTemplate
25832584
customFilePath = entryPointMeta[metaIndex].OutputPath
25842585
useOutputFile = s.options.AbsOutputFile != ""
2586+
isEntryPoint = true
25852587
}
25862588
}
25872589

@@ -2632,8 +2634,14 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann
26322634
helpers.QuoteForJSON(result.file.inputFile.Source.PrettyPath, s.options.ASCIIOnly),
26332635
len(bytes),
26342636
)
2637+
entryPointJSON := ""
2638+
if isEntryPoint {
2639+
entryPointJSON = fmt.Sprintf("\"entryPoint\": %s,\n ",
2640+
helpers.QuoteForJSON(result.file.inputFile.Source.PrettyPath, s.options.ASCIIOnly))
2641+
}
26352642
jsonMetadataChunk = fmt.Sprintf(
2636-
"{\n \"imports\": [],\n \"exports\": [],\n \"inputs\": %s,\n \"bytes\": %d\n }",
2643+
"{\n \"imports\": [],\n \"exports\": [],\n %s\"inputs\": %s,\n \"bytes\": %d\n }",
2644+
entryPointJSON,
26372645
inputs,
26382646
len(bytes),
26392647
)

internal/bundler_tests/bundler_loader_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ func TestLoaderCopyWithBundleEntryPoint(t *testing.T) {
11941194
".css": config.LoaderCSS,
11951195
".file": config.LoaderCopy,
11961196
},
1197+
NeedsMetafile: true,
11971198
},
11981199
})
11991200
}

internal/bundler_tests/snapshots/snapshots_loader.txt

+74
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,80 @@ console.log(x);
337337
body {
338338
background: url("../assets/some.file");
339339
}
340+
---------- metafile.json ----------
341+
{
342+
"inputs": {
343+
"Users/user/project/assets/some.file": {
344+
"bytes": 5,
345+
"imports": []
346+
},
347+
"Users/user/project/src/entry.js": {
348+
"bytes": 63,
349+
"imports": [
350+
{
351+
"path": "Users/user/project/assets/some.file",
352+
"kind": "import-statement",
353+
"original": "../assets/some.file"
354+
}
355+
],
356+
"format": "esm"
357+
},
358+
"Users/user/project/src/entry.css": {
359+
"bytes": 64,
360+
"imports": [
361+
{
362+
"path": "Users/user/project/assets/some.file",
363+
"kind": "url-token",
364+
"original": "../assets/some.file"
365+
}
366+
]
367+
}
368+
},
369+
"outputs": {
370+
"out/assets/some.file": {
371+
"imports": [],
372+
"exports": [],
373+
"entryPoint": "Users/user/project/assets/some.file",
374+
"inputs": {
375+
"Users/user/project/assets/some.file": {
376+
"bytesInOutput": 5
377+
}
378+
},
379+
"bytes": 5
380+
},
381+
"out/src/entry.js": {
382+
"imports": [
383+
{
384+
"path": "out/assets/some.file",
385+
"kind": "import-statement"
386+
}
387+
],
388+
"exports": [],
389+
"entryPoint": "Users/user/project/src/entry.js",
390+
"inputs": {
391+
"Users/user/project/src/entry.js": {
392+
"bytesInOutput": 53
393+
}
394+
},
395+
"bytes": 88
396+
},
397+
"out/src/entry.css": {
398+
"imports": [
399+
{
400+
"path": "out/assets/some.file",
401+
"kind": "url-token"
402+
}
403+
],
404+
"entryPoint": "Users/user/project/src/entry.css",
405+
"inputs": {
406+
"Users/user/project/src/entry.css": {
407+
"bytesInOutput": 51
408+
}
409+
},
410+
"bytes": 90
411+
}
412+
}
413+
}
340414

341415
================================================================================
342416
TestLoaderCopyWithBundleFromCSS

0 commit comments

Comments
 (0)