(插件commonjs)SyntaxError:使用rollup打包vue组件时出现意外标记

Ehs*_*san 6 javascript typescript vue.js babeljs rollupjs

用rollup打包Vue组件时,出现这样的错误:

(插件 commonjs)语法错误:意外的标记 在此输入图像描述

rollup.config.js:

import peerDepsExternal from "rollup-plugin-peer-deps-external";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
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(), commonjs(), typescript(), vue()]
};
Run Code Online (Sandbox Code Playgroud)

按钮.vue:

<template>
  <button>button</button>
</template>
<script lang="ts">
</script>
Run Code Online (Sandbox Code Playgroud)

有什么解决办法吗?

所以我找到了更好的方法:

最好使用vue-sfc-rollup来打包 Vue 组件。

2023年更新: 现在Vite很流行,你可以使用Vite Lib模式为NPM打包Vue组件。是一个可以帮助您的入门模板。

doo*_*911 6

我遇到了这样的问题。咒骂 .html 文件中的任何 html 标签<template>

plugins: [
  vue() //should be the first
  babel(),
  peerDepsExternal(),
  resolve(),
  commonjs(),
  typescript(),
],
Run Code Online (Sandbox Code Playgroud)

关注本期

  • 这应该是一个可接受的答案...在“commonjs()”之前移动“vue()”插件可以立即解决问题 (2认同)