当前位置: 首页 > 工具软件 > X-Pack > 使用案例 >

webpack3.x 升级至webpack4.x

颛孙航
2023-12-01

webpack3.x 升级至webpack4.x
预装环境:
nodejs v10.16.0 以上
npm 6.1.0 以上
文件修改
1、修改package.json 相关模块版本,然后删除node_modules文件夹,运行如下命令:

cnpm install
cnpm list

vue项目常用依赖如下

{
  "name": "nxin.ant.front",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "author": "ziyang",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
    "e2e": "node test/e2e/runner.js",
    "test": "npm run unit && npm run e2e",
    "lint": "eslint --ext .js,.vue src test/unit test/e2e/specs",
    "build": "node build/build.js"
  },
  "dependencies": {
    "@progress/kendo-spreadsheet-vue-wrapper": "^2018.2.516",
    "@progress/kendo-theme-default": "^2.53.2",
    "@progress/kendo-ui": "^2018.2.530",
    "ace-builds-ant": "0.0.3",
    "css-line-break": "^1.0.1",
    "element-ui": "^2.3.7",
    "install": "^0.11.2",
    "jquery": "^3.3.1",
    "jszip": "^2.6.1",
    "npm": "^6.4.1",
    "or": "^0.2.0",
    "serviceworker-webpack-plugin": "^1.0.1",
    "vue": "^2.6.10",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1",
    "xlsx": "^0.14.1"
  },
  "devDependencies": {
    "acorn": "^6.1.1",
    "acorn-jsx": "^5.0.1",
    "ajv": "^6.10.0",
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^10.0.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-istanbul": "^4.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
    "chai": "^4.1.2",
    "chalk": "^2.0.1",
    "chromedriver": "^2.39.0",
    "copy-webpack-plugin": "^4.0.1",
    "cross-env": "^5.0.1",
    "cross-spawn": "^5.0.1",
    "css-loader": "^0.28.0",
    "eslint": "^5.16.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-friendly-formatter": "^4.0.1",
    "eslint-import-resolver-node": "^0.3.2",
    "eslint-loader": "^2.1.2",
    "eslint-plugin-flowtype": "^3.9.1",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-node": "^9.1.0",
    "eslint-plugin-promise": "^4.1.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^5.2.2",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "happypack": "^5.0.0",
    "html-webpack-plugin": "^3.2.0",
    "inject-loader": "^4.0.1",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "mini-css-extract-plugin": "^0.7.0",
    "mocha": "^5.2.0",
    "nightwatch": "^1.0.9",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "phantomjs-prebuilt": "^2.1.16",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "selenium-server": "^3.0.1",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "sinon": "^4.0.0",
    "sinon-chai": "^2.8.0",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^1.1.1",
    "vue-loader": "^15.7.0",
    "vue-particles": "^1.0.9",
    "vue-style-loader": "^4.1.2",
    "vue-template-compiler": "^2.6.10",
    "vue-test-utils": "^1.0.0-beta.11",
    "vueify": "^9.4.1",
    "webpack": "^4.6.0",
    "webpack-bundle-analyzer": "^3.3.2",
    "webpack-cli": "^3.3.2",
    "webpack-dev-server": "^3.1.1",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "main": "index.js",
  "repository": "http://gitlab.nxin.com/ziyang/Nxin.Ant.Front.git",
  "license": "MIT"
}

2、显示添加 vueLoaderConfig 插件
修改 webpack.base.conf.js 文件

  // ...(config.dev.useEslint ? [createLintingRule()] : []), 注释掉
  
  
  const vueLoaderConfig = require('./vue-loader.conf')
  // 添加代码
  plugins: [
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
    }),
    new VueLoaderPlugin(),
  ], 

3、ExtractTextPlugin 插件改为使用 MiniCssExtractPlugin插件
utils.js 文件

 // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      // return ExtractTextPlugin.extract({
      //   use: loaders,
      //   fallback: 'vue-style-loader'
      // })
      return [ MiniCssExtractPlugin.loader ].concat(loaders)
    } else {
      return ['vue-style-loader'].concat(loaders)
    }

webpack.prod.conf.js 文件

// extract css into its own file
    new MiniCssExtractPlugin({
      filename: utils.assetsPath('css/[name].[contenthash].css'),
      // Setting the following option to `false` will not extract CSS from codesplit chunks.
      // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
      // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, 
      // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
      chunkFilename: "[id].[contenthash].css",
      allChunks: true,
    })

4、webpack.optimize.CommonsChunkPlugin 插件改为使用 webpack.optimize.SplitChunksPlugin
webpack.prod.conf.js 文件

// split vendor js into its own file
    new webpack.optimize.SplitChunksPlugin ({
      name: 'vendor',
      minChunks (module) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.SplitChunksPlugin ({
      name: 'manifest',
      minChunks: Infinity
    }),
    // This instance extracts shared chunks from code splitted chunks and bundles them
    // in a separate chunk, similar to the vendor chunk
    // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
    new webpack.optimize.SplitChunksPlugin({
      name: 'app',
      async: 'vendor-async',
      children: true,
      minChunks: 3
    }),
 类似资料: