Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - How to Configure Babel, Webpack, and Corejs Properly to ensure browser compatability

Background

I am desperately trying to configure Babel to compile my application to make it backwards compatible based on the target list of browsers I define.

Here is my current configuration:

babel.config.json

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "corejs": 3,
                "useBuiltIns": "usage",
                "targets": {
                    "browsers": [
                        "> 1%",
                        "last 2 versions",
                        "ie >= 8"
                    ]
                }
            }
        ]
    ],
    "plugins": [
        "@babel/plugin-syntax-dynamic-import",
        [
            "@babel/plugin-transform-runtime",
            {
                "regenerator": true
            }
        ]
    ]
}

In Webpack configuration:

{
    test: /.js$/,
    exclude: /node_modules/,
    use: {
        loader: 'babel-loader'
    }
}

I have tried numerous things but everything I try results in certain browser errors...

The Error(s) - and what I have tried...

With modules set to false, I get the following errors:

  • Chrome: Cannot assign to read only property 'exports' of object '#<Object>'
  • IE 11: Assignment to read-only properties is not allowed in strict mode

With modules set to commonjs, I get the following errors:

  • IE8: Expected Identifier (a lot of them)

To attempt to fix the above, I then removed the exclude from the Webpack rule, but this resulted in a compilation error:

TypeError: $ is not a function

My Question

Quite simply, what am I doing wrong? Are there limitations to Babel's power? Should I just use commonjs and accept that it doesn't work in IE8?

I am hesitant to use commonjs as I would rather my ECMAScript is not all converted to CommonJS, but I will accept this if there is no other solution.

NOTE: The purpose of this is to show a pretty error to those on ancient browsers, and as such, I need it to be compatible with browsers such as IE8/9

question from:https://stackoverflow.com/questions/65625715/how-to-configure-babel-webpack-and-corejs-properly-to-ensure-browser-compatabi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...