ES2015 to CommonJS import/export transformer
Fully based on ascjs, asbundle is a no-brainer to create out of the box browser compatible bundles. Don't miss it out!
This module does one thing only: it loosely transpiles ES2015import/export statements into valid CommonJS in order to fix the only part of Node that's incompatible with modern JS.
You can use ascjs as binary utility or as module.
npm install -g ascjs # to see what you can do ascjs --help As executable, you can use ascjs to output, or save, some code content.
ascjs code ascjs --ignore=a.js,b.js sourceFile ascjs --no-default ascjs sourceFile ascjs sourceFile destFile # folders are recursively parsed# destFolder is mandatory ascjs sourceFolder destFolderYou can also use it via pipe operator.
echo code | ascjs cat source.js | ascjs | uglifyjs -o dest.jsAs module, you can require it and use it to convert ESM to CJS.
constascjs=require('ascjs');ascjs('import "test";');// require("test");- extremely lightweight, based on babylon for performance and reliability, it transforms only imports/exports ignoring everything else
- produces modern JavaScript, you are in charge of extra transformations if needed
- indentation, spaces, semi or no-semi are preserved: beautiful source code remains beautiful
- uses same Babel convention, resolving
export default ...intent asexports.default - you can finally write
.jscode and transform it for Node only before publishing on npm - you could write
.mjsmodules and transform them into CommonJS for Browserify or other bundlers as target
- live bindings for exported values are not preserved. You need to delegate in scope eventual changes
- dynamic
import(...)is untouched. If you write that, let Webpack handle it for you later on - there is no magic whatsoever in module names resolution, what you write in ESM is what you get as CJS
--ignore=...a comma separated paths to ignore parsing--no-defaultremove the__esModuleflag and export default viamodule.exports =
This module can transform the following ES2015+ code
importfunc,{a,b}from'./module.js';import*astmpfrom'other';constval=123;exportdefaultfunctiontest(){console.log('ascjs');};export{func,val};into the following one:
'use strict';constfunc=(m=>m.__esModule ? m.default : m)(require('./module.js'));const{a, b}=require('./module.js');consttmp=require('other');constval=123;functiontest(){console.log('ascjs');}Object.defineProperty(exports,'__esModule',{value: true}).default=test;exports.func=func;exports.val=val;