I want to create an npm library that delivers modules using ES import/export instead of commonjs (like for example lodash-es does). If I'm not mistaken, the delivered code must be ES5 and has import/export.
I can't figure out how to configure Babel to do that. I expect that the option targets.esmodules should do the trick.
When I have the following code in calc.js:
// calc.js export function add (a, b) { const result = a + b return result } and babel configuration .babelrc:
{ "presets": [ [ "@babel/preset-env", { "targets": { "esmodules": true } } ] ] } When running babel:
babel calc.js The output is:
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.add = add; function add(a, b) { var result = a + b; return result; } Which is commonjs (exports.add). The option targets.esmodules seems to have no effect. How can I get Babel to generate an es module (export function add)? Or am I misunderstanding the idea of delivering ES modules on npm?
import/exportdoes also understand all the other ES6 syntax, so this requirement is really weird.redux, theesfolder contains esimport/exportand the code is transpiled ES5 (no=>,const, etc). Same withlodash-es. How to achieve that with Babel?