使用webpack和babel在保留函数名称的同时缩小

w51jfk4q  于 2022-12-08  发布在  Babel
关注(0)|答案(2)|浏览(153)

我试图阻止babel和webpack之间的缩小发生在重命名我的函数。我遇到了this,但显然没有正确的选项设置。
这是我的webpack配置文件。我需要做什么修改来使它保留我的函数名?

const path = require('path');
const MinifyPlugin = require('babel-minify-webpack-plugin');

module.exports = {
    entry: {
        translate: ["@babel/polyfill", "./Scripts/es6/translate.js"],
        setupForm: ["./Scripts/es6/setupForm.js"],
        prelimForm: ["./Scripts/es6/prelimForm.js"],
        recruiters: ["./Scripts/es6/recruiters.js"]
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, './Scripts/build'),
    },
    plugins: [
        new MinifyPlugin({
            "keepFnName": true
        }, {})
    ],
    module: {
        rules: [{
            loader: 'babel-loader',
            test: /\.js$/,
            exclude: /node_modules/,
            query: {
                presets: ['@babel/preset-env']
            }
        }]
    }
}
ffscu2ro

ffscu2ro1#

这是terser-webpack-plugin的工作,以缩小html,你可以传递给它keep_fnames: false

module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          keep_fnames: true,
        },
      }),
    ],
  },
};
yv5phkfx

yv5phkfx2#

您可以要求Babel在您的babel.config / babelrc中删除一些旧浏览器与此配置的兼容性:

presets: [
  ['@babel/preset-env', {targets: {browsers: ['> 2%']}}]
]

有关targets的更多信息:https://babeljs.io/docs/en/babel-preset-env#targets
已删除的浏览器兼容性:https://caniuse.com/#feat=mdn-javascript_builtins_function_name

相关问题