Skip to content

Commit

Permalink
add the original field to the metafile
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 2, 2023
1 parent d59ca10 commit 9234378
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

The CLI calls the Go API and then also writes out the metafile and/or mangle cache JSON files if those features are enabled. This extra step is necessary because these files are returned by the Go API as in-memory strings. However, this extra step accidentally didn't happen for all builds after the initial build when watch mode was enabled. This behavior used to work but it was broken in version 0.14.18 by the introduction of the mangle cache feature. This release fixes the combination of these features, so the metafile and mangle cache features should now work with watch mode. This behavior was only broken for the CLI, not for the JS or Go APIs.

* Add an `original` field to the metafile

The metadata file JSON now has an additional field: each import in an input file now contains the pre-resolved path in the `original` field in addition to the post-resolved path in the `path` field. This means it's now possible to run certain additional analysis over your bundle. For example, you should be able to use this to detect when the same package subpath is represented multiple times in the bundle, either because multiple versions of a package were bundled or because a package is experiencing the [dual-package hazard](https://nodejs.org/api/packages.html#dual-package-hazard).

## 0.16.12

* Loader defaults to `js` for extensionless files ([#2776](https://github.com/evanw/esbuild/issues/2776))
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,6 @@ READMIN_ESBUILD_FLAGS += --format=esm
READMIN_ESBUILD_FLAGS += --loader:.png=file
READMIN_ESBUILD_FLAGS += --loader:.svg=file
READMIN_ESBUILD_FLAGS += --minify
READMIN_ESBUILD_FLAGS += --outdir=derp
READMIN_ESBUILD_FLAGS += --sourcemap
READMIN_ESBUILD_FLAGS += --splitting
READMIN_ESBUILD_FLAGS += --target=esnext
Expand Down
5 changes: 3 additions & 2 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1913,9 +1913,10 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann
} else {
sb.WriteString(",\n ")
}
sb.WriteString(fmt.Sprintf("{\n \"path\": %s,\n \"kind\": %s\n }",
sb.WriteString(fmt.Sprintf("{\n \"path\": %s,\n \"kind\": %s,\n \"original\": %s\n }",
helpers.QuoteForJSON(otherFile.inputFile.Source.PrettyPath, s.options.ASCIIOnly),
helpers.QuoteForJSON(record.Kind.StringForMetafile(), s.options.ASCIIOnly)))
helpers.QuoteForJSON(record.Kind.StringForMetafile(), s.options.ASCIIOnly),
helpers.QuoteForJSON(record.Path.Text, s.options.ASCIIOnly)))
}

