Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import assertions are not getting stripped out of dynamic import #2036

Closed
tinovyatkin opened this issue Feb 19, 2022 · 4 comments
Closed

Import assertions are not getting stripped out of dynamic import #2036

tinovyatkin opened this issue Feb 19, 2022 · 4 comments

Comments

@tinovyatkin
Copy link

Import assertions are mandatory in dynamic import of JSON modules also, starting Node 16.14 - see https://nodejs.org/docs/latest-v16.x/api/esm.html#import-assertions

But they are resulting in error on previous versions of Node, and so, should be stripped when target is below 16.14 (just like with standard import).

I.e.:

// source
const { default: barData } =
  await import('./bar.json', { assert: { type: 'json' } });

// should stay like this with `target: 'node16.14'`, but should be converted into following with `target: 'node16.13.2`:
const { default: barData } = await import('./bar.json');
@evanw
Copy link
Owner

evanw commented Feb 20, 2022

This appears to be working correctly:

$ echo "import('./bar.json', { assert: { type: 'json' } })" | esbuild --target=node16.14
import("./bar.json", { assert: { type: "json" } });

$ echo "import('./bar.json', { assert: { type: 'json' } })" | esbuild --target=node16.13.2
import("./bar.json");

Are you sure you are using the latest version? What are the steps to reproduce the issue?

@tinovyatkin
Copy link
Author

tinovyatkin commented Feb 20, 2022

Hmm, maybe there is something in details then. I do bundle actually:

Source file, import-test.ts:

import staticImport from 'test.json' assert { type: 'json' };
console.log('static import transpiles well', staticImport);

export const schemaDocument = (
  await import(process.env.SCHEMA_LOCATION!, { assert: { type: 'json' } })
).default;

Bundle:

$ npx esbuild --bundle ./import-test.ts --target=node16.13.2 --format=esm --external:test.json
// ./import-test.ts
import staticImport from "test.json";
console.log('static import transpiles well', staticImport);
var schemaDocument = (await import(process.env.SCHEMA_LOCATION, { assert: { type: "json" } })).default;
export {
  schemaDocument
};

$ npx esbuild --bundle ./import-test.ts --target=node16.14 --format=esm --external:test.json
// ./import-test.ts
import staticImport from "test.json" assert { type: "json" };
console.log('static import transpiles well', staticImport);
var schemaDocument = (await import(process.env.SCHEMA_LOCATION, { assert: { type: "json" } })).default;
export {
  schemaDocument
};

@hyrious
Copy link

hyrious commented Feb 21, 2022

It seems esbuild cannot handle non-string tokens as the first param of import().
(live link).

import(a, { assert: { type: 'json' } })

@evanw
Copy link
Owner

evanw commented Feb 21, 2022

Ah, that makes sense. Looks like I missed one of the cases. Will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants