简体   繁体   English

使用 webpack 4 和 Babel 转译为 ES5

[英]Transpiling to ES5 using webpack 4 and Babel

I'm still using webpack 4.28.4 for organizational reasons.出于组织原因,我仍在使用 webpack 4.28.4。 We are trying to transpile some of our code to support ES5 using Babel 7, but our resulting bundle is throwing console errors when loaded in a browser (Chromium v44).我们正在尝试使用 Babel 7 转换我们的一些代码以支持 ES5,但是我们生成的包在加载到浏览器(Chromium v44)时会引发控制台错误。 We have seen some of the newer options available in webpack 5, which some of my colleagues have been able to use to resolve similar failures:我们已经看到 webpack 5 中提供了一些较新的选项,我的一些同事已经能够使用它们来解决类似的故障:

  • output.environment.arrowFunction = false output.environment.arrowFunction = false
  • target = 'es5'目标='es5'

Is there some sort of equivalent approach for webpack 4? webpack 4 是否有某种等效方法? Do we just need to find the right combination of Babel plugins to fully convert to ES5?我们是否只需要找到正确的 Babel 插件组合就可以完全转换为 ES5?

The console error we get in Chromium 44 is always an "Uncaught SyntaxError: Unexpected token )" in some low level Dojo JavaScript file which we know is unchanged and should be a valid function.我们在 Chromium 44 中得到的控制台错误始终是某个低级别 Dojo JavaScript 文件中的“Uncaught SyntaxError: Unexpected token )”,我们知道该文件没有改变,应该是有效的 ZC1C425268E683894F11AB45A

Here's a cleaned up version of our webpack.config.js:这是我们的 webpack.config.js 的清理版本:

const path = require('path');
const webpack = require('webpack');
const BabelLoaderPlugin = require('babel-loader');
const BabelPreset = require('@babel/preset-env');

const makeConfig = ({
      component_path,
      js_module,
      entryPoints = {"index": js_module + "/entryPoint",
                     "index-css": js_module + "/css/main"},
      outputPublicPath = 'release/',
      outputPath = path.resolve(component_path, 'release'),
      dev_mode = 'production',
      babel_chrome_level = "44",
    } = {}) => {
    return {
        entry: entryPoints,
        output: {
            path: outputPath,
            publicPath: outputPublicPath,
            pathinfo: true,
            filename: "bundle.[name].js"
        },
        module: {
            rules: [
                {
                    test: /\.m?js$/,
                    exclude: /(node_modules|bower_components|dojo)/,
                    use: {
                        loader: 'babel-loader',
                        query: {
                            babelrc: false,
                            presets: [
                                [BabelPreset,
                                {
                                    "exclude": ["transform-regenerator"],
                                    "targets": {
                                        "chrome": babel_chrome_level
                                    }
                                }]
                            ],
                            plugins: [
                                require("@babel/plugin-transform-async-to-generator"),
                                require("@babel/plugin-transform-arrow-functions"),
                                require("@babel/plugin-transform-modules-commonjs")
                            ]
                        }
                    }
                },
            ]
        },
        plugins: [
            ...
        ],
    };
}
module.exports = { makeConfig };

Did you try to hardcoded it to 44 instead of using babel_chrome_level ?您是否尝试将其硬编码为44而不是使用babel_chrome_level

In Chrome devtools, you might be able to get it to pretty-print the minified Dojo sources to better identify exactly which feature it's tripping over在 Chrome devtools 中,您可能可以让它漂亮地打印缩小的 Dojo 源,以更好地准确识别它绊倒的功能

The better fix for this is to find whatever numbskull in your organization has you stuck on a FIVE YEAR OLD version of a browser and smack them until they stop更好的解决方法是找到您组织中的任何 numbskull 让您卡在五年版本的浏览器上并打他们直到他们停止

Also it looks like you're explicitly excluding dojo from your Babel transform此外,您似乎从 Babel 转换中明确排除dojo

I don't know much about Dojo -- I guess they made a totally new "modern" version at some point?我对 Dojo 不太了解——我猜他们在某个时候制作了一个全新的“现代”版本? -- but maybe you can pin to an older version of it that officially supports your version of Chrome? -- 但也许您可以固定到正式支持您的 Chrome 版本的旧版本?

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

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