eight04 / cjs-es Goto Github PK
View Code? Open in Web Editor NEWTransform CommonJS modules into ES modules.
License: MIT License
Transform CommonJS modules into ES modules.
License: MIT License
Transform
const foo = require("foo");
class bar extends foo {}
to
import foo from "foo";
class bar extends foo {}
entry.js
const foo = require("./foo");
foo.foo();
foo.js
module.exports = {
value: "FOO",
foo: function() {
return this.value;
}
}
Lines 72 to 74 in 0488677
[, foo] = bar();
hey @eight04 ,
I guess there's no built-in way to require from a path relative to some pre-defined global app root in Node. There sure are a lot of userland npm modules for this. It might be nice to add support for something basic. My current thought is to just configure a pre-defined global [1]. This would allow cjs modules to work normally in node but also properly convert to es imports/exports.
Maybe there's nothing cjs-es can do here, i dunno.
For now, I can probably work around this by setting a global in Node and using a rollup/regex-replace plugin to swap APP_ROOT before passing it on to cjs-es.
if (typeof exports === "object") {
module.exports = factory();
}
function test() {
module.exports();
}
module.exports = () => {};
to
function test() {
_export_default_();
}
const _export_default_ = () => {};
export default _export_default_;
(module.exports = () => "foo").bar = true;
to
let _export_default_;
export default _export_default_;
(_export_default_ = () => "foo").bar = true;
Transform
var a = require("a"),
b = require("b")
to
import * as a from "a";
import * as b from "b";
nested: true
? or can we analyze the entire VariableDeclaration
?exports.bar = exports.foo = "foo";
exports.foo = "bar";
Hey there, have you thought about adding a cli entry point? Or is there already another package doing that?
Context: On the journey of making a library written in commonjs format also available in es module format, it would make sense to generate all the es modules from the commonjs modules (after every change).
I do not have a lot of experience on the topic, but I think using rollup for that would generate a single file, not multiple files, which is not what I would prefer.
https://unpkg.com/browse/[email protected]/dist/util/unesc.js
exports.foo = "foo";
module.exports = exports.foo;
function test() {
return exports.foo();
}
exports.foo = () => "foo";
Could be transformed into
function test() {
return _export_foo_();
}
const _export_foo_ = () => "foo";
export {_export_foo_ as foo};
code: a.js
require('./c');
if (true) {
require('./a');
} else {
require('./b');
}
function A () {
console.log('hhhh');
}
module.exports = A;
and I use the below code to transform
const fs = require('fs');
const {parse} = require("acorn");
const {transform} = require("cjs-es");
const code = fs.readFileSync('./a.js', 'utf-8');
transform({code, parse})
.then(result => {
console.log(result.code);
});
the result show that not working in if-else block
acorn: 8.0.4
cjs-es: 0.8.2
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.