webpack3到4的遷移

webpack4大幅度減少配置成本浮声。為了優(yōu)化打包流程和打包效率礁叔,決定升級(jí)新版本,擁抱新技術(shù)

計(jì)劃步驟:

  1. 從現(xiàn)有的一個(gè)項(xiàng)目,找到每個(gè)配置的作用是什么谍肤,以及新舊版本的區(qū)別啦租,然后制定修改方案,同時(shí)埋好日志輸出荒揣,便于排查錯(cuò)誤
  2. 打包測(cè)試篷角,修正問(wèn)題

找到每個(gè)配置的作用是什么,以及新舊版本的區(qū)別系任,然后制定修改方案恳蹲,同時(shí)埋好日志輸出,便于排查錯(cuò)誤

為防止不同npm包版本兼容有問(wèn)題俩滥,我們把所有涉及到的包都升級(jí)到最新

先把"webpack": "3.6.0"嘉蕾,升級(jí)到4.39.3

打開項(xiàng)目根目錄下package.jsonscripts下的dev開始分析

webpack-dev-server --config build/webpack.dev.conf.js // 通過(guò)webpack-dev-server啟動(dòng)的本地服務(wù)∷桑看到最新版本webpack-dev-server错忱,需要webpack4,索性把webpack-dev-server升級(jí)到最新版颁糟,把原來(lái)的 "webpack-dev-server": "2.9.1", 改成 "3.8.0" 

分析build/webpack.dev.conf.js文件

引入的插件

const merge = require('webpack-merge') // "webpack-merge": "4.1.0" 升級(jí)到 "4.2.2"
const HtmlWebpackPlugin = require('html-webpack-plugin') // "html-webpack-plugin": "2.30.1" 升級(jí)到 "3.2.0"
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') // 最新版不用動(dòng)
const portfinder = require('portfinder') // "portfinder": "1.0.13" 升級(jí) "1.0.23"

引入其他文件const utils = require('./utils')

webpack4航背,ExtractTextPlugin廢棄了,需要換成MiniCssExtractPlugin

const ExtractTextPlugin = require('extract-text-webpack-plugin') // 用于提取css文件 webpack4 不需要用這個(gè)插件棱貌, 換成 mini-css-extract-plugin
// 在工程里搜索 extract-text-webpack-plugin 發(fā)現(xiàn)可能有用到的文件或者包有:
1. /build/utils.js
// const ExtractTextPlugin = require('extract-text-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
if (options.extract) {
      // return ExtractTextPlugin.extract({
      //   use: loaders,
      //   fallback: 'vue-style-loader'
      // })
      return [
        {
          loader: MiniCssExtractPlugin.loader
        },
        'css-loader'
      ] 
 } 