// Validate that imports with "assert { type: 'json' }" were imported
Expand Down
6 changes: 4 additions & 2 deletions internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ console.log("bar");
"imports": [
{
"path": "common.css",
"kind": "import-statement"
"kind": "import-statement",
"original": "../common.css"
}
]
},
Expand All @@ -434,7 +435,8 @@ console.log("bar");
"imports": [
{
"path": "common.css",
"kind": "import-statement"
"kind": "import-statement",
"original": "../common.css"
}
]
}
Expand Down
39 changes: 26 additions & 13 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3297,19 +3297,23 @@ d {
},
{
"path": "project/esm.js",
"kind": "import-statement"
"kind": "import-statement",
"original": "./esm"
},
{
"path": "<data:application/json,2>",
"kind": "import-statement"
"kind": "import-statement",
"original": "data:application/json,2"
},
{
"path": "project/file.file",
"kind": "import-statement"
"kind": "import-statement",
"original": "./file.file"
},
{
"path": "project/copy.copy",
"kind": "import-statement"
"kind": "import-statement",
"original": "./copy.copy"
},
{
"path": "extern-cjs",
Expand All @@ -3318,11 +3322,13 @@ d {
},
{
"path": "project/cjs.js",
"kind": "require-call"
"kind": "require-call",
"original": "./cjs"
},
{
"path": "project/dynamic.js",
"kind": "dynamic-import"
"kind": "dynamic-import",
"original": "./dynamic"
}
]
},
Expand All @@ -3340,15 +3346,18 @@ d {
},
{
"path": "project/inline.svg",
"kind": "url-token"
"kind": "url-token",
"original": "inline.svg"
},
{
"path": "project/file.file",
"kind": "url-token"
"kind": "url-token",
"original": "file.file"
},
{
"path": "project/copy.copy",
"kind": "url-token"
"kind": "url-token",
"original": "copy.copy"
},
{
"path": "extern.png",
Expand Down Expand Up @@ -3537,7 +3546,8 @@ a {
"imports": [
{
"path": "project/111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.file",
"kind": "import-statement"
"kind": "import-statement",
"original": "./111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.file"
}
]
},
Expand All @@ -3550,7 +3560,8 @@ a {
"imports": [
{
"path": "project/222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.copy",
"kind": "import-statement"
"kind": "import-statement",
"original": "./222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.copy"
}
]
},
Expand All @@ -3563,7 +3574,8 @@ a {
"imports": [
{
"path": "project/333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.js",
"kind": "dynamic-import"
"kind": "dynamic-import",
"original": "./333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.js"
}
]
},
Expand All @@ -3576,7 +3588,8 @@ a {
"imports": [
{
"path": "project/444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.file",
"kind": "url-token"
"kind": "url-token",
"original": "444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.file"
}
]
}
Expand Down
18 changes: 12 additions & 6 deletions internal/bundler_tests/snapshots/snapshots_loader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ a {
"imports": [
{
"path": "a.empty",
"kind": "import-rule"
"kind": "import-rule",
"original": "a.empty"
},
{
"path": "b.empty",
"kind": "url-token"
"kind": "url-token",
"original": "b.empty"
}
]
}
Expand Down Expand Up @@ -129,19 +131,23 @@ console.log(ns, import_c.default, void 0);
"imports": [
{
"path": "a.empty",
"kind": "import-statement"
"kind": "import-statement",
"original": "./a.empty"
},
{
"path": "b.empty",
"kind": "import-statement"
"kind": "import-statement",
"original": "./b.empty"
},
{
"path": "c.empty",
"kind": "import-statement"
"kind": "import-statement",
"original": "./c.empty"
},
{
"path": "d.empty",
"kind": "import-statement"
"kind": "import-statement",
"original": "./d.empty"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ export interface Metafile {
path: string
kind: ImportKind
external?: boolean
original?: string
}[]
}
}
Expand Down
58 changes: 35 additions & 23 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,9 @@ body {
// Check inputs
assert.deepStrictEqual(json.inputs[makePath(entry)].bytes, 144)
assert.deepStrictEqual(json.inputs[makePath(entry)].imports, [
{ path: makePath(imported), kind: 'import-statement' },
{ path: makePath(css), kind: 'import-statement' },
{ path: makePath(text), kind: 'require-call' },
{ path: makePath(imported), kind: 'import-statement', original: './imported' },
{ path: makePath(css), kind: 'import-statement', original: './example.css' },
{ path: makePath(text), kind: 'require-call', original: './text.txt' },
])
assert.deepStrictEqual(json.inputs[makePath(imported)].bytes, 18)
assert.deepStrictEqual(json.inputs[makePath(imported)].imports, [])
Expand Down Expand Up @@ -1124,8 +1124,14 @@ body {
const outEntry2 = makeOutPath(path.basename(entry2));
const outChunk = makeOutPath(chunk);

assert.deepStrictEqual(json.inputs[inEntry1], { bytes: 94, imports: [{ path: inImported, kind: 'import-statement' }] })
assert.deepStrictEqual(json.inputs[inEntry2], { bytes: 107, imports: [{ path: inImported, kind: 'import-statement' }] })
assert.deepStrictEqual(json.inputs[inEntry1], {
bytes: 94,
imports: [{ path: inImported, kind: 'import-statement', original: './' + path.basename(imported) }],
})
assert.deepStrictEqual(json.inputs[inEntry2], {
bytes: 107,
imports: [{ path: inImported, kind: 'import-statement', original: './' + path.basename(imported) }],
})
assert.deepStrictEqual(json.inputs[inImported], { bytes: 118, imports: [] })

assert.deepStrictEqual(json.outputs[outEntry1].imports, [{ path: makeOutPath(chunk), kind: 'import-statement' }])
Expand Down Expand Up @@ -1188,8 +1194,14 @@ body {
const outEntry2 = makeOutPath(path.basename(entry2));
const outChunk = makeOutPath(chunk);

assert.deepStrictEqual(json.inputs[inEntry1], { bytes: 94, imports: [{ path: inImported, kind: 'import-statement' }] })
assert.deepStrictEqual(json.inputs[inEntry2], { bytes: 107, imports: [{ path: inImported, kind: 'import-statement' }] })
assert.deepStrictEqual(json.inputs[inEntry1], {
bytes: 94,
imports: [{ path: inImported, kind: 'import-statement', original: './' + path.basename(imported) }],
})
assert.deepStrictEqual(json.inputs[inEntry2], {
bytes: 107,
imports: [{ path: inImported, kind: 'import-statement', original: './' + path.basename(imported) }],
})
assert.deepStrictEqual(json.inputs[inImported], { bytes: 118, imports: [] })

assert.deepStrictEqual(json.outputs[outEntry1].imports, [{ path: makeOutPath(chunk), kind: 'import-statement' }])
Expand All @@ -1212,18 +1224,18 @@ body {
const import2 = path.join(importDir, 'import2.js')
const shared = path.join(testDir, 'shared.js')
const outdir = path.join(testDir, 'out')
const makeImportPath = (importing, imported) => JSON.stringify('./' + path.relative(path.dirname(importing), imported).split(path.sep).join('/'))
const makeImportPath = (importing, imported) => './' + path.relative(path.dirname(importing), imported).split(path.sep).join('/')
await mkdirAsync(importDir)
await writeFileAsync(entry, `
import ${makeImportPath(entry, shared)}
import(${makeImportPath(entry, import1)})
import(${makeImportPath(entry, import2)})
import ${JSON.stringify(makeImportPath(entry, shared))}
import(${JSON.stringify(makeImportPath(entry, import1))})
import(${JSON.stringify(makeImportPath(entry, import2))})
`)
await writeFileAsync(import1, `
import ${makeImportPath(import1, shared)}
import ${JSON.stringify(makeImportPath(import1, shared))}
`)
await writeFileAsync(import2, `
import ${makeImportPath(import2, shared)}
import ${JSON.stringify(makeImportPath(import2, shared))}
`)
await writeFileAsync(shared, `
console.log('side effect')
Expand Down Expand Up @@ -1258,21 +1270,21 @@ body {
assert.deepStrictEqual(json.inputs[inEntry], {
bytes: 112,
imports: [
{ path: inShared, kind: 'import-statement' },
{ path: inImport1, kind: 'dynamic-import' },
{ path: inImport2, kind: 'dynamic-import' },
{ path: inShared, kind: 'import-statement', original: makeImportPath(entry, shared) },
{ path: inImport1, kind: 'dynamic-import', original: makeImportPath(entry, import1) },
{ path: inImport2, kind: 'dynamic-import', original: makeImportPath(entry, import2) },
]
})
assert.deepStrictEqual(json.inputs[inImport1], {
bytes: 35,
imports: [
{ path: inShared, kind: 'import-statement' },
{ path: inShared, kind: 'import-statement', original: makeImportPath(import1, shared) },
]
})
assert.deepStrictEqual(json.inputs[inImport2], {
bytes: 35,
imports: [
{ path: inShared, kind: 'import-statement' },
{ path: inShared, kind: 'import-statement', original: makeImportPath(import2, shared) },
]
})
assert.deepStrictEqual(json.inputs[inShared], { bytes: 38, imports: [] })
Expand Down Expand Up @@ -1447,11 +1459,11 @@ body {
const makePath = pathname => path.relative(cwd, pathname).split(path.sep).join('/')
const json = result.metafile
assert.deepStrictEqual(json.inputs[makePath(entry)].imports, [
{ path: makePath(nested1), kind: 'import-statement' },
{ path: makePath(nested2), kind: 'import-statement' },
{ path: makePath(nested1), kind: 'import-statement', original: nested1 },
{ path: makePath(nested2), kind: 'import-statement', original: nested2 },
])
assert.deepStrictEqual(json.inputs[makePath(nested1)].imports, [
{ path: makePath(nested3), kind: 'import-statement' },
{ path: makePath(nested3), kind: 'import-statement', original: nested3 },
])
assert.deepStrictEqual(json.inputs[makePath(nested2)].imports, [])
assert.deepStrictEqual(json.inputs[makePath(nested3)].imports, [])
Expand Down Expand Up @@ -1494,12 +1506,12 @@ body {
[makePath(entry)]: {
bytes: 98,
imports: [
{ path: makePath(imported), kind: 'import-rule' },
{ path: makePath(imported), kind: 'import-rule', original: './imported' },
{ external: true, kind: 'url-token', path: 'https://example.com/external.png' },
]
},
[makePath(image)]: { bytes: 8, imports: [] },
[makePath(imported)]: { bytes: 48, imports: [{ path: makePath(image), kind: 'url-token' }] },
[makePath(imported)]: { bytes: 48, imports: [{ path: makePath(image), kind: 'url-token', original: './example.png' }] },
},
outputs: {
[makePath(output)]: {
Expand Down

0 comments on commit 9234378

Please sign in to comment.