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

ERROR Unexpected token (1:0) #20

Open
ghost opened this issue Jul 15, 2022 · 5 comments
Open

ERROR Unexpected token (1:0) #20

ghost opened this issue Jul 15, 2022 · 5 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@ghost
Copy link

ghost commented Jul 15, 2022

Hello,

I use vite-plugin-dynamic-import to import my .vue files into Nuxt3 (3.0.0-rc.3).

However, since version 0.9.6 and all higher versions of vite-plugin-dynamic-import, I have the following error: "ERROR Unexpected token (1:0)"

Reproduction link : https://github.com/antoineanorra/vite-plugin-dynamic-import-issue

Version >=0.9.6 not working, run this :

npm i && npm run dev

Version 0.9.5 working, run this :

npm i -D vite-plugin-dynamic-import@0.9.5 && npm run dev
@cram0
Copy link

cram0 commented Jul 15, 2022

The same is happening to me, it would be cool to have a fix for it.

@caoxiemeihao
Copy link
Contributor

caoxiemeihao commented Jul 16, 2022

Below is the order in which plugins actually run, this behavior is not freely controllable in Nuxt. :(

[ 
  'vite:pre-alias',
  'alias',
  'nuxt:vite-node-server',
  'nuxt:import-protection',
  'vite:modulepreload-polyfill',
  'vite:resolve',
  'vite:optimized-deps',
  'vite:html-inline-proxy',
  'vite:css',
  'vite:esbuild',
  'vite:json',
  'vite:wasm',
  'vite:worker',
  'vite:asset',
  'replace',
  'virtual',
  // 🐞 This should come after `vite:vue` and `vite:vue-jsx`
  'vite-plugin-dynamic-import',
  'test-plugin-order',
  'nuxt:cache-dir',
  'vite:vue',
  'vite:vue-jsx',
  'nuxt:vite-relative-asset',
  'vite:define',
  'vite:css-post',
  'vite:worker-import-meta-url',
  'nuxt:composable-keys',
  'nuxt:dynamic-base-path',
  'nuxt:dev-style-ssr',
  'unctx:transfrom',
  'nuxt:components-loader',
  'nuxt:auto-imports-transform',
  'vite:client-inject',
  'vite:import-analysis'
]

You can see the plugin execution order in the following way.

export default defineNuxtConfig({
    vite: {
        plugins: [
            dynamicImport(),
            {
                name: 'view-plugin-order',
                configResolved(conf) {
                    console.log(conf.plugins.map(p => p.name));
                },
            },
        ],
    }
})

Maybe we can force the order of plugins to achieve our purpose.

  1. npm i -D vite-plugin-utils
  2. configure the plugin into nuxt.config.ts
+ import { sortPlugin } from 'vite-plugin-utils'

export default defineNuxtConfig({
    vite: {
        plugins: [
-           dynamicImport(),
+           sortPlugin({
+               plugin: dynamicImport(),
+               names: ['vite:vue', 'vite:vue-jsx'],
+               enforce: 'post',
+           }),
        ],
    }
})

We will got the following plugins order.

[
  'vite:pre-alias',
  'alias',
  'nuxt:import-protection',
  'vite:modulepreload-polyfill',
  'vite:resolve',
  'vite:optimized-deps',
  'vite:html-inline-proxy',
  'vite:css',
  'vite:esbuild',
  'vite:json',
  'vite:wasm',
  'vite:worker',
  'vite:asset',
  'replace',
  'virtual',
  'view-plugin-order',
  'nuxt:cache-dir',
  'nuxt:vite-relative-asset',
  'vite:vue',
  'vite:vue-jsx',
  // 🎉 Work normally
  'vite-plugin-dynamic-import',
  'vite:define',
  'vite:css-post',
  'vite:worker-import-meta-url',
  'nuxt:composable-keys',
  'nuxt:dynamic-base-path',
  'unctx:transfrom',
  'nuxt:components-loader',
  'nuxt:auto-imports-transform',
  'vite:client-inject',
  'vite:import-analysis'
]

image

@caoxiemeihao
Copy link
Contributor

Since v0.9.6 and later, I have removed the sortPlugin in the plugin. At the moment, this may be the wrong decision. 😅

db14977

image

@caoxiemeihao caoxiemeihao added bug Something isn't working documentation Improvements or additions to documentation labels Jul 16, 2022
@ghost
Copy link
Author

ghost commented Jul 16, 2022

I confirm that everything works thanks to your solution, thank you @caoxiemeihao !

@noel-schenk
Copy link

Still works but now the plugins was updated:
This is also the solution if your alias is not working.

// https://nuxt.com/docs/api/configuration/nuxt-config

import dynamicImport from "vite-plugin-dynamic-import";
import { sort } from "vite-plugin-utils/sort-plugin";

export default defineNuxtConfig({
  vite: {
    plugins: [
      sort({
        plugin: dynamicImport(),
        names: ["vite:vue", "vite:vue-jsx"],
        enforce: "post",
      }),
    ],
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants