I try to bundle two JavaScript modules, so that the resulting code works in IE11. For this I have setup a yarn/npm project which uses rollup.js for bundling and Babel for transpiling. Everything works fine until I add the (non-dev) dependency core-js
.
Here the details:
package.json
{
"name": "rollup_for_ie",
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"rollup": "^2.24.0"
},
}
rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
export default {
input: 'src/main.js',
output: {
file: 'dist/main.js',
format: 'iife'
},
plugins: [
resolve({
browser: true
}),
babel({
exclude: "node_modules/**", // only transpile our source code
babelHelpers: 'bundled'
})
]
};
babel.config.js
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
browsers: "> 0.5%, ie >= 11"
},
modules: false,
spec: true,
useBuiltIns: "usage",
forceAllTransforms: true,
corejs: 3
}
]
],
};
When I run rollup -c
I get warnings about unresolved dependencies, but a bundled file dist/main.js
with the (used) stuff from the src directory is generated. The resulting file works even in modern browsers like Chrome. So far so good.
However the bundled file is not yet IE11 ready: In IE I get errors like Object doesn't support property or method 'getOwnPropertySymbols'. So the polyfills from core-js need to be added.
For this I install core-js as a prod dependency. Now rollup -c
doesn't give me warnings - but the resulting dist/main.js begins like
(function (exports) {
'use strict';
var $ = require('../internals/export');
.
.
.
which as a script can not neither Chrome nor IE execute! It looks like that somehow the presence of the core-js library throws the rollup bundler off.
How can I fix my setup so that the resulting dist/main.js
works as non-module script in Chrome and IE11?
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the new standardized format for code modules included in the ES6 revision of JavaScript, instead of previous idiosyncratic solutions such as CommonJS and AMD.
webpack and Rollup both require a config file specifying entry, output, loaders, plugins, transformations, etc. However, there's a slight difference: Rollup has node polyfills for import/export, but webpack doesn't. Rollup has support for relative paths in config, but webpack doesn't — which is why you use path.
There is a slight difference between rollup and webpack config file. Rollup has node polyfills for import/export, whereas webpack doesn't. Rollup has support for relative paths, whereas webpack does not, so we either use path. resolve or path.
This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
I think as you enabled the option useBuiltIns: "usage"
which means it will append code from corejs
into your module files which is written with cjs
style. So you have to add this plugin @rollup/plugin-commonjs
to convert back to esm
, then it will work:
import commonjs from '@rollup/plugin-commonjs';
export default {
// ...
plugins: [
// ...
commonjs(),
]
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With