Skip to content

Loading…

Amd support #21

Merged
merged 3 commits into from

2 participants

@kennethkufluk
Collaborator

Patch for amd support.

Kenneth Kufluk added some commits
Kenneth Kufluk Add AMD dependency support. a4852f8
Kenneth Kufluk bump 0c6e1d4
@shinypb shinypb commented on the diff
lib/loadbuilder/builder.js
@@ -262,7 +262,7 @@ builder.matchers.add = function(regex, factory) {
this.unshift([regex, factory]);
}
-asset.Module.regexp = /^\.?[a-zA-Z0-9_\-\/]+$/;
+asset.Module.regexp = /^\.?[a-zA-Z0-9_\/.-]+$/;
@shinypb Collaborator
shinypb added a note

Why this change? Won't it make it match any character?

@kennethkufluk Collaborator

No. Why would it do that?
/./ Match any character
/[.]/ Match full stop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Kenneth Kufluk Escape your dots. 9de82ca
@kennethkufluk kennethkufluk merged commit 13b55da into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 3, 2012
  1. Add AMD dependency support.

    Kenneth Kufluk committed
  2. bump

    Kenneth Kufluk committed
Commits on Aug 4, 2012
  1. Escape your dots.

    Kenneth Kufluk committed
This page is out of date. Refresh to see the latest.
View
2 Makefile
@@ -1,2 +1,2 @@
test_all:
- NODE_PATH=./lib `npm bin`/expresso -g test/*.js
+ NODE_PATH=./lib `npm bin`/expresso -g test/*_test.js
View
83 lib/loadbuilder/asset.js
@@ -3,7 +3,7 @@ var util = require('./util'),
path = require('path'),
jshint = require('jshint').JSHINT,
file = require('./file');
-
+Error.stackTraceLimit = 100;
var ARRAY_ARG = {
type: "ArrayExpression",
elements: null
@@ -25,7 +25,6 @@ util.extend(Script.prototype, {
var usings, dependencies = [];
usings = this.fromFile().usings();
-
usings.forEach(function(call) {
var args = call.values[0];
@@ -111,12 +110,17 @@ util.extend(Module.prototype, {
toSource: function() {
if (this.isCJS()) {
return this.amdWrappedSource();
+ } else if (this.isAMD()) {
+ return this.addIdToAMD();
} else {
- return this.addId();
+ return this.addIdToProvide();
}
},
isCJS: function() {
- return !this.fromFile().provides().length;
+ return !this.fromFile().provides().length && !this.isAMD();
+ },
+ isAMD: function() {
+ return this.fromFile().defines().length && !this.fromFile().provides().length;
},
amdWrappedSource: function() {
var deps = ['module', 'require', 'exports'].concat(this.dependencies().map(function(d) { return d.id; })),
@@ -127,42 +131,75 @@ util.extend(Module.prototype, {
return preamble + this.fromFile().generate() + postamble;
},
- dependencies: function() {
+ dependencies: function(parent) {
if (this.isCJS()) {
- return this.dependenciesFromRequire();
+ return this.dependenciesFromRequire(parent);
+ } else if (this.isAMD()) {
+ return this.dependenciesFromAMD(parent);
} else {
return Script.prototype.dependencies.call(this);
}
},
- dependenciesFromRequire: function() {
+ getAsset: function(dep, parent) {
+ var asset = this.builder.matchAsset(resolveRelativePath(dep, parent), false);
+ return (asset.length==0) ? null : asset;
+ },
+ dependenciesFromRequire: function(parent) {
var requires = this.fromFile().requires();
-
return requires.map(function(r) {
- var asset = this.builder.matchAsset(r.values[0][0].value, false);
- return (asset.length==0) ? null : asset;
+ return this.getAsset(r.values[0][0].value, parent);
+ }, this).filter(function(item){ return item!=null; })
+ },
+ dependenciesFromAMD: function(parent) {
+ var defines = analyzer.analyze(ARRAY_ARG, this.fromFile().defines()[0].values);
+ var deps = defines[0].values[0].filter(function(item){
+ return ['module', 'exports', 'require'].indexOf(item.value) == -1;
+ }).map(function(dep) {
+ return this.getAsset(dep.value, parent);
}, this).filter(function(item){ return item!=null; });
+ return deps;
},
- addId: function() {
+ addIdToProvide: function() {
var provides = this.fromFile().provides(),
- tree = this.ast(),
- provide;
+ tree = this.ast();
if (provides.length > 0) {
- provide = provides[0];
-
- // TODO make this nice - maybe have a transform function?
- if (analyzer.match(STRING_ARG, provide.values[0][0]) == null) {
- provide.parent.expression.arguments.unshift({
- type: "Literal",
- value: this.id
- });
- this.fromFile().taintAst();
- }
+ this.addId(provides[0])
+ }
+ return this.fromFile().generate();
+ },
+ addIdToAMD: function() {
+ var defines = this.fromFile().defines(),
+ tree = this.ast();
+
+ if (defines.length > 0) {
+ this.addId(defines[0]);
}
return this.fromFile().generate();
+ },
+ addId: function(subTree) {
+ // Always regenerate the ID.
+ var args = subTree.parent.expression.arguments;
+ if (args[0] && args[0].type == 'Literal') {
+ args.shift();
+ }
+ subTree.parent.expression.arguments.unshift({
+ type: "Literal",
+ value: this.id
+ });
+ this.fromFile().taintAst();
}
});
+function resolveRelativePath(id, mod) {
+ // replace the './' on the id with the dir taken from the mod id.
+ var from = (mod && mod.id) || '';
+ var parts = from.split('/'); parts.pop();
+ var dir = parts.join('/');
+ var filepath = path.join(dir, id);
+ return (dir.length && id.match(/^\./)) ? filepath : id;
+}
+
module.exports = {
Script: Script,
Module: Module
View
4 lib/loadbuilder/builder.js
@@ -24,7 +24,7 @@ function collect(excluded, assets, includeDependencies) {
var deps = [];
if (excluded.indexOf(asset.id) < 0) {
if (includeDependencies) {
- deps = asset.dependencies();
+ deps = asset.dependencies(asset);
}
excluded = excluded.concat([asset.id]);
@@ -262,7 +262,7 @@ builder.matchers.add = function(regex, factory) {
this.unshift([regex, factory]);
}
-asset.Module.regexp = /^\.?[a-zA-Z0-9_\-\/]+$/;
+asset.Module.regexp = /^\.?[a-zA-Z0-9_\/.-]+$/;
@shinypb Collaborator
shinypb added a note

Why this change? Won't it make it match any character?

@kennethkufluk Collaborator

No. Why would it do that?
/./ Match any character
/[.]/ Match full stop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
builder.matchers.add(asset.Module.regexp, function(id) {
return new asset.Module(id);
})
View
20 lib/loadbuilder/file.js
@@ -19,6 +19,14 @@ var REQUIRE = {
},
arguments: null
};
+var DEFINE = {
+ type: "CallExpression",
+ callee: {
+ type: "Identifier",
+ name: "define"
+ },
+ arguments: null
+};
var PROVIDE = {
type: "CallExpression",
callee: {
@@ -111,6 +119,18 @@ util.extend(File.prototype, {
}
return this.derived.requires;
},
+ defines: function() {
+ if (!this.derived.defines) {
+ this.derived.defines = this.analyzer().analyze(DEFINE, this.ast());
+ }
+ return this.derived.defines;
+ },
+ requires: function() {
+ if (!this.derived.requires) {
+ this.derived.requires = this.analyzer().analyze(REQUIRE, this.ast());
+ }
+ return this.derived.requires;
+ },
provides: function() {
if (!this.derived.provides) {
this.derived.provides = this.analyzer().analyze(PROVIDE, this.ast());
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "loadbuilder",
- "version": "0.3.1",
+ "version": "0.3.2",
"description": "Combine and compress dependency chains created by Loadrunner",
"contributors": [{ "name": "Dan Webb", "email": "dan@danwebb.net" }, { "name": "Kenneth Kufluk", "email": "kenneth@kufluk.com" }],
"homepage": "https://github.com/danwrong/loadbuilder",
View
0 test/analyzer.js → test/analyzer_test.js
File renamed without changes.
View
23 test/asset.js → test/asset_test.js
@@ -71,5 +71,28 @@ module.exports = {
assert.equal('define("common",["module","require","exports","anon"],' +
'function(module, require, exports) {\nvar a = require(\'anon\');\n});', a.toSource());
+ },
+ testShouldNotWrapAMDModule: function() {
+ var a = new asset.Module('subfolder/amd');
+
+ a.builder = builder({
+ docroot: __dirname,
+ path: '/modules'
+ });
+
+ assert.equal("define('subfolder/amd', [\n './amd_dep',\n '../../fixtures/dep1',\n 'module',\n 'require',\n 'exports'\n], " +
+ "function (common, dep1, module, require, exports) {\n var a = require('./common');\n return a;\n});",
+ a.toSource());
+ },
+ testShouldAddNameAMDModule: function() {
+ var a = new asset.Module('subfolder/amd_anon');
+
+ a.builder = builder({
+ docroot: __dirname,
+ path: '/modules'
+ });
+
+ assert.equal("define('subfolder/amd_anon', [\n './common'\n], function (common) {\n return common;\n});",
+ a.toSource());
}
}
View
7 test/builder.js → test/builder_test.js
@@ -39,6 +39,13 @@ module.exports = {
builder(opts).include('fixtures/has_dep.js').toSource()
);
},
+ testShouldCollectAMDDependencies: function() {
+ var expected = "define('subfolder/amd_dep', [\n 'module',\n 'require',\n 'exports'\n], function (common, dep1, module, require, exports) {\n var a = require('./common');\n return a;\n});\ndefine(\"../fixtures/dep1\",[\"module\",\"require\",\"exports\"],function(module, require, exports) {\nusing('fixtures/dep1dep.js');\n});\ndefine('subfolder/amd', [\n './amd_dep',\n '../../fixtures/dep1',\n 'module',\n 'require',\n 'exports'\n], function (common, dep1, module, require, exports) {\n var a = require('./common');\n return a;\n});";
+ assert.equal(
+ expected,
+ builder(opts).include('subfolder/amd').toSource()
+ );
+ },
testShouldGenerateManifestList: function() {
assert.deepEqual(
["fixtures/dep1dep.js","fixtures/dep1.js","fixtures/dep2.js","fixtures/has_dep.js"],
View
7 test/modules/subfolder/amd.js
@@ -0,0 +1,7 @@
+define("amd",
+ ['./amd_dep', '../../fixtures/dep1', "module", "require", "exports"],
+ function(common, dep1, module, require, exports) {
+ var a = require('./common');
+ return a;
+ }
+);
View
5 test/modules/subfolder/amd_anon.js
@@ -0,0 +1,5 @@
+define(['./common'],
+ function(common) {
+ return common;
+ }
+);
View
6 test/modules/subfolder/amd_dep.js
@@ -0,0 +1,6 @@
+define(["module", "require", "exports"],
+ function(common, dep1, module, require, exports) {
+ var a = require('./common');
+ return a;
+ }
+);
Something went wrong with that request. Please try again.