简体   繁体   English

如何修复Webpack避免在生产环境中破坏代码?

[英]How do I fix Webpack from mangling my code in production?

I'm building an application using BitcoinJS, and have found that using the -p flag causes webpack to mangle certain parts, breaking the ability to produce transactions. 我正在使用BitcoinJS构建应用程序,并且发现使用-p标志会使webpack破坏某些部分,从而破坏了产生交易的能力。

I was advised to use noParse with alias in the config, which made the problem worse (the entire application would not load at all). 建议我在配置中使用带有alias noParse ,这会使问题变得更糟(整个应用程序根本无法加载)。

So far I have not found any workaround other than turning off production (which leaves all sorts of strange things in the javascript file, including my local development paths). 到目前为止,除了关闭生产之外,我没有找到其他解决方法(这会在javascript文件中留下各种奇怪的东西,包括我的本地开发路径)。

Example project available here (with install instructions, and how to reproduce): https://github.com/Someguy123/example-webpack-issue 此处提供示例项目 (带有安装说明以及如何复制): https : //github.com/Someguy123/example-webpack-issue

EDIT/UPDATE : @bebraw has provided a working solution, which is available on the fixed branch in the example project if anyone else wanted to see it in action. 编辑/更新 :@bebraw提供了一个有效的解决方案,如果其他人想看到它的作用,可以在示例项目的fixed分支上找到该解决方案。

The problem is that Uglify mangles too much by default in your case. 问题在于,在您的情况下,默认情况下,Uglify会处理过多的内容。 As per bitcoinjs-lib instructions, you need to exclude certain names like this: 按照bitcoinjs-lib的说明,您需要排除某些名称,例如:

plugins: [
    new webpack.optimize.UglifyJsPlugin({
        mangle: {
            except: [
                'Array', 'BigInteger', 'Boolean', 'Buffer',
                'ECPair', 'Function', 'Number', 'Point'
            ]
        }
    })
]

Use webpack instead of webpack -p after configuring the plugin. 配置插件后,请使用webpack而不是webpack -p

It will make the resulting bundle a little larger, but it will work. 它将使生成的捆绑包更大一点,但它将起作用。

Another alternative would be to generate the bundle separately for bitcoinjs-lib elsewhere and then consume that instead. 另一种选择是为其他地方的bitcoinjs-lib分别生成捆绑包,然后使用它。 You would have to take mangling into account there as well, but it would keep your project a little neater. 您也必须在那里考虑修改,但这会使您的项目更加整洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM