I am attempting to configure babel preset-env to take care of injecting polyfills automatically, with respect to the specified list of target browsers.
The documentation states that configuring the preset-env
preset with a property in the form of "useBuiltIns": "usage"
will do exactly this.
(I noticed that specifying this property alone caused a warning to show in the console, however, stating that I ought to specify a core-js version to use, so I have added this too.)
Therefore, my babel.config.js
is looking like this:
module.exports = {
"presets": [
"@babel/preset-react",
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": ['macros'],
};
Fantastic! This should be a breeze!
However, having re-compiled with webpack, my app now dies in the browser, with a console message of:
Uncaught TypeError: __webpack_require__(...) is not a function
at Module../node_modules/webpack/buildin/harmony-module.js (harmony-module.js:24)
I've done some research (only about 3 hours worth!) and I understand this is the result of babel trying to transpile babel, or something of that sort...that I need to add some files to a list of ignores that will not be transpiled, or doubly-transpiled, perhaps.
In my webpack config, I've put the following. (Note the exclude entries).
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: [/core-js/, /regenerator-runtime/],
use: [
{
loader: 'babel-loader',
options: {
presets: babelConfig.presets,
cacheDirectory: babelCacheDir
}
},
'eslint-loader'
]
}
]
}
I've also tried adding a few variations on the ignore pattern, but I still have fatal console errors.
Another thing I've tried, is ignoring the node_modules
folder, but then I have big chunks of code that aren't transpilled at all. i.e. In IE11, I get syntax errors regarding ES6 arrow functions that originate in untranspilled vendor scripts.
If anyone is able to explain why __webpack_require__
is being altered in an undesirable way, I would be very greteful, as the exact solution that this problem requires is being quite resistant to research.
Edit: The following suggests that babel must be made to ignore core-js
, which seems very reasonable. It suggets using the following in the babel config file:
{
ignore: [
/\/core-js/,
],
sourceType: "unambiguous",
presets: [
['@babel/preset-env', { modules: false, useBuiltIns: 'usage' }],
],
}
However, this causes another fairly, seemingly nonspecific error to arise:
has.js:4 Uncaught TypeError: Cannot convert undefined or null to object
at hasOwnProperty (<anonymous>)
at push../node_modules/core-js/internals/has.js.module.exports (has.js:4)