最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (plugin commonjs) SyntaxError: Unexpected token when packaging vue component with rollup - Stack Overflow

programmeradmin0浏览0评论

When packaging a Vue ponent with rollup, this error appears:

(plugin monjs) SyntaxError: Unexpected token

rollup.config.js:

import peerDepsExternal from "rollup-plugin-peer-deps-external";
import resolve from "@rollup/plugin-node-resolve";
import monjs from "@rollup/plugin-monjs";
import typescript from "rollup-plugin-typescript2";
import vue from "rollup-plugin-vue";
import {babel} from '@rollup/plugin-babel';

import packageJson from "./package.json";

export default {
    input: "src/index.ts",
    output: [
        {
            format: "cjs",
            file: packageJson.main,
            sourcemap: true
        },
        {
            format: "esm",
            file: packageJson.module,
            sourcemap: true
        }
    ],
    plugins: [babel(), peerDepsExternal(), resolve(), monjs(), typescript(), vue()]
};

button.vue:

<template>
  <button>button</button>
</template>
<script lang="ts">
</script>

any solution?

So I found a better way:

It's better to use vue-sfc-rollup to package Vue ponents.

Update 2023: These days Vite is trendy and you can use Vite Lib mode to package Vue ponents for NPM. This is a starter template that may help you.

When packaging a Vue ponent with rollup, this error appears:

(plugin monjs) SyntaxError: Unexpected token

rollup.config.js:

import peerDepsExternal from "rollup-plugin-peer-deps-external";
import resolve from "@rollup/plugin-node-resolve";
import monjs from "@rollup/plugin-monjs";
import typescript from "rollup-plugin-typescript2";
import vue from "rollup-plugin-vue";
import {babel} from '@rollup/plugin-babel';

import packageJson from "./package.json";

export default {
    input: "src/index.ts",
    output: [
        {
            format: "cjs",
            file: packageJson.main,
            sourcemap: true
        },
        {
            format: "esm",
            file: packageJson.module,
            sourcemap: true
        }
    ],
    plugins: [babel(), peerDepsExternal(), resolve(), monjs(), typescript(), vue()]
};

button.vue:

<template>
  <button>button</button>
</template>
<script lang="ts">
</script>

any solution?

So I found a better way:

It's better to use vue-sfc-rollup to package Vue ponents.

Update 2023: These days Vite is trendy and you can use Vite Lib mode to package Vue ponents for NPM. This is a starter template that may help you.

Share Improve this question edited Feb 15, 2023 at 10:20 Ehsan asked Jul 7, 2021 at 5:45 EhsanEhsan 81910 silver badges19 bronze badges 4
  • please share a screenshot of the error – Boussadjra Brahim Commented Jul 7, 2021 at 5:54
  • Edited and added screenshot. – Ehsan Commented Jul 7, 2021 at 6:00
  • inside the script add import { defineComponent } from 'vue';export default defineComponent({}) or remove the script section – Boussadjra Brahim Commented Jul 7, 2021 at 6:02
  • Still the same error. – Ehsan Commented Jul 7, 2021 at 6:05
Add a ment  | 

2 Answers 2

Reset to default 6

I had a problem like this. Swearing at any html tag in the <template>.

plugins: [
  vue() //should be the first
  babel(),
  peerDepsExternal(),
  resolve(),
  monjs(),
  typescript(),
],

Watch this issue.

The purpose of the monjs plugin is to "convert CommonJS modules to ES6" according to the docs. Assuming you are using ESM in your source code, there's no need transpile your code with the monjs plugin. The exclude option achieves this.

E.g.

plugins: [
  // some plugins here
  monjs({
    exclude: 'src/**',
  }),
  // other plugins here
],

As a side note, the babel plugin should appear after the monjs plugin in the plugins array, as per the docs, which say:

When using @rollup/plugin-babel with @rollup/plugin-monjs in the same Rollup configuration, it's important to note that @rollup/plugin-monjs must be placed before this plugin in the plugins array for the two to work together properly.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论