amd2cmd
v0.2.1
Published
transform AMD(require.js) to CommonJS
Downloads
4
Readme
AMD2CMD
transform AMD or CommonJS inspired by require.js to CommonJS.
Transform What
before(AMD):
define(['dep1', 'dep2'], function(dep1, dep2OtherName) {
return dep1 + dep2OtherName;
});
transform after(CommonJS):
var dep1 = require('dep1');
var dep2OtherName = require('dep2');
module.exports = dep1 + dep2OtherName;
- transform
define(['dep1', 'dep2'], function() {});
torequire('dep1');require('dep2');
- transform
define(function(){return statements;})
tomodule.exports = statements;
- transform
require('obj/xxx')
torequire('../obj/xxx')
So, Notice this tool can not transform all require.js features.
Usage
cli
First, install amd2cmd:
npm install -g amd2cmd
Second, cd your project, and exec cmd:
amd2cmd --in=src/scripts/**/*.js --out=build/scripts --basedir=src/scripts
use with node.js
First, install amd2cmd:
npm install --save amd2cmd
Then, you can use amd2cmd like this:
import { amd2cmd } from 'amd2cmd';
amd2cmd(['src/scripts/**/*.js'], 'build/scripts', 'src/scripts')
.on('finish', function() {
console.log('finish to transform amd code to cmd code');
});
or like this:
import { transformCode } from 'amd2cmd';
const cmdCode = transformCode(`define(['dep1', 'dep2'], function(dep1, dep2OtherName) {
return dep1 + dep2OtherName;
});`, 'file path', 'base path');
console.log(cmdCode);
/* print:
var dep1 = require('dep1');
var dep2OtherName = require('dep2');
module.exports = dep1 + dep2OtherName;
*/
use with gulp
import { transform } from 'amd2cmd';
gulp.src('app/**/*.js')
.pipe(transform({
basedir: 'app'
}))
.pipe(gulp.dest('build/scripts'));
scripts
Build the project shell:
$ npm run build
Test the project shell:
$ npm run test
Test the project with coverage result:
$ npm run coverage
Generate JavaScript API doc:
$ npm run doc