rollup打包vue文件报错[!] (plugin commonjs--resolver) RollupError:xxxx,src/test.vue?vue&type=template&id=0f

在这里插入图片描述
rollup打包vue文件报错,如上图。
rollup.config.dev.js中已经配置了解析vue相关插件,但是还是报错
报错原因及解决方案
plugin有序,插件引入顺序有误,vue插件需要放到commonjs插件之前。
在这里插入图片描述

vue相关插件

  • rollup-plugin-vue
  • @vue/compiler-sfc
  • rollup-plugin-postcss
  • sass

相关配置文件完整代码如下:
rollup.config.dev.js

const path = require('path'); // 引入path模块
const resolve = require('@rollup/plugin-node-resolve'); // 告诉 Rollup 如何查找外部模块
const commonjs = require('@rollup/plugin-commonjs'); // 将CommonJS模块转换为 ES2015 供 Rollup 处理
const babel = require('@rollup/plugin-babel'); // rollup 的 babel 插件,ES6转ES5
const json = require('@rollup/plugin-json'); // rollup 的 json 插件,读取json文件数据
const vue = require('rollup-plugin-vue'); // rollup 的 vue 插件,处理vue文件
const postcss = require('rollup-plugin-postcss'); // rollup 的 postcss 插件,处理css文件
// const terser = require('@rollup/plugin-terser'); // 压缩打包后的文件

const inputPath = path.resolve(__dirname, 'src/index.js'); // 入口文件地址
const outputUmdPath = path.resolve(__dirname, 'dist/bundle.js'); // umd格式打包后文件地址
const outputEsPath = path.resolve(__dirname, 'dist/bundle.es.js'); // es格式打包后文件地址
const outputCjsPath = path.resolve(__dirname, 'dist/bundle.cjs.js'); // cjs格式打包后文件地址


module.exports ={
    input: inputPath, // 入口文件
    output: [{ // 输出文件
        file: outputUmdPath, // umd格式打包后文件地址
        format: 'umd', // umd格式
        name: 'bundle', // 打包后的全局变量名
        globals: {
            vue: 'Vue'
        }
    },{
        file: outputEsPath,
        format: 'es',
        name: 'bundle',
        globals: {
            vue: 'Vue'
        }
    },{
        file: outputCjsPath,
        format: 'cjs',
        name: 'bundle',
        globals: {
            vue: 'Vue'
        }
    }],
    plugins: [
        vue(), // 处理vue文件
        resolve({
            extensions: ['.js', '.jsx', '.json', '.vue']
        }), // 告诉 Rollup 如何查找外部模块
        commonjs(), // 将CommonJS模块转换为 ES2015 供 Rollup 处理
        babel({
            exclude: 'node_modules/**', // 排除node_modules下的文件
            babelHelpers: 'runtime', // 配置runtime,不设置会报错
        }), // rollup 的 babel 插件,ES6转ES5
        json(), // rollup 的 json 插件,读取json文件数据
        postcss({
            extract: true, // 提取css
            minimize: true, // 压缩css
            plugins: [] // postcss插件
        }), // 处理css文件
        // terser() // 压缩打包后的文件
    ],
    external: ['vue'] // 告诉 Rollup 不要将此vue打包,而作为外部依赖
};

package.json

{
  "name": "cx-vue-datav",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "rollup -w -c rollup.config.dev.js",
    "build": "rollup -c rollup.config.prod.js"
  },
  "author": "jieyucx",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.23.0",
    "@babel/plugin-external-helpers": "^7.22.5",
    "@babel/plugin-transform-runtime": "^7.22.15",
    "@babel/preset-env": "^7.22.20",
    "@rollup/plugin-babel": "^6.0.4",
    "@rollup/plugin-commonjs": "^25.0.5",
    "@rollup/plugin-json": "^6.0.1",
    "@rollup/plugin-node-resolve": "^15.2.2",
    "@rollup/plugin-terser": "^0.4.4",
    "@vue/compiler-sfc": "^3.3.4",
    "rollup": "^4.0.2",
    "rollup-plugin-postcss": "^4.0.2",
    "rollup-plugin-vue": "^6.0.0",
    "sass": "^1.69.0"
  },
  "dependencies": {
    "@babel/runtime": "^7.23.1",
    "@babel/runtime-corejs3": "^7.23.1"
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值