《实战:如何使用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",

  2. 安装相关插件

bash 复制代码
npm i [email protected]

npm i [email protected] -D
npm i [email protected] -D
npm i [email protected] -D
npm i [email protected] -D


npm i @babel/[email protected] -D
npm i @babel/[email protected] -D
npm i @babel/[email protected] -D
npm i @babel/preset-react@7 -D
npm i @vue/[email protected] -D
npm i @vue/[email protected] -D
        
npm i [email protected]

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

复制代码
@vue/compiler-sfc
  1. 安装 webpack5
bash 复制代码
npm install [email protected] [email protected] -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

  2. 修改文件 .npmrc

    init.author.name 改为 --init-author-name
    init.author.email 改为 --init-author-email

  3. 修改文件 packages.json 的编译命令

json 复制代码
"scripts": {
	"build": "webpack --config ./webpack.config.js --progress"
},
  1. 修改文件 .babelrc
json 复制代码
{ 
  "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做如下配置:

js 复制代码
const { VueLoaderPlugin } = require('vue-loader')

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

The code generator has deoptimised the styling

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

js 复制代码
// .babelrc:
{
  "compact": false
}

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

Missing class properties transform

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

json 复制代码
{ 
  "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 下配置

json 复制代码
// .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 中配置

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

编译报错

Uncaught ReferenceError: exports is not defined

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

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

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

export {
    MyTestComponents,
    MyDirectives
}

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

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

相关推荐
初辰ge1 分钟前
做个大屏既要不留白又要不变形还要没滚动条,我直接怒斥领导,大屏适配就这四种模式
前端·javascript
Face4 分钟前
路由Vue-router 及 异步组件
前端·javascript·vue.js
Nano5 分钟前
Axios 进阶指南:掌握请求取消与进度监控的艺术
前端
工呈士5 分钟前
Context API 应用与局限性
前端·react.js·面试
ArcX5 分钟前
从 JS 到 Rust 的旅程
前端·javascript·rust
胡gh6 分钟前
深入理解React,了解React组件化,脱离”切图崽“,迈向高级前端开发师行列
前端·react.js
技术小丁7 分钟前
使用 HTML + JavaScript 实现自定义富文本编辑器开发实践(附完整代码)
前端·javascript·html
Alla T32 分钟前
【前端】缓存相关
前端·缓存
christine-rr43 分钟前
征文投稿:如何写一份实用的技术文档?——以软件配置为例
运维·前端·网络·数据库·软件构建