Skip to content

Commit

Permalink
fix #549: do not include public path in metafile
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 20, 2020
1 parent ed2064b commit 112c06d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@

The install script runs `npm` in a temporary directory to download the correct binary executable for the current architecture. It then removes the temporary directory after the installation. However, removing a directory is sometimes impossible on Windows. To work around this problem, the install script now installs to the system's temporary directory instead of a directory inside the project itself. That way it's not problematic if a directory is left behind by the install script. This change was contributed by [@Djaler](https://github.com/Djaler).

* Fix the public path ending up in the metafile ([#549](https://github.com/evanw/esbuild/issues/549))

The change in version 0.8.7 to include the public path in import paths of code splitting chunks caused a regression where the public path was also included in the list of chunk imports in the metafile. This was unintentional. Now the public path setting should not affect the metafile contents.

## 0.8.11

* Fix parsing of casts in TypeScript followed by certain tokens
Expand Down
3 changes: 2 additions & 1 deletion internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3465,7 +3465,8 @@ func (repr *chunkReprJS) generate(c *linkerContext, chunk *chunkInfo) func([]ast
} else {
jMeta.AddString(",")
}
importAbsPath := c.fs.Join(c.options.AbsOutputDir, chunk.relDir, record.Path.Text)
chunkBaseWithoutPublicPath := path.Base(record.Path.Text)
importAbsPath := c.fs.Join(c.options.AbsOutputDir, chunk.relDir, chunkBaseWithoutPublicPath)
jMeta.AddString(fmt.Sprintf("\n {\n \"path\": %s\n }",
js_printer.QuoteForJSON(c.res.PrettyPath(logger.Path{Text: importAbsPath, Namespace: "file"}), c.options.ASCIIOnly)))
}
Expand Down
65 changes: 65 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,71 @@ body {
assert.deepStrictEqual(json.outputs[outChunk].inputs, { [inImported]: { bytesInOutput: 51 } })
},

async metafileSplittingPublicPath({ esbuild, testDir }) {
const entry1 = path.join(testDir, 'entry1.js')
const entry2 = path.join(testDir, 'entry2.js')
const imported = path.join(testDir, 'imported.js')
const outdir = path.join(testDir, 'out')
const metafile = path.join(testDir, 'meta.json')
await writeFileAsync(entry1, `
import x, {f1} from "./${path.basename(imported)}"
console.log(1, x, f1())
export {x}
`)
await writeFileAsync(entry2, `
import x, {f2} from "./${path.basename(imported)}"
console.log('entry 2', x, f2())
export {x as y}
`)
await writeFileAsync(imported, `
export default 123
export function f1() {}
export function f2() {}
console.log('shared')
`)
await esbuild.build({
entryPoints: [entry1, entry2],
bundle: true,
outdir,
metafile,
splitting: true,
format: 'esm',
publicPath: 'public',
})

const json = JSON.parse(await readFileAsync(metafile))
assert.strictEqual(Object.keys(json.inputs).length, 3)
assert.strictEqual(Object.keys(json.outputs).length, 3)
const cwd = process.cwd()
const makeOutPath = basename => path.relative(cwd, path.join(outdir, basename)).split(path.sep).join('/')
const makeInPath = pathname => path.relative(cwd, pathname).split(path.sep).join('/')

// Check metafile
const inEntry1 = makeInPath(entry1);
const inEntry2 = makeInPath(entry2);
const inImported = makeInPath(imported);
const chunk = 'chunk.CAAA46JO.js';
const outEntry1 = makeOutPath(path.basename(entry1));
const outEntry2 = makeOutPath(path.basename(entry2));
const outChunk = makeOutPath(chunk);

assert.deepStrictEqual(json.inputs[inEntry1], { bytes: 94, imports: [{ path: inImported }] })
assert.deepStrictEqual(json.inputs[inEntry2], { bytes: 107, imports: [{ path: inImported }] })
assert.deepStrictEqual(json.inputs[inImported], { bytes: 118, imports: [] })

assert.deepStrictEqual(json.outputs[outEntry1].imports, [{ path: makeOutPath(chunk) }])
assert.deepStrictEqual(json.outputs[outEntry2].imports, [{ path: makeOutPath(chunk) }])
assert.deepStrictEqual(json.outputs[outChunk].imports, [])

assert.deepStrictEqual(json.outputs[outEntry1].exports, ['x'])
assert.deepStrictEqual(json.outputs[outEntry2].exports, ['y'])
assert.deepStrictEqual(json.outputs[outChunk].exports, ['imported_default'])

assert.deepStrictEqual(json.outputs[outEntry1].inputs, { [inImported]: { bytesInOutput: 18 }, [inEntry1]: { bytesInOutput: 40 } })
assert.deepStrictEqual(json.outputs[outEntry2].inputs, { [inImported]: { bytesInOutput: 18 }, [inEntry2]: { bytesInOutput: 48 } })
assert.deepStrictEqual(json.outputs[outChunk].inputs, { [inImported]: { bytesInOutput: 51 } })
},

async metafileCJSInFormatIIFE({ esbuild, testDir }) {
const entry = path.join(testDir, 'entry.js')
const outfile = path.join(testDir, 'out.js')
Expand Down

0 comments on commit 112c06d

Please sign in to comment.