2. /build/webpack.prod.conf.js
// new ExtractTextPlugin({
new MiniCssExtractPlugin({
3. 包 avg // 未在package.json中出現(xiàn)玖媚,不做處理
4. 包 async // 未在package.json中出現(xiàn),不做處理
5. 包 css-loader // "css-loader": "0.28.0", 升級(jí)到 "3.2.0"
6. 包 element-ui // "element-ui": "2.3.2", 升級(jí)到2.11.1
7. 包 html-webpack-plugin // 已經(jīng)升級(jí)過(guò)
8. 包 loader-utils // 未在package.json中出現(xiàn)婚脱,不做處理
9. 包 optimize-css-assets-webpack-plugin // "3.2.0",升級(jí)到5.0.3
10. 包 postcss-loader // "postcss-loader": "2.0.8", 升級(jí)到3.0.0
11. 包 sass-loader // 6.0.6今魔,升級(jí)到7.3.1
12. 包 schema-utils // 未在package.json中出現(xiàn),不做處理
13. 包 svg-sprite-loader // 3.7.3 升級(jí)到4.1.6
14. 包 tabel // 未在package.json中出現(xiàn)障贸,不做處理 
15. 包 ajv // 未在package.json中出現(xiàn)错森,不做處理 
16. 包 ve-calendar // 沒(méi)有新版本暫時(shí)不處理
17. 包 vue-loader // 13.3.0 升級(jí)到15.7.1
18. 包 vue-resize-directive // 未在package.json中出現(xiàn),不做處理 
19. 包 webpack-sources // 未在package.json中出現(xiàn)篮洁,不做處理 

引入其他文件const config = require('../config')

根據(jù)不同環(huán)境:develop或者production等采取不同的配置涩维,可能一些配置和webpack4不一致,暫定不需修改袁波。

引入其他文件const baseWebpackConfig = require('./webpack.base.conf')

引入文件 vue-loader.conf瓦阐,用于vue-loader需要的配置,暫定不需修改篷牌。

分析build/webpack.dev.conf.js文件配置的參數(shù)

暫定不需修改

打包測(cè)試睡蟋,修正問(wèn)題

安裝npm包

刪掉node_modules目錄

執(zhí)行npm i

npm WARN element-ui@2.11.1 requires a peer of vue@^2.5.17 but none is installed. You must install peer dependencies yourself.

提示element-ui需要vue@2.5.17,將vue升級(jí)到2.6.10枷颊。重新npm i

npm WARN babel-loader@7.1.1 requires a peer of webpack@2 || 3 but none is installed. You must install peer dependencies yourself.
npm WARN file-loader@1.1.4 requires a peer of webpack@^2.0.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN uglifyjs-webpack-plugin@1.1.1 requires a peer of webpack@^2.0.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.

升級(jí)babel-loader到8.0.6戳杀,升級(jí)file-loader到4.2.0该面,webpack4.0已經(jīng)不用uglifyjs-webpack-plugin,直接刪掉信卡。重新npm i

npm WARN babel-loader@8.0.6 requires a peer of @babel/core@^7.0.0 but none is installed. You must install peer dependencies yourself.

升級(jí)babel-core@babel/core] 7.5.5重新npm i

測(cè)試

npm run dev

The CLI moved into a separate package: webpack-cli
Please install 'webpack-cli' in addition to webpack itself to use the CLI
# 安裝webpack-cli 
npm install webpack-cli --save-dev

重新npm run dev

ERROR  Failed to compile with 1 errors                             
Module build failed (from ./node_modules/eslint-loader/index.js):
TypeError: Cannot read property 'eslint' of undefined at Object.module.exports (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-loader/index.js:155:18)

eslint-loader 1.7.1升級(jí)到3.0.0, 重新npm i

npm WARN eslint-loader@3.0.0 requires a peer of eslint@^5.0.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.

eslint 3.19.0升級(jí)到6.2.2, 重新npm i

npm WARN eslint-plugin-import@2.7.0 requires a peer of eslint@2.x - 4.x but none is installed. You must install peer dependencies yourself.
npm WARN acorn-jsx@5.0.2 requires a peer of acorn@^6.0.0 || ^7.0.0 but none is installed. You must install peer dependencies yourself.

eslint-plugin-import 2.7.0升級(jí)到2.18.2隔缀。

通過(guò)命令npm ls查看整個(gè)包依賴樹,

webpack-bundle-analyzer 引用了acorn@5.7.3坐求,所以把webpack-bundle-analyzer2.9.0升級(jí)到3.4.1

另外一個(gè)引用jest@21.2.0 -> jest-cli -> jest-environment-jsdom -> jsdom -> acorn@4.0.13 所有把jest@21.2.0升級(jí)到24.9.0

重新npm i

還是有警告

npm WARN acorn-jsx@5.0.2 requires a peer of acorn@^6.0.0 || ^7.0.0 but none is installed. You must install peer dependencies yourself.

依賴樹中看到

│ │ ├── UNMET PEER DEPENDENCY acorn@7.0.0
│ │ ├── acorn-jsx@5.0.2

只好手動(dòng)安裝acorn最新版本

npm install acorn --save-dev

重新npm i

沒(méi)有警告

npm run dev

Module build failed (from ./node_modules/eslint-loader/dist/cjs.js):
TypeError: Cannot read property 'forEach' of undefined
    at Linter.parseResults (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-loader/dist/Linter.js:121:13)
    at Linter.printOutput (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-loader/dist/Linter.js:85:26)
    at Object.loader (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-loader/dist/index.js:26:10)

表面上看不出這個(gè)錯(cuò)誤導(dǎo)致是怎么導(dǎo)致的蚕泽。在/node_modules/eslint-loader/dist/Linter.js的lint(content)函數(shù)中輸出捕獲的錯(cuò)誤.

Failed to load plugin 'html' declared in '.eslintrc.js': eslint-plugin-html error: It seems that eslint is not loaded. If you think it is a bug, please file a report at https://github.com/BenoitZugmeyer/eslint-plugin-html/issues
    at iterateESLintModules (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-plugin-html/src/index.js:79:11)
    at Object.<anonymous> (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-plugin-html/src/index.js:21:1)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at ConfigArrayFactory._loadPlugin (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/cli-engine/config-array-factory.js:861:49)

eslint-plugin-html從3.0.0升級(jí)到6.0.0

npm i

成功升級(jí)eslint-plugin-html

npm run dev

 Cannot find module 'escope'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:613:15)
    at monkeypatch (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/babel-eslint/index.js:53:26)
    at Object.exports.parse (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/babel-eslint/index.js:358:5)
    at parse (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/linter/linter.js:625:29)
    at Linter._verifyWithoutProcessors (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/linter/linter.js:1067:33)
    at Linter.(anonymous function) (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint-plugin-html/src/index.js:151:21)
    at Linter._verifyWithConfigArray (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/linter/linter.js:1210:21)
    at Linter.verify (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/linter/linter.js:1165:25)
    at Linter.verifyAndFix (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/linter/linter.js:1355:29)
    at verifyText (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/eslint/lib/cli-engine/cli-engine.js:231:48)

