[bug] JSON imports broken
niedzielski opened this issue · 3 comments
Thanks for the cool tool! This is a small bug report.
When I try to import any JSON such as the following:
import gamepadMapJSON from './src/input/gamepad/gamepad-map.json' assert {
type: 'json',
}
I get an error:
✘ [ERROR] The file "src/input/gamepad/gamepad-map.json" was loaded with the "js" loader
mod.ts:48:27:
48 │ import gamepadMapJSON from './src/input/gamepad/gamepad-map.json' assert {
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This import assertion requires the loader to be "json" instead:
mod.ts:49:2:
49 │ type: 'json',
╵ ~~~~~~~~~~~~
If I omit the assertion, Deno complains The module is a JSON module and not being imported with an import assertion. Consider adding
assert { type: "json" } to the import statement.deno(no-assert-type)
(and I can't seem to ignore that).
I think I should just be able to add a loader to esbuild:
loader: {
'.json': 'json',
},
But that seemed to have no effect. I tried to track it down and I think esbuild_deno_loader is maybe somehow intercepting the JSON import? I'm working around this with the following JSON plugin:
{
name: 'json',
setup: (build) =>
build.onLoad({ filter: /\.json$/ }, () => ({ loader: 'json' })),
},
In case it's helpful, the whole script looks like this:
const options: esbuild.BuildOptions = {
sourcemap: 'linked',
bundle: true,
entryPoints: [input],
format: 'esm',
logLevel: `warning`,
outfile: output,
treeShaking: true,
minify: true,
plugins: [
// Hack around JSON loader overrides in esbuild_deno_loader that flag type
// assertions.
{
name: 'json',
setup: (build) =>
build.onLoad({ filter: /\.json$/ }, () => ({ loader: 'json' })),
},
denoPlugin({
...(importMap != null && { importMapURL: new URL(importMap) }),
}) as unknown as esbuild.Plugin,
],
}
await esbuild.build(options)
esbuild.stop()
Nice, esbuild fixed the behaviour of the json
loader to only return JSON in the default export recently: evanw/esbuild@b1e737b. I can remove my workaround that is impacting you :)
Still broken on latest version (0.7) with esbuild 0.17.18, using the @niedzielski workaround works
Fixed by #69