sidvishnoi / esm-to-cjs Goto Github PK
View Code? Open in Web Editor NEWTransform ESM to Common JS for present NodeJS, without any junk wrappers or useless renaming
Home Page: https://sidvishnoi.github.io/esm-to-cjs/
License: MIT License
Transform ESM to Common JS for present NodeJS, without any junk wrappers or useless renaming
Home Page: https://sidvishnoi.github.io/esm-to-cjs/
License: MIT License
Hello, I encountered some problems while experiencing the conversion function
case 1 Import only is not supported
// input
import "lib";
// what i wanted
require("lib");
// actual:ParseError
case 2 Single and double quotation marks should be mixed
// input
import mod from "mod";
import foo from 'foo';
// what i wanted
const mod = require("mod");
const foo = require("foo");
case 3 export mix
// input
export function foo(){}
exports.bar = function (){}
// what i wanted
function foo(){}
exports.bar = function (){}
exports.foo = foo;
// actual
function foo(){}
exports.bar = function (){}
module.exports = { // covered
foo
};
Would be cool to have typings for this lib
I came to your interesting package in searching of a lighter esm to cjs transformer.
But I noticed the code is semantically wrong which can lead to logic error.
Your example
// input
import { resolve as resolvePath } from "path";
resolvePath("./hello")
// what i wanted
const { resolve: resolvePath } = require("path");
resolvePath("./hello")
Translated the esm import to a simple variable, this is wrong in esm semantic. There is a reason why all babel/TS transforms esm import into verbose a_namespace.an_exported_name
, because in esm standard, the import does not create a local JS variable, it's a read-only property on the namespace of that esm module.
Consider following proof, create two local files:
foo.mjs
let foo = 1;
function increaseFoo() {
foo += 1;
}
export { foo, increaseFoo };
index.mjs
import { foo, increaseFoo } from './foo.mjs';
console.log('foo is ' + foo);
increaseFoo();
console.log('foo is ' + foo);
Then use nodejs v12 or above
node --experimental-modules index.mjs
It prints out:
foo is 1
foo is 2
You can see that foo
inside index.mjs
is not a local variable, otherwise it cannot be mutated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.