babel-eslint版本也比較老,7.1.1換成 10.0.3

npm i

成功升級(jí)babel-eslint

npm run dev

Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: .plugins[1][1] must be an object, false, or undefined
    at assertPluginItem (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/option-assertions.js:244:15)
    at arr.forEach (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/option-assertions.js:222:30)
    at Array.forEach (<anonymous>)
    at assertPluginList (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/option-assertions.js:222:9)
    at Object.keys.forEach.key (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/options.js:107:5)
    at Array.forEach (<anonymous>)
    at validateNested (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/options.js:83:21)
    at validate (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/validation/options.js:74:10)
    at file (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-chain.js:174:34)
    at cachedFunction (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/caching.js:33:19)

/node_modules/@babel/core/lib/config/validation/options.js添加日志輸出

console.log('optLoc--------', optLoc)
console.log('opts--------', opts)
console.log('key-----', key)
    validator(optLoc, opts[key]);
// 報(bào)錯(cuò)的前的輸出是
optLoc-------- { type: 'option',
  name: 'plugins',
  parent: { type: 'root', source: 'babelrcfile' } }
opts-------- { presets: [ [ 'env', [Object] ], 'stage-2' ],
  plugins: [ 'transform-runtime', [ 'component', [Array] ] ],
  env: { test: { presets: [Array], plugins: [Array] } } }
key----- plugins
// 是babelr配置文件出問(wèn)題了桥嗤,這里應(yīng)該是babel修改配置規(guī)則了,plugins里的元素不能是數(shù)組了

修改.babelrc文件

"plugins": ["transform-runtime", ["component", [
  {
    "libraryName": "element-ui"
  }
]]]
改為    
"plugins": ["transform-runtime", "component": [
  {
    "libraryName": "element-ui"
  }
]]

