symfony 如何使用TerserPlugin和webpack-encore进行缩小

rseugnpd  于 2022-12-30  发布在  Webpack
关注(0)|答案(3)|浏览(190)

这个webpack.config.js使用了webpack-encore,并用terser进行了minify,我可以成功地编译,但是绝对没有任何东西被minify,注解和完整的变量名仍然存在。

var Encore = require('@symfony/webpack-encore');

const TerserPlugin = require('terser-webpack-plugin');

Encore
    // the project directory where compiled assets will be stored
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    // the public path used by the web server to access the previous directory
    .cleanupOutputBeforeBuild()
    .enableSourceMaps(!Encore.isProduction())
    .enableReactPreset()
    .enableSassLoader()
    .enableLessLoader()
    .autoProvidejQuery()
    .disableSingleRuntimeChunk()

     .addEntry('app_prescription', [
         './assets/js/prescription/app_prescription.js'
     ])


    .addLoader({
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader?cacheDirectory"
    })

;

if( Encore.isProduction() ) {
    Encore.addPlugin(new TerserPlugin({parallel: true,cache:true}) );
}

module.exports = function(env) {

    Encore.enableVersioning();
    const config = Encore.getWebpackConfig() ;

    if( Encore.isProduction() ) {

        config.optimization = {
            minimizer: [new TerserPlugin({parallel: true,cache:true})]
        }
    }


    return config ;
}

这个代码有什么问题?

osh3o9ms

osh3o9ms1#

对于2021年及以后偶然发现这一点的每一个人,迟来的答案是:
使用Encore的configureTerserPlugin方法:

Encore.configureTerserPlugin((options) => {
     options.cache = true;
     options.terserOptions = {
         output: {
             comments: false
         }
     }
 })
lkaoscv7

lkaoscv72#

根据https://github.com/webpack-contrib/terser-webpack-plugin#extractcomments,为避免生成*.LICENSE.txt文件,应使用以下方法选项:

Encore.configureTerserPlugin(options => {
    options.extractComments = false;
});
42fyovps

42fyovps3#

const TerserPlugin = require('terser-webpack-plugin');

Encore.configureTerserPlugin((options) => {
    options.extractComments = false;
    options.terserOptions = {
        output: {
            comments: false,
        },
    };
});

// Minify code
const optimization = {
    minimize: true,
    minimizer: [
        new TerserPlugin(),
    ],
}

... // other encore code in here

module.exports = {
    ...Encore.getWebpackConfig(),
    name: config.name,
    optimization,
};

相关问题