How to include webpack plugins when using Laravel Mix?

风流意气都作罢 提交于 2019-12-04 01:35:46

Mix's API provides a useful webpackConfig method for adjusting the webpack config.

https://laravel.com/docs/5.6/mix#custom-webpack-configuration

The webpackConfig method accepts an object, which should contain any Webpack-specific configuration that you wish to apply.

I believe the following code should work.

webpack.mix.js:

const mix = require('laravel-mix');
const CompressionPlugin = require('compression-webpack-plugin');

mix.setPublicPath('dist')
  .js('src/app.js', 'scripts/')
  .extract([
    'jquery',
    'axios',
    'babel-polyfill',
    'lodash',
    'tether',
    'vue',
    'bootstrap-vue',
    'vuex',
    'vuex-localstorage',
  ])
  .sass('src/styles/app.scss', 'styles/')
  .copyDirectory('src/assets', 'dist/assets')
  .options({
    processCssUrls: false,
    uglify: true,
  })
  .webpackConfig({
    plugins: [
      new CompressionPlugin({
        asset: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.js$|\.css$|\.html$|\.svg$/,
        threshold: 10240,
        minRatio: 0.8,
      }),
    ],
  })
  .version();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!