npm run dev

Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions. In /Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/babel-preset-stage-2/lib/index.js
    at createDescriptor (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
    at items.map (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPresetDescriptors (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
    at presets (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
    at mergeChainOpts (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-chain.js:320:26)
    at /Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-chain.js:283:7
    at buildRootChain (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/config-chain.js:120:22)
    at loadPrivatePartialConfig (/Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/@babel/core/lib/config/partial.js:85:55)

先將babel-preset-stage-2從6.22.0升級(jí)到6.24.1

npm i

成功升級(jí)babel-preset-stage-2

npm run dev

一樣的錯(cuò)誤

/@babel/core/lib/config/config-chain.js283行添加打印日志

console.log('chain--',  chain)
console.log('op--',  op)
      mergeChainOpts(chain, op);
// 報(bào)錯(cuò)前輸出
chain-- { options: [], presets: [], plugins: [] }
op-- { options:
   { presets: [ [Array], 'stage-2' ],
     plugins: [ 'transform-runtime', [Object] ],
     env: { test: [Object] } },
  plugins: [Function: plugins],
  presets: [Function: presets] }

babel-plugin-transform-runtime@6.22.0更新為@babel/plugin-transform-runtime@7.5.5

.babelrc文件中

"plugins": ["transform-runtime", {
 改為
"plugins": ["@babel/plugin-transform-runtime", {

npm i

這里一直報(bào)babel的錯(cuò)仔蝌,把babel的相關(guān)包都做下升級(jí)

"babel-plugin-dynamic-import-node": "1.2.0", => 2.3.0
"babel-plugin-transform-es2015-modules-commonjs": "6.26.0", => 6.26.2
"babel-preset-env": "1.3.2", => 1.7.0
"babel-register": "6.22.0", => 6.26.0
"babel-plugin-component": "0.10.1", => 1.1.1

npm i

npm run dev 一樣的錯(cuò)泛领,在網(wǎng)上找到有將babel-preset-env換成 @babel/preset-env,試下

"babel-preset-env": "1.7.0", => "@babel/preset-env" : "7.5.5",
"babel-register": "6.26.0",  => "@babel/register" : "7.5.5", // 一起改掉
"babel-polyfill": "6.26.0", => "@babel/polyfill" : "7.4.4", // 一起改掉

npm i

npm run dev 依然報(bào)錯(cuò)

臨時(shí)把.babelrc文件簡(jiǎn)化

{
   "presets": ["@babel/preset-env"]
}

npm run dev

報(bào)其他錯(cuò)誤敛惊。

先做下相關(guān)npm包升級(jí)

"vue-template-compiler": "2.5.2", => "2.6.10",
"vue-style-loader": "3.0.1",  => "@babel/register" : "4.1.2", 

依然報(bào)錯(cuò)

有個(gè)老兄說(shuō) webpack4 不兼容vue-loader 15.x.x版本https://www.cnblogs.com/tugenhua0707/p/9695179.html

我也把vue-loader 降低成^14.2.2

之前錯(cuò)誤沒(méi)了渊鞋,只有eslint的錯(cuò)誤

暫時(shí)將/config/index.js中的useEslint設(shè)置成false,

npm run dev

終于跑起來(lái)了

/config/index.js中的useEslint設(shè)置成true

eslint相關(guān)的包升級(jí)為最新

"eslint-config-standard": "10.2.1", => "14.1.0",
"eslint-friendly-formatter": "3.0.0",  => "4.0.1", 
"eslint-plugin-node": "5.2.0", => "10.0.0",
"eslint-plugin-promise": "3.5.0",  => "4.2.1", 
"eslint-plugin-standard": "3.0.1",  => "4.0.1", 

npm i

npm run dev 依然報(bào)錯(cuò),猜測(cè)可能和簡(jiǎn)化.babelrc有關(guān)系

把babelrc中的配置一點(diǎn)點(diǎn)加回來(lái)

{
   "presets": [["@babel/preset-env", {"modules": false}], "stage-2"]
}

npm run dev 依然報(bào)錯(cuò)

查到babel7 stage-2的文章 https://babeljs.io/docs/en/babel-preset-stage-2 發(fā)現(xiàn)babel-preset-stage-2插件需要更換

"babel-preset-stage-2": "6.24.1",  => "@babel/preset-stage-2": "7.0.0", 

npm i

babelrc中的配置修改

{
   "presets": [["@babel/preset-env", {"modules": false}], "@babel/preset-stage-2"]
}

npm run dev , 提示As of v7.0.0-beta.55, we've removed Babel's Stage presets.發(fā)現(xiàn)babel7已經(jīng)移除了stage preset https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets

babelrc中去掉@babel/preset-stage-2 并添加pluginx component的結(jié)構(gòu)做了點(diǎn)調(diào)整

{
  "presets": [["@babel/preset-env", {"modules": false}]],
  "plugins": [
    "@babel/plugin-transform-runtime", 
    [ 
      "component",
      {
        "libraryName": "element-ui"
      }
    ]
  ]
}

npm run dev

# 提示錯(cuò)誤信息
These dependencies were not found:
* @babel/runtime/helpers/defineProperty in ./src/router/index.js
...

添加@babel/runtime

"@babel/runtime": "^7.6.0"

npm i

npm run dev

# 提示錯(cuò)誤信息
These dependencies were not found:
* element-ui/lib/alert/style.css in ./src/element-ui/index.js
* element-ui/lib/aside/style.css in ./src/element-ui/index.js
...

element包導(dǎo)入有問(wèn)題瞧挤,babelrc需要添加styleLibraryName用來(lái)加載樣式文件

{
  "presets": [["@babel/preset-env", {"modules": false}]],
  "plugins": [
    "@babel/plugin-transform-runtime",
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

npm run lint(lint配置在package中)

還是報(bào)錯(cuò)

http://eslint.org/docs/rules/dot-notation  ["children"] is better written in dot notation  
  src/utils/index.js:36:31
...
    1 | <template>
  2 |   <div class="mod-user">
> 3 |     <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
    |                     ^
  4 |       <el-form-item>
  5 |         <el-input v-model="dataForm.userName" placeholder="用戶名" clearable></el-input>
  6 |       </el-form-item>

首先先 將 --fix 添加到package中的lint腳步中

"scripts": {
  ...
  "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs --fix",
    ...
},

npm run lint

只剩下vue lint 相關(guān)的錯(cuò)誤

看到.eslintrc.js中锡宋,plugins:[ 'html'] 上有注釋// required to lint *.vue files 查了下eslint-plugin-html插件

發(fā)現(xiàn)

Linting VUE files
Initially, eslint-plugin-vue was using eslint-plugin-html to lint code inside script tags. Since v3, eslint-plugin-vue is using its own parser, so it is incompatible with eslint-plugin-html. You should use eslint-plugin-vue exclusively and remove eslint-plugin-html from your dependencies if you still have it.

原來(lái)用 eslint-plugin-html插件就可以檢測(cè)vue文件,現(xiàn)在更換最新的eslint-plging-vue

// 刪除
"eslint-plugin-html"
// 添加
"eslint-plugin-vue": "^5.2.3",

參考https://vuejs.github.io/eslint-plugin-vue/user-guide/#faq

.eslintrc.js修改plugin

  "plugins": [
    "vue",
-   "html"
  ]

.eslintrc.js修改extends

  extends: ['plugin:vue/essential', 'standard'],

.eslintrc.js修改parserOptions 添加 parser: 'babel-eslint' 這個(gè)是為了給eslint-plugin-vue提供解析 .vue文件中<script>標(biāo)簽中的代碼特恬,因?yàn)?code>eslint-plugin-vue的解析器只解析<template>標(biāo)簽里的內(nèi)容

去掉原來(lái)的parser

- "parser": "babel-eslint",
  parserOptions: {
    sourceType: 'module',
      parser: 'babel-eslint',
  },

npm run lint

僅剩下一些defined but never used的錯(cuò)誤执俩,逐個(gè)更正

npm run lint 成功

.babelrc中添加完剩下的信息

之前的evn配置

"env": {
  "test": {
    "presets": ["env", "stage-2"],
      "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"]
  }
}

但是并沒(méi)有地方使用。

babel會(huì) 從按這個(gè)順序找到一個(gè)環(huán)境變量值process.env.BABEL_ENV || process.env.NODE_ENV || "development"

然后用這個(gè)值做為key癌刽,在上面的env配置中找到信息役首,合并到根對(duì)象中指定的選項(xiàng)之上。

但是項(xiàng)目中并沒(méi)有設(shè)置 process.env.BABEL_ENV || process.env.NODE_ENV 的地方显拜,所以不會(huì)解析test里的內(nèi)容衡奥,除非程序運(yùn)行所在電腦上配置BABEL_ENV 環(huán)境變量為test

如果改成新版的形式: 因?yàn)椴恍枰付?stage的preset 所以去掉preset。 plugins里的 transform-es2015-modules-commonjs 改成 @babel/plugin-transform-modules-commonjs远荠,dynamic-import-node不需要改矮固。但是暫時(shí)不用test這些,整個(gè)env配置都先去掉譬淳,后面要在test環(huán)境測(cè)試一些插件档址,再添加上。

開發(fā)環(huán)境修改完畢瘦赫,測(cè)試下生產(chǎn)環(huán)境

npm run build

Error: Cannot find module 'del'

在package.lock文件中搜索這個(gè)模塊辰晕,發(fā)現(xiàn)是webpack-dev-server的依賴,刪除package.lock從新安裝webpack-dev-server

npm run build

Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead.

按官方文檔添加splitChunks

module.exports = {
  //...
  optimization: {
    splitChunks: {
      chunks: 'async',
      minSize: 30000,
      maxSize: 0,
      minChunks: 1,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: '~',
      automaticNameMaxLength: 30,
      name: true,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    }
  }
};

npm run build

.scss文件中一些sass的寫法确虱,報(bào)錯(cuò)

比如 雙斜線注釋// 會(huì)報(bào)錯(cuò) 需改成 /* */ 含友; @import 下劃線開頭的scss文件,需要嚴(yán)格寫文件名包括后綴,否則報(bào)錯(cuò)

npm run build

報(bào)錯(cuò)

? building for production.../Users/treePro/develop/X/good_things/common-manage-platform-vue/node_modules/copy-webpack-plugin/dist/index.js:127
                    compilation.contextDependencies.push(context);
                                                    ^

TypeError: compilation.contextDependencies.push is not a function

更新copy-webpack-plugin

"copy-webpack-plugin": "4.0.1", => "copy-webpack-plugin": "5.0.4",

npm i

npm run build 打包成功

本地啟動(dòng)nginx服務(wù)窘问,制定dist目錄位置靜態(tài)服務(wù)目錄

瀏覽器訪問(wèn) http://localhost/index.html 
// 報(bào)錯(cuò)
index.js?t=1568122675619:86 GET http://localhost/1909100108/static/js/manifest.js net::ERR_ABORTED 404 (Not Found)

發(fā)現(xiàn) manifest.js 和 vendor.js 沒(méi)有導(dǎo)出

查了一通

// 去掉 output 里的 chunkFilename: utils.assetsPath('js/[id].js')
// optimization 添加 runtimeChunk
optimization: {
  runtimeChunk: {
      name: "manifest" // 所有生成 chunk 之間共享的運(yùn)行時(shí)文件打包到 manifest
  },  

npm run build 正常h輸出manifest.js 和 vendor.js

刷新頁(yè)面發(fā)現(xiàn)樣式錯(cuò)亂辆童,調(diào)試發(fā)現(xiàn) .vue文件里的scss沒(méi)有提取出來(lái)。猜測(cè)是不是mini-css-extract-plugin的問(wèn)題

發(fā)現(xiàn)/build/utils.js 中有問(wèn)題

return [
  {
    loader: MiniCssExtractPlugin.loader
  },
  'css-loader'
]
// 改成
return [MiniCssExtractPlugin.loader].concat(loaders)

npm run build 刷新頁(yè)面惠赫,正確

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末把鉴,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子儿咱,更是在濱河造成了極大的恐慌庭砍,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件混埠,死亡現(xiàn)場(chǎng)離奇詭異怠缸,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)钳宪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門揭北,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人吏颖,你說(shuō)我怎么就攤上這事搔体。” “怎么了半醉?”我有些...
    開封第一講書人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵疚俱,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我奉呛,道長(zhǎng)计螺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任瞧壮,我火速辦了婚禮登馒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咆槽。我一直安慰自己陈轿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開白布秦忿。 她就那樣靜靜地躺著麦射,像睡著了一般。 火紅的嫁衣襯著肌膚如雪灯谣。 梳的紋絲不亂的頭發(fā)上潜秋,一...
    開封第一講書人閱讀 49,772評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音胎许,去河邊找鬼峻呛。 笑死罗售,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的钩述。 我是一名探鬼主播寨躁,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼牙勘!你這毒婦竟也來(lái)了职恳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤方面,失蹤者是張志新(化名)和其女友劉穎放钦,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葡幸,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡最筒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蔚叨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡辙培,死狀恐怖蔑水,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情扬蕊,我是刑警寧澤搀别,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站尾抑,受9級(jí)特大地震影響歇父,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜再愈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一榜苫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翎冲,春花似錦垂睬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至缴渊,卻和暖如春赏壹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背衔沼。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工蝌借, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留昔瞧,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓骨望,卻偏偏與公主長(zhǎng)得像硬爆,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子擎鸠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • babel官網(wǎng) babel 介紹 Babel 是一個(gè)通用的多用途 JavaScript 編譯器缀磕。通過(guò) Babel ...
    鋒享前端閱讀 1,813評(píng)論 0 10
  • webpack-4.x 安裝 npm i webpack -g: 全局安裝webapck 基本使用 不使用配置文件...
    duans_閱讀 1,725評(píng)論 0 12
  • 最近學(xué)習(xí)webpack相關(guān)知識(shí),特此記錄下學(xué)習(xí)過(guò)的文檔以及搭建過(guò)程劣光;如有錯(cuò)誤袜蚕,記得告訴我呀。項(xiàng)目地址:地址,求星星...
    前端gogogo閱讀 959評(píng)論 0 0
  • babel 7 的使用的個(gè)人理解 最近看了很多關(guān)于babel的使用方法绢涡,大部分在一些點(diǎn)上都沒(méi)有說(shuō)明白牲剃,同時(shí)給出的代...
    zshawk1982閱讀 20,993評(píng)論 14 43
  • 本文主要從一個(gè)上線項(xiàng)目的packge.json分析一個(gè)上線項(xiàng)目中常見(jiàn)的插件以及為什么會(huì)引入這些插件。 通過(guò)這些配置...
    Leslie_2386閱讀 990評(píng)論 0 2