《实战:如何使用Vue2.0开发一个npm组件库》- 6、Vue2.x 组件 webpack3 升 webpack5

升级

  1. package.json 删除冗余依赖
"extract-text-webpack-plugin": "^3.0.2",

"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"webpack-glob-entry": "^2.1.1"               
        
"babel-core": "^6.26.3",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",

"file-loader": "^1.1.6",
  1. 安装相关插件
npm i vue@2.6.14

npm i vue-template-compiler@2.6.14 -D
npm i vue-loader@15.9.8 -D
npm i file-loader@6.2.0 -D
npm i babel-loader@8.3.0 -D


npm i @babel/core@7.2.2 -D
npm i @babel/plugin-transform-runtime@7.22.5 -D
npm i @babel/preset-env@7.3.1 -D
npm i @babel/preset-react@7 -D
npm i @vue/cli-plugin-babel@5.0.6 -D
npm i @vue/cli-service@5.0.6 -D
        
npm i axios@1.6.0

注意千万不要安装,这是给vue3和vue2的兼任版本使用

@vue/compiler-sfc
  1. 安装 webpack5
npm install webpack@5.89.0 webpack-cli@5.1.4 -D
  1. 安装
npm i mini-css-extract-plugin -D
npm i css-minimizer-webpack-plugin -D
npm i javascript-obfuscator webpack-obfuscator -D
npm i style-loader@2 -D
  1. 修改文件 .npmrc
init.author.name 改为 --init-author-name
init.author.email 改为 --init-author-email
  1. 修改文件 packages.json 的编译命令
"scripts": {
   
	"build": "webpack --config ./webpack.config.js --progress"
},
  1. 修改文件 .babelrc
{
    
  "presets":[
    [
      "@babel/preset-env",
      {
    "targets": "> 1% in AU and not dead", "shippedProposals": true },
    ],
    ["@babel/preset-react", {
    "runtime": "automatic" }]
  ]
}

启动报错

VueLoaderPlugin is not a constructor。

解决方案:vue-loader 必须在 15+,且在 webpack.config.js做如下配置:

const {
    VueLoaderPlugin } = require('vue-loader')

plugins: [
    // 引入VueLoader插件
    new VueLoaderPlugin(),
],

The code generator has deoptimised the styling

**解决方案:**在项目根目录下查找文件 .babelrcbabel.config.js ,如果没有就创建一个(两者其一即可),配置如下:

// .babelrc:
{
   
  "compact": false
}

// babel.config.js:
module.exports = {
   
    compact: false,
}

Missing class properties transform

**解决方案:**在文件 .babelrcbabel.config.js` 下配置

{
    
  "presets":[
    [
      "@babel/preset-env",
      {
    "targets": "> 1% in AU and not dead", "shippedProposals": true },
    ],
    ["@babel/preset-react", {
    "runtime": "automatic" }]
  ]
}

Uncaught ReferenceError: Cannot access ‘l’ before initialization

解决方案: 在文件 .babelrcbabel.config.jswebpack.config.js 下配置

// .babel 或 babel.config.js
"plugins": [
    "@babel/plugin-transform-runtime"
]
  
// webpack.config.js
module.exports = {
   
  target: ['web', 'es5'],
}

插件 xlsx 报错

**解决方案:**如下方式引入

import * as XLSX from "xlsx";

Can’t resolve ‘[object Module]’

解决方案:字体库报错,属于资源配置错误。 文件 webpack.config.js 中配置

{
   
    test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
    type: "asset/resource", // 正确
    // loader: "file-loader"// 错误
},

编译报错

Uncaught ReferenceError: exports is not defined

解决方案:第三方参考,也可研究 babel-loader。没真正解决,因为仅在开发联调环境下报此错误。

组件库文件 index.js 导出组件,要判断当前环境是否为 production

// 合并导出
if (process.env.NODE_ENV === 'production') {
   
    Object.assign(exports, {
    MyTestComponents }, {
    MyDirectives });
}

export {
   
    MyTestComponents,
    MyDirectives
}

使用相对路径引用的图片解析后的地址不正确

**解决方案:**未解决,把小图片改为 base64 配置;

相关推荐

  1. Webpack5 升级》- Vue2.x Webpack3 5

    2023-12-08 07:14:03       41 阅读
  2. 《基于 Vue Webpack5 配置》- 总结

    2023-12-08 07:14:03       36 阅读
  3. 开发Vue

    2023-12-08 07:14:03       16 阅读
  4. Vue 3.x生命周期

    2023-12-08 07:14:03       10 阅读
  5. npm发布Vue

    2023-12-08 07:14:03       27 阅读
  6. 使用Vue3一个抽屉Drawer

    2023-12-08 07:14:03       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 07:14:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 07:14:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 07:14:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 07:14:03       20 阅读

热门阅读

  1. Glide系列-活动缓存和内存缓存

    2023-12-08 07:14:03       30 阅读
  2. 笙默考试管理系统-MyExamTest----codemirror(47)

    2023-12-08 07:14:03       27 阅读
  3. leetcode每日一题37

    2023-12-08 07:14:03       41 阅读
  4. 神经网络----网络模型的加载及保存

    2023-12-08 07:14:03       31 阅读
  5. ffmpeg学习日记619-指令-透明通道视频相关指令

    2023-12-08 07:14:03       34 阅读
  6. 低代码:美味膳食或垃圾食品?

    2023-12-08 07:14:03       38 阅读