'vite.config.js rollupOptions leads to "Unexpected token" error during build

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();
}





Solution 1:[1]

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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gajus