deno_emit icon indicating copy to clipboard operation
deno_emit copied to clipboard

`Deno.emit` with `bundle` emit type not working with JSON imports

Open sno2 opened this issue 4 years ago • 4 comments

Found by john.spurlock on Discord

Trying to bundle files through Deno.emit that import JSON Modules via import assertions fails. However, the expected behavior should be for the JSON imports to be handled in some way at compilation. Here's an example project:

// mod.ts
import data from "./data.json" assert { type: "json" };
console.log(data);
// build.ts
console.log(await Deno.emit("./mod.ts"));
// data.json
{ "age": 5 }

The expected result is to be a JavaScript bundle that correctly handles for the JSON imports. However, it instead shows the following error in the diagnostics:

{
      category: 1,
      code: 7042,
      start: { line: 0, character: 20 },
      end: { line: 0, character: 36 },
      messageText: "Module './package.json' was resolved to 'file:///____/data.json', but '--resolveJsonModule' is not used.", 
      messageChain: null,
      source: null,
      sourceLine: 'import { age } from "./data.json" assert { type: "json" };',
      fileName: "file:///____/build.ts",
      relatedInformation: null
}

sno2 avatar Jan 21 '22 02:01 sno2

IMO it would be fine to instead just replace the

import FOO from "./bar.json" assert { type: "json" }

with

const FOO = JSON.parse(/* contents of "./bar.json" as a string */);

Along with the same behavior but Promise.resolve(..) for dynamic imports.

Note: this is what deno bundle already does.

sno2 avatar Jan 21 '22 03:01 sno2

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 22 '22 03:03 stale[bot]

Note: emit is working correctly now (not erroring), but json file is not inlined:

image

kamilogorek avatar Dec 27 '22 22:12 kamilogorek