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

await import(pathToJSONFile, { with: { type: 'json' } }) transpiles to { assert: { type: 'json' }} in 6.1.x #19506

Closed
7 tasks done
johnnybenson opened this issue Feb 24, 2025 · 3 comments

Comments

@johnnybenson
Copy link

johnnybenson commented Feb 24, 2025

Describe the bug

Looking at the compiled output, it looks like 6.1.x reverted back to the old keyword assert for dynamic imports instead of with -- is this expected?

6.1.x

const outputFilePath = path.join(PATHS.source, outputFile);
const outputData = await import(outputFilePath, { assert: { type: 'json' } });

6.0.1

const outputFilePath = path.join(PATHS.source, outputFile);
const outputData = await import(outputFilePath, { with: { type: 'json' } });
node:internal/modules/esm/assert:88
        throw new ERR_IMPORT_ATTRIBUTE_MISSING(url, 'type', validType);
              ^

TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file:///Users/johnny/path/to/my/data.json" needs an import attribute of "type: json"
    at validateAttributes (node:internal/modules/esm/assert:88:15)
    at defaultLoad (node:internal/modules/esm/load:129:3)
    at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:516:32)
    at async ModuleJob._link (node:internal/modules/esm/module_job:115:19) {
  code: 'ERR_IMPORT_ATTRIBUTE_MISSING'
}

Node.js v23.6.0
error Command failed

The compiled CJS code like the above started emitting errors for me in 6.1.0, 6.1.1.
Downgrading to 6.0.11, the error seems to go away.

Experimented with build target a bit esnext, modules, node23, chrome123
None of these transpile to the syntax { with: { type: 'json' } }

Checked MDN to make sure with is still the thing 😅

Checked MDN again to make sure variables are OK to use. Vite Docs give mixed signals about variables in dynamic imports, despite this working prior to upgrading.

Checked esbuild and everything seems good
Image

Note: This particular project targets 'esnext' everywhere, I understand that's a moving target. If I just need to update configuration to achieve the expected output, that sounds good to me! Thanks for any insights.

Reproduction

https://github.com/johnnybenson/vite-await-import-with-type-json-bug

Steps to reproduce

Steps

git clone git@github.com:johnnybenson/vite-await-import-with-type-json-bug.git .
yarn install
yarn build
yarn start_cjs 
# yarn start_es

Source

cat ./src/index.ts | grep with   
#  return await import(pathToJSONFile, { with: { type: 'json' } });

Build

cat ./dist/index.js | grep assert                                             
#  return await import(pathToJSONFile, { assert: { type: "json" } });

System Info

System:
    OS: macOS 15.3.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 22.34 GB / 96.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.6.0 - ~/.nvm/versions/node/v23.6.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v23.6.0/bin/yarn
    npm: 10.9.2 - ~/.nvm/versions/node/v23.6.0/bin/npm
    pnpm: 9.15.4 - ~/.nvm/versions/node/v23.6.0/bin/pnpm
  Browsers:
    Chrome: 133.0.6943.127
    Safari: 18.3
  npmPackages:
    vite: 6.1.1 => 6.1.1

Used Package Manager

yarn

Logs

No response

Validations

@johnnybenson
Copy link
Author

johnnybenson commented Feb 24, 2025

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 25, 2025

It looks like this needs to be configured by rollup's output.importAttributesKey: "with" since the default is "assert". Here is an updated repro https://stackblitz.com/edit/github-492ifbmg?file=vite.config.ts%3AL19

Probably older rollup was ignoring this config and outputting with, but it was fixed to respect the config rollup/rollup#5814 You need to manually configure output.importAttributesKey from now on.

(Maybe the default should be changed, but I think this is an upstream issue, so let me close this for now.)

@johnnybenson
Copy link
Author

thanks so much, agree about the default behavior too. cheers!

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

No branches or pull requests

2 participants