GithubHelp home page GithubHelp logo

sidvishnoi / esm-to-cjs Goto Github PK

View Code? Open in Web Editor NEW
42.0 1.0 5.0 46 KB

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

JavaScript 100.00%
esmodules esm commonjs nodejs string-manipulation transformer gulp-plugin

esm-to-cjs's People

Contributors

favna avatar sidvishnoi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

esm-to-cjs's Issues

Import only is not supported

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
};

The simpler transform is semantically wrong

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.