2
1

More than 5 years have passed since last update.

重い腰をあげて vue init pwa で作ったサービスをwebpack4にアップデートした件

Posted at

戦略

  • まず主要packageを一括で更新して、buildエラーを見ながら修正していく

パッケージ更新

  • package.jsonのwebpack系とbabel系を@latestで一気に更新
  • 下記は見やすいよう複数に分けてますが、実際はワンライナーでやった
yarn add -D webpack@latest webpack-cli@latest webpack-merge@latest
yarn add -D webpack-dev-middleware@latest webpack-hot-middleware
yarn add -D html-webpack-plugin@latest mini-css-extract-plugin@latest
yarn add -D optimize-css-assets-webpack-plugin@latest
yarn add -D @babel/core@latest @babel/preset-env@latest

TL;DR;

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

以降は興味がある人向け

buildしてエラー見ながら潰した際のメモ

  • yarn buildしてエラーが沢山でてきたので、地道に対応する

webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

  • mode 'producetion'を設定する
  • webpack.prod.conf.jsから、UglifyJsPluginに係る項目を消す

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

  • 既存のCommonsChunkに関する記述を消す
  • 下記を変わりに記述する
    optimization: {
      splitChunks: {
        cacheGroups: {
          vendor: {
            test: /node_modules/,
            name: 'vendor',
            chunks: 'initial',
            enforce: true
          }
        }
      }
    },

Use Chunks.groupsIterable and filter by instanceof Entrypoint instead

  • extract-text-webpack-pluginパッケージを最新化するとこのエラーは消えるが、別なエラーがでる。
  • mini-css-extract-pluginを使うと解消される(後述)
  • 使わないので削除しておく
yarn remove extract-text-webpack-plugin

Error: Plugin/Preset files are not allowed to export objects, only functions. In /node_modules/babel-preset-stage-2/lib/index.js

  • babel-*関連を、一律アップデートした
  • 一例として
yarn add -D @babel/core@latest @babel/preset-env@latest

Please remove the `import 'babel-polyfill'` call or use `useBuiltIns: 'entry'` instead.

  • 素直に設定変更する
.babelrc
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "browsers": ["last 2 versions", "IE >= 11"]
        },
        "useBuiltIns": "entry",
        "corejs": 3
      }
    ]
  ]
}

Path variable [contenthash] not implemented in this context: static/css/[name].[contenthash].css


// extract css into its own file
new MiniCssExtractPlugin({
  filename: utils.assetsPath('css/[name].[contenthash].css')
}),

大変参考になったページ

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1