S
S
SokLor2021-10-15 19:45:39
JavaScript
SokLor, 2021-10-15 19:45:39

Why convert ES modules to CommonJS?

There is a plugin for babel that converts ES modules to CommonJS. For now, it's gone under the hood @babel/preset-env to the modules option.

But I don't quite understand its purpose. Tell me, please, why in the final bundle - code already compiled by babel - change imports / exports to CommonJS?

From
export default 42;

To

Object.defineProperty(exports, "__esModule", {
  value: true,
});

exports.default = 42;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-10-15
@SokLor

Well, because old browsers don't know how to use esmodules, and babel's original idea was to support old browsers - everything else piled up after.
Also, esm cannot be bundled into a single file, unlike CommonJS.
In any case, these are just defaults that you can always change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question