async-define
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -11,3 +11,2 @@ #! /usr/bin/env node | ||
var minified = argv.minified || argv.m; | ||
var fragmentPath = argv.fragment || argv.f; | ||
@@ -27,2 +26,3 @@ var importVar = argv.import || argv.i; | ||
var script = scriptPath ? fs.readFileSync(scriptPath, {encoding: 'utf8'}) : ''; | ||
var scriptName = scriptPath ? path.basename(scriptPath) : ''; | ||
@@ -35,3 +35,3 @@ console.log(asyncDefineBundle({ | ||
script: script, | ||
minified: minified, | ||
scriptName: scriptName | ||
})); | ||
@@ -38,0 +38,0 @@ process.exit(); |
@@ -8,7 +8,12 @@ (function (w, cb){ | ||
var i, | ||
tmp, depNS, depProp, | ||
len = depNames.length, | ||
deps = []; | ||
for (i = 0; i < len; i++){ | ||
if (depNames[i] in cached_dependencies){ | ||
deps.push(cached_dependencies[depNames[i]]); | ||
for (i = 0; i < len; i++) { | ||
tmp = depNames[i].split('|'); | ||
depNS = tmp[0]; | ||
depProp = tmp[1]; | ||
if (depNS in cached_dependencies){ | ||
deps.push(depProp ? cached_dependencies[depNS][depProp] : cached_dependencies[depNS]); | ||
} | ||
@@ -15,0 +20,0 @@ else { |
{ | ||
"name": "async-define", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Coordinate the execution of asynchronous scripts", | ||
@@ -10,3 +10,3 @@ "main": "lib/asyncDefine.js", | ||
"serve": "http-server -p 9090 demo", | ||
"build": "uglifyjs templates/main.hbs -m -r asyncDefine -c -o templates/main-min.hbs && bin/async-define-bundle.js -f=templates/umd.hbs > lib/asyncDefine.js", | ||
"build": "bin/async-define-bundle.js -f=templates/umd.js > lib/asyncDefine.js", | ||
"build:jquery": "browserify demo/jquery.js -o demo/dist/jquery.js", | ||
@@ -13,0 +13,0 @@ "build:react": "browserify demo/react.js -o demo/dist/react.js", |
@@ -57,2 +57,17 @@ asyncDefine | ||
If one of your library exposes multiple items using an object, you can easily inject these properties in a module: | ||
```js | ||
defineAsync('tools', function (){ | ||
return { | ||
hammer: 'hammer', | ||
spade: 'spade', | ||
drill: 'drill', | ||
}; | ||
}); | ||
defineAsync(['tools|hammer'], function (hammer){ | ||
console.log('Using an ', hammer); | ||
}); | ||
``` | ||
Syntax | ||
@@ -59,0 +74,0 @@ ------ |
var fs = require('fs'); | ||
var path = require('path'); | ||
var hb = require('handlebars'); | ||
var placeholder = '__placeholder__'; | ||
var MagicString = require( 'magic-string' ); | ||
var template = function (templ, str) { | ||
return templ.replace(placeholder, str); | ||
} | ||
var magicTemplate = function (magic, template){ | ||
var fragments = template.split(placeholder); | ||
magic.prepend(fragments[0]); | ||
magic.append(fragments[1]); | ||
return magic; | ||
} | ||
function asyncDefineBundle(opts) { | ||
var fragment; | ||
var fragmentPath = opts.fragmentPath; | ||
@@ -11,29 +26,34 @@ var bundleName = opts.bundleName; | ||
var script = opts.script; | ||
var minified = opts.minified; | ||
var scriptName = opts.scriptName; | ||
var mainTemplate = minified ? '../templates/main-min.hbs' : '../templates/main.hbs'; | ||
var mainTemplate = '../templates/main.hbs'; | ||
var templateStr = fs.readFileSync(path.join(__dirname, mainTemplate), {encoding: 'utf8'}); | ||
var template = function (str) { | ||
return templateStr.replace('asyncDefine()', str); | ||
} | ||
var fragmentTmp; | ||
if (!fragmentPath) { | ||
fragmentTmp = fs.readFileSync(path.join(__dirname, '../templates/wrapper.hbs'), {encoding: 'utf8'}); | ||
if (fragmentPath) { | ||
fragment = fs.readFileSync(fragmentPath, {encoding: 'utf8'}); | ||
return template(templateStr, fragment); | ||
} | ||
else { | ||
fragmentTmp = fs.readFileSync(fragmentPath, {encoding: 'utf8'}); | ||
} | ||
fragmentTmp = fs.readFileSync(path.join(__dirname, '../templates/wrapper.hbs'), {encoding: 'utf8'}); | ||
fragment = hb.compile(fragmentTmp)({ | ||
bundleName: bundleName, | ||
importVar: importVar, | ||
exportVar: exportVar, | ||
depsArgs: Object.keys(importVar).map(function (item) { return importVar[item] ? '___' + item : item; }).join(' ,'), | ||
depsStr: Object.keys(importVar).map(function (item) {return '"' + item + '"';}).join(' ,') | ||
}); | ||
var s = new MagicString(script); | ||
s = magicTemplate(s, fragment); | ||
s = magicTemplate(s, templateStr); | ||
var fragment = hb.compile(fragmentTmp)({ | ||
bundleName: bundleName, | ||
importVar: importVar, | ||
exportVar: exportVar, | ||
script: script, | ||
depsArgs: Object.keys(importVar).map(function (item) { return importVar[item] ? '___' + item : item; }).join(' ,'), | ||
depsStr: Object.keys(importVar).map(function (item) {return '"' + item + '"';}).join(' ,') | ||
}); | ||
var map = s.generateMap({ | ||
source: scriptName, | ||
file: scriptName + '.map', | ||
includeContent: true | ||
}); // generates a v3 sourcemap | ||
return s.toString() + '\n//# sourceMappingURL=' + map.toUrl(); | ||
} | ||
return template(fragment); | ||
@@ -40,0 +60,0 @@ } |
@@ -20,3 +20,3 @@ var asyncDefine = require('../lib/asyncDefine'); | ||
}, 100); | ||
setTimeout(function (){ | ||
@@ -27,3 +27,3 @@ asyncDefine("world", ["hello"], function (hello){ | ||
return hello + " world"; | ||
}); | ||
}); | ||
}, 50); | ||
@@ -38,7 +38,7 @@ }); | ||
}, 100); | ||
setTimeout(function (){ | ||
asyncDefine("b", function (){ | ||
return "b"; | ||
}); | ||
}); | ||
}, 50); | ||
@@ -50,3 +50,3 @@ | ||
return "c"; | ||
}); | ||
}); | ||
}, 75); | ||
@@ -61,3 +61,3 @@ | ||
return "d"; | ||
}); | ||
}); | ||
}, 60); | ||
@@ -75,3 +75,3 @@ | ||
}, 10); | ||
setTimeout(function (){ | ||
@@ -81,3 +81,3 @@ asyncDefine("duplicated", function (hello){ | ||
return "duplicated2"; | ||
}); | ||
}); | ||
}, 20); | ||
@@ -89,6 +89,22 @@ | ||
}, 50); | ||
}); | ||
}); | ||
it("should unpack variables from a namespace", function (done) { | ||
setTimeout(function (){ | ||
asyncDefine("greetings", function (){ | ||
return { | ||
day: "good morning", | ||
night: "good night" | ||
}; | ||
}); | ||
}, 100); | ||
}); | ||
setTimeout(function (){ | ||
asyncDefine(["greetings|day"], function (hello){ | ||
assert.equal(hello, 'good morning'); | ||
done(); | ||
return hello + " world"; | ||
}); | ||
}, 50); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20518
311
203
16