vite icon indicating copy to clipboard operation
vite copied to clipboard

Build Error: [rollup-plugin-dynamic-import-variables] Unexpected token

Open HenryWong-fe opened this issue 5 years ago • 18 comments

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

  • [x] Read the docs.
  • [x] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.

Describe the bug

can no build this project

Reproduction

https://github.com/HenryWong-fe/vite-project

System Info

  • vite version: ^2.0.0
  • Operating System: mac os 10.15.4
  • Node version: v14.15.1
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.8

Logs (Optional if provided reproduction)

image

HenryWong-fe avatar Mar 02 '21 07:03 HenryWong-fe

Hi, I encounter 'unknown variable dynamic import' error when build for production. However, it is ok when dev env. Is there any configuration needed for dynamic import? There are little about this in the doc

softboy99 avatar Mar 02 '21 09:03 softboy99

There are too few error messages to locate accurately。I found a related bug in issue, but it has been fixed,It's not clear what caused the build error。

HenryWong-fe avatar Mar 02 '21 09:03 HenryWong-fe

Hi, I encounter 'unknown variable dynamic import' error when build for production. However, it is ok when dev env. Is there any configuration needed for dynamic import? There are little about this in the doc

Did you solve this issue?

ilmcraft avatar Mar 25 '21 15:03 ilmcraft

Hi, I encounter 'unknown variable dynamic import' error when build for production. However, it is ok when dev env. Is there any configuration needed for dynamic import? There are little about this in the doc

Did you solve this issue?

Hi, add "@rollup/plugin-dynamic-import-vars" to your devdependencies and configure it in your vite.config.js, fix it.

vite.config:

build: { rollupOptions: { plugins: [ dynamicImportVars() ], }, },

softboy99 avatar Mar 26 '21 01:03 softboy99

Hi, I encounter 'unknown variable dynamic import' error when build for production. However, it is ok when dev env. Is there any configuration needed for dynamic import? There are little about this in the doc

Did you solve this issue?

Hi, add "@rollup/plugin-dynamic-import-vars" to your devdependencies and configure it in your vite.config.js, fix it.

vite.config:

build: { rollupOptions: { plugins: [ dynamicImportVars() ], }, },

Added in vite config file as you have suggested but no luck when deployed to the server. It's giving the same error! I have attached both code and error. Please check and suggest if possible. Thanks

image

image

ilmcraft avatar Mar 26 '21 05:03 ilmcraft

@rollup/plugin-dynamic-import-vars is already included in Vite. The repro is too big to test a particular issue and it is currently outdated. Please open a new issue with a minimal reproduction against the latest Vite version if you still have a problem.

patak-cat avatar Mar 28 '21 21:03 patak-cat

This is possibly caused because you did not load the vue plugin in vite: https://github.com/vitejs/vite/tree/main/packages/plugin-vue

My guess is that without it the SFC is not parsed correctly and top level imports are wrongly handled as dynamic imports, thus causing the error. Loading the plugin fixed the problem for me.

stefanvanherwijnen avatar Apr 08 '21 14:04 stefanvanherwijnen

I ran into the same problem, but the difference is that I ran the react app.

image

xinyao27 avatar Apr 10 '21 11:04 xinyao27

This can also happen if you are using Typescript and building with "vue-tsc --noEmit && vite build" but you've somehow managed to emit a JS copy of vite.config.ts to disk. Check for vite.config.js, delete and rerun.

nukefusion avatar Apr 14 '21 19:04 nukefusion

Hi, I encounter 'unknown variable dynamic import' error when build for production. However, it is ok when dev env. Is there any configuration needed for dynamic import? There are little about this in the doc

Did you solve this issue?

Hi, add "@rollup/plugin-dynamic-import-vars" to your devdependencies and configure it in your vite.config.js, fix it. vite.config: build: { rollupOptions: { plugins: [ dynamicImportVars() ], }, },

Added in vite config file as you have suggested but no luck when deployed to the server. It's giving the same error! I have attached both code and error. Please check and suggest if possible. Thanks

image

image

Hi,I have the same error and the suggestions above doesn't work for me. Did you fixed it?

CocaColf avatar Apr 16 '21 02:04 CocaColf

I'm facing the same issue too while running npm run build

DhvanilPatel avatar Apr 21 '21 08:04 DhvanilPatel

I'm facing the same issue too while running npm run build

LauJon avatar May 26 '21 03:05 LauJon

Same issue - has anyone found a solution?

It happens when I upgrade from [email protected] -> [email protected]

callmeberzerker avatar May 27 '21 08:05 callmeberzerker

I'm facing this issue as well.

const page = await import(`./pages/${name}.tsx`);

Files that are directly in the pages directly are included, but nested ones e.g. pages/user/profile.tsx are not.

Edit: this seems a limitation of rollup-plugin-dynamic-import-variables as mentioned in the readme.

robertvansteen avatar May 27 '21 11:05 robertvansteen

Same here on latest Vite while running npm run build.

birkhofflee avatar Jun 11 '21 04:06 birkhofflee

If you happen to be running into this with Svelte, a workaround was suggested here. I was getting build errors on .svelte pages that have a style tag and use an environmental variable via import.meta.env.

nstuyvesant avatar Jun 13 '21 15:06 nstuyvesant

For me, I mistakenly named my file .js instead of .jsx. It was kind enough to tell me the file causing the error.

collinsuzebu avatar Jun 17 '21 15:06 collinsuzebu

This issue has been locked since it has been closed for more than 14 days.

If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.

github-actions[bot] avatar Jul 14 '21 00:07 github-actions[bot]