Comparing version 0.0.1 to 0.1.0
68
index.js
@@ -0,1 +1,4 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var through = require('through') | ||
@@ -8,2 +11,18 @@ , esprima = require('esprima') | ||
/** | ||
* Transform AMD to CommonJS. | ||
* | ||
* This transform translates AMD modules into CommonJS modules. AMD modules | ||
* are defined by calling the `define` function that is available as a free | ||
* or global variable. The transform translates that call into traditional | ||
* CommonJS require statements. Any value returned from the factory function | ||
* is assigned to `module.exports`. | ||
* | ||
* After the transform is complete, Browserify will be able to parse and | ||
* bundle the module as if it were a Node.js module. | ||
* | ||
* @param {String} file | ||
* @return {Stream} | ||
* @api public | ||
*/ | ||
module.exports = function (file) { | ||
@@ -18,11 +37,25 @@ var data = ''; | ||
var ast = esprima.parse(data) | ||
, tast; | ||
, tast | ||
, isAMD = false; | ||
console.log('-- ORIGINAL AST --'); | ||
console.log(util.inspect(ast, false, null)); | ||
console.log('------------------'); | ||
//console.log('-- ORIGINAL AST --'); | ||
//console.log(util.inspect(ast, false, null)); | ||
//console.log('------------------'); | ||
// TODO: Ensure that define is a top-level function call. | ||
// TODO: Ensure that define is a free variable. | ||
// TODO: Implement support for amdWeb UMD modules. | ||
estraverse.replace(ast, { | ||
enter: function(node) { | ||
if (isDefine(node)) { | ||
var parents = this.parents(); | ||
// Check that this module is an AMD module, as evidenced by invoking | ||
// `define` at the top-level. Any CommonJS or UMD modules are pass | ||
// through unmodified. | ||
if (parents.length == 2 && parents[0].type == 'Program' && parents[1].type == 'ExpressionStatement') { | ||
isAMD = true; | ||
} | ||
} | ||
}, | ||
leave: function(node) { | ||
@@ -56,2 +89,11 @@ if (isDefine(node)) { | ||
this.break(); | ||
} else if (node.arguments.length == 3 && node.arguments[0].type == 'Literal' && node.arguments[1].type == 'ArrayExpression' && node.arguments[2].type == 'FunctionExpression') { | ||
var dependencies = node.arguments[1] | ||
, factory = node.arguments[2]; | ||
var ids = dependencies.elements.map(function(el) { return el.value }); | ||
var vars = factory.params.map(function(el) { return el.name }); | ||
var reqs = createRequires(ids, vars); | ||
tast = createProgram([reqs].concat(factory.body.body)); | ||
this.break(); | ||
} | ||
@@ -61,3 +103,3 @@ } else if (isReturn(node)) { | ||
if (parents.length == 5 && isDefine(parents[2])) { | ||
if (parents.length == 5 && isDefine(parents[2]) && isAMD) { | ||
return createModuleExport(node.argument); | ||
@@ -69,7 +111,13 @@ } | ||
if (!isAMD) { | ||
stream.queue(data); | ||
stream.queue(null); | ||
return; | ||
} | ||
tast = tast || ast; | ||
console.log('-- TRANSFORMED AST --'); | ||
console.log(util.inspect(tast, false, null)); | ||
console.log('---------------------'); | ||
//console.log('-- TRANSFORMED AST --'); | ||
//console.log(util.inspect(tast, false, null)); | ||
//console.log('---------------------'); | ||
@@ -105,2 +153,4 @@ var out = escodegen.generate(tast); | ||
for (var i = 0, len = ids.length; i < len; ++i) { | ||
if (['require', 'module', 'exports'].indexOf(ids[i]) != -1) { continue; } | ||
decls.push({ type: 'VariableDeclarator', | ||
@@ -107,0 +157,0 @@ id: { type: 'Identifier', name: vars[i] }, |
{ | ||
"name": "deamdify", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Browserify transform that converts AMD to CommonJS.", | ||
@@ -8,2 +8,3 @@ "keywords": [ | ||
"transform", | ||
"requirejs", | ||
"amd" | ||
@@ -10,0 +11,0 @@ ], |
# deAMDify | ||
This module is a [browserify](http://browserify.org/) plugin that will transform | ||
[AMD](https://github.com/amdjs) modules into [Node.js](http://nodejs.org/)-style | ||
modules so that they can be included in browser-ified bundles. | ||
With this transform in place, Node and AMD modules can be freely intermixed, and | ||
the resulting bundle can be used without the need for a separate loader such as | ||
[RequireJS](http://requirejs.org/). | ||
## Install | ||
$ npm install deamdify | ||
## Usage | ||
#### Command Line | ||
Bundle up all required modules, including AMD modules, into a single file | ||
using `browserify` with the `deamdify` transform. | ||
browserify -t deamdify main.js -o bundle.js | ||
#### API | ||
```javascript | ||
var browserify = require('browserify'); | ||
var fs = require('fs'); | ||
var b = browserify('main.js'); | ||
b.transform('deamdify'); | ||
b.bundle().pipe(fs.createWriteStream('bundle.js')); | ||
``` | ||
## Tests | ||
$ npm install | ||
$ make test | ||
[![Build Status](https://secure.travis-ci.org/jaredhanson/deamdify.png)](http://travis-ci.org/jaredhanson/deamdify) [![David DM](https://david-dm.org/jaredhanson/deamdify.png)](http://david-dm.org/jaredhanson/deamdify) | ||
## Credits | ||
@@ -4,0 +44,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9104
148
52
0
5