vite.config.js rollupOptions 导致“意外令牌”构建期间出错
我目前正在尝试将我的 laravel 项目从 laravel-mix 切换到 Vitejs 之类的项目。我按照此处提供的教程进行操作: https://owenconti.com/posts/replacing- laravel-mix-with-vite。对此的任何帮助将不胜感激。谢谢!
当尝试运行 npm run production 时,我收到以下错误:
Unexpected token
file: /Users/celsoluiz81/Sites/batatolandia/node_modules/vite/dist/node/chunks/dep-971d9e33.js:56066:60
56064: const pendingModules = new Map();
56065: const pendingImports = new Map();
56066: async function ssrLoadModule(url, server, context = { global }, urlStack = []) {
^
56067: url = unwrapId$1(url).replace(NULL_BYTE_PLACEHOLDER, '\0');
56068: // when we instantiate multiple dependency modules in parallel, they may
error during build:
Error: Unexpected token
at error (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:159:30)
at Module.error (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12437:16)
at Module.tryParse (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12813:25)
at Module.setSource (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12716:24)
at ModuleLoader.addModuleSource (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:22191:20)
[!] Error: unfinished hook action(s) on exit:
(commonjs) load "\u0000/Users/celsoluiz81/Sites/batatolandia/node_modules/vite/dist/node/chunks/dep-971d9e33.js?commonjs-proxy"
经过一番挖掘,我能够将错误隔离到 vite.config.js 中的这一行,更具体地说是 rollupOptions 部分:
build: {
outDir: resolve(__dirname, 'public/dist'),
emptyOutDir: true,
manifest: true,
target: 'es2018',
rollupOptions: {
input: '/js/app.js'
}
},
当我删除 rollupOptions 部分时,我能够成功编译。
我的设置
- vite v2.8.4
- vue 3.2.31
vite.config.js
import vue from '@vitejs/plugin-vue';
const { resolve } = require('path');
const Dotenv = require('dotenv');
Dotenv.config();
const ASSET_URL = process.env.ASSET_URL || '';
export default {
plugins: [
vue(),
],
root: 'resources',
base: `${ASSET_URL}/dist/`,
server: {
strictPort: true,
port: 3000
},
build: {
outDir: resolve(__dirname, 'public/dist'),
emptyOutDir: true,
manifest: true,
target: 'es2018',
rollupOptions: {
input: '/js/app.js'
}
},
resolve: {
alias: {
'@': '/js',
}
},
optimizeDeps: {
include: [
'vue',
'axios'
]
}
}
app.js
import 'vite/modulepreload-polyfill'
import './bootstrap';
/**
* Vue
*/
import VueApp from './modules/main.module';
if (document.getElementById('app')) {
VueApp();
}
I am currently trying to switch my laravel project from laravel-mix to something like Vitejs. I followed the tutorial available here: https://owenconti.com/posts/replacing-laravel-mix-with-vite. Any help with this would be truly appreciated. Thanks!
When trying to run npm run production I'm getting the following error:
Unexpected token
file: /Users/celsoluiz81/Sites/batatolandia/node_modules/vite/dist/node/chunks/dep-971d9e33.js:56066:60
56064: const pendingModules = new Map();
56065: const pendingImports = new Map();
56066: async function ssrLoadModule(url, server, context = { global }, urlStack = []) {
^
56067: url = unwrapId$1(url).replace(NULL_BYTE_PLACEHOLDER, '\0');
56068: // when we instantiate multiple dependency modules in parallel, they may
error during build:
Error: Unexpected token
at error (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:159:30)
at Module.error (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12437:16)
at Module.tryParse (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12813:25)
at Module.setSource (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:12716:24)
at ModuleLoader.addModuleSource (/Users/celsoluiz81/Sites/batatolandia/node_modules/rollup/dist/shared/rollup.js:22191:20)
[!] Error: unfinished hook action(s) on exit:
(commonjs) load "\u0000/Users/celsoluiz81/Sites/batatolandia/node_modules/vite/dist/node/chunks/dep-971d9e33.js?commonjs-proxy"
After some digging around, I was able to isolate the error to this line in my vite.config.js, more specifically the rollupOptions part:
build: {
outDir: resolve(__dirname, 'public/dist'),
emptyOutDir: true,
manifest: true,
target: 'es2018',
rollupOptions: {
input: '/js/app.js'
}
},
When I remove the rollupOptions sections, I am able to compile successfully.
My setup
- vite v2.8.4
- vue 3.2.31
vite.config.js
import vue from '@vitejs/plugin-vue';
const { resolve } = require('path');
const Dotenv = require('dotenv');
Dotenv.config();
const ASSET_URL = process.env.ASSET_URL || '';
export default {
plugins: [
vue(),
],
root: 'resources',
base: `${ASSET_URL}/dist/`,
server: {
strictPort: true,
port: 3000
},
build: {
outDir: resolve(__dirname, 'public/dist'),
emptyOutDir: true,
manifest: true,
target: 'es2018',
rollupOptions: {
input: '/js/app.js'
}
},
resolve: {
alias: {
'@': '/js',
}
},
optimizeDeps: {
include: [
'vue',
'axios'
]
}
}
app.js
import 'vite/modulepreload-polyfill'
import './bootstrap';
/**
* Vue
*/
import VueApp from './modules/main.module';
if (document.getElementById('app')) {
VueApp();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您使用
type="module"
加载生成的模块,即,而不是:
它应该是:
Make sure that you are loading the generated module with
type="module"
, i.e.Instead of:
It should be: