1 | # ascjs
|
2 |
|
3 | [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC) [![Build Status](https://travis-ci.org/WebReflection/ascjs.svg?branch=master)](https://travis-ci.org/WebReflection/ascjs) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/ascjs/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/ascjs?branch=master) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/WebReflection/donate) [![Greenkeeper badge](https://badges.greenkeeper.io/WebReflection/ascjs.svg)](https://greenkeeper.io/)
|
4 |
|
5 | ES2015 to CommonJS import/export transformer
|
6 |
|
7 | - - -
|
8 |
|
9 | ### Looking for a CommonJS minimalistic bundler ?
|
10 |
|
11 | Fully based on _ascjs_, **[asbundle](https://github.com/WebReflection/asbundle)** is a no-brainer to create out of the box browser compatible bundles. Don't miss it out!
|
12 |
|
13 | - - -
|
14 |
|
15 | This module does one thing only:
|
16 | it loosely transpiles **ES2015** [import](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/import)/[export](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export) statements **into** valid **CommonJS** in order to fix the only part of Node that's incompatible with modern JS.
|
17 |
|
18 | ## How to
|
19 |
|
20 | You can use _ascjs_ as binary utility or as module.
|
21 |
|
22 | ```sh
|
23 | npm install -g ascjs
|
24 |
|
25 | # to see what you can do
|
26 | ascjs --help
|
27 |
|
28 | ```
|
29 |
|
30 | As executable, you can use _ascjs_ to output, or save, some code content.
|
31 | ```sh
|
32 | ascjs code
|
33 | ascjs --ignore=a.js,b.js sourceFile
|
34 | ascjs --no-default
|
35 | ascjs sourceFile
|
36 | ascjs sourceFile destFile
|
37 |
|
38 | # folders are recursively parsed
|
39 | # destFolder is mandatory
|
40 | ascjs sourceFolder destFolder
|
41 | ```
|
42 |
|
43 | You can also use it via pipe operator.
|
44 | ```sh
|
45 | echo code | ascjs
|
46 | cat source.js | ascjs | uglifyjs -o dest.js
|
47 | ```
|
48 |
|
49 | As module, you can require it and use it to convert ESM to CJS.
|
50 | ```js
|
51 | const ascjs = require('ascjs');
|
52 |
|
53 | ascjs('import "test";');
|
54 | // require("test");
|
55 | ```
|
56 |
|
57 | ### Features
|
58 |
|
59 | * extremely lightweight, based on [babylon](https://github.com/babel/babylon) for performance and reliability, it transforms only imports/exports ignoring everything else
|
60 | * produces modern JavaScript, you are in charge of extra transformations if needed
|
61 | * indentation, spaces, semi or no-semi are preserved: beautiful source code remains beautiful
|
62 | * uses same [Babel](http://babeljs.io) convention, resolving `export default ...` intent as `exports.default`
|
63 | * you can finally write `.js` code and transform it for Node only before publishing on _npm_
|
64 | * you could write `.mjs` modules and transform them into CommonJS for [Browserify](http://browserify.org) or other bundlers as target
|
65 |
|
66 | ### Constrains
|
67 |
|
68 | * live bindings for exported values are not preserved. You need to delegate in scope eventual changes
|
69 | * dynamic `import(...)` is untouched. If you write that, let [Webpack](https://webpack.js.org) handle it for you later on
|
70 | * there is no magic whatsoever in module names resolution, what you write in ESM is what you get as CJS
|
71 |
|
72 | ### Flags
|
73 |
|
74 | * `--ignore=...` a comma separated paths to ignore parsing
|
75 | * `--no-default` remove the `__esModule` flag and export default via `module.exports = `
|
76 |
|
77 | ### Example
|
78 | This module can transform the following ES2015+ code
|
79 | ```js
|
80 | import func, {a, b} from './module.js';
|
81 | import * as tmp from 'other';
|
82 |
|
83 | const val = 123;
|
84 |
|
85 | export default function test() {
|
86 | console.log('ascjs');
|
87 | };
|
88 |
|
89 | export {func, val};
|
90 | ```
|
91 | into the following one:
|
92 | ```js
|
93 | 'use strict';
|
94 | const func = (m => m.__esModule ? m.default : m)(require('./module.js'));
|
95 | const {a, b} = require('./module.js');
|
96 | const tmp = require('other');
|
97 |
|
98 | const val = 123;
|
99 |
|
100 | function test() {
|
101 | console.log('ascjs');
|
102 | }
|
103 | Object.defineProperty(exports, '__esModule', {value: true}).default = test;
|
104 |
|
105 | exports.func = func;
|
106 | exports.val = val;
|
107 | ``` |
\ | No newline at end of file |