vite.config.js rollupOptions 导致“意外令牌”构建期间出错

发布于 2025-01-09 03:29:33 字数 3050 浏览 0 评论 0原文

我目前正在尝试将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2025-01-16 03:29:33

确保您使用 type="module" 加载生成的模块,即,而

不是:

<script src="/static/assets/index.f551f06e.js"></script>

它应该是:

<script type="module" src="/static/assets/index.f551f06e.js"></script>

Make sure that you are loading the generated module with type="module", i.e.

Instead of:

<script src="/static/assets/index.f551f06e.js"></script>

It should be:

<script type="module" src="/static/assets/index.f551f06e.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文