Skip to content

Commit

Permalink
Fixes for external deps and access to global variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Nov 19, 2014
1 parent 3e81707 commit 051e5dc
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
4 changes: 1 addition & 3 deletions lib/astConsts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ exports.moduleArgs = [b.identifier('module'), b.identifier('exports')];

exports.tmpl = {};

['preamble', 'umdWrapper'].forEach(function (name) {
['external', 'preamble', 'umdWrapper'].forEach(function (name) {
var tmpl = estemplate.compile(fs.readFileSync(__dirname + '/templates/' + name + '.js', 'utf-8'), {
loc: true,
source: name + '.js',
comment: true,
attachComment: true
});
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ exports.transformAST = function (inOptions) {
return map.whenAll().then(function (modules) {
var factoryExpr = astConsts.tmpl.preamble({
deps: options.deps,
exports: options.exports,
modules: modules,
replacer: replacer
});

return {
ast: b.program([b.expressionStatement(
options.exports
? b.callExpression(astConsts.tmpl.umdWrapper(options), [b.literal(options.exports), factoryExpr])
? b.callExpression(astConsts.tmpl.umdWrapper(options), [factoryExpr])
: b.callExpression(factoryExpr, [])
)]),
options: options
Expand Down
2 changes: 1 addition & 1 deletion lib/replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Replacer(options) {
this.refs = [];

if (options.externalId) {
this.promise = Promise.resolve([b.returnStatement(options.externalId)]);
this.promise = Promise.resolve([b.expressionStatement(astConsts.tmpl.external(options))]);
} else {
this.promise = this.map.getFileAST({
source: this.path,
Expand Down
1 change: 1 addition & 0 deletions lib/templates/external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <%= externalId %>;
14 changes: 8 additions & 6 deletions lib/templates/preamble.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
(function (%= deps.map(function (dep) { return dep.id }) %, define) {
function _require(index) {
var module = _require.cache[index];

(function (%= deps.map(function (dep) { return dep.id }) %) {
var global = this, define;

function _require(id) {
var module = _require.cache[id];

if (!module) {
var exports = {};
module = _require.cache[index] = {id: index, exports: exports};
_require.modules[index].call(exports, module, exports);
module = _require.cache[id] = {id: id, exports: exports};
_require.modules[id].call(exports, module, exports);
}

return module.exports;
Expand Down
10 changes: 5 additions & 5 deletions lib/templates/umdWrapper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<%
var undef = b.identifier('undefined');
%>
(function (name, factory) {
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([%= deps.map(function (dep) {
return dep.amd ? b.literal(dep.amd) : undef;
return b.literal(dep.amd || '');
}) %], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
Expand All @@ -15,9 +15,9 @@ var undef = b.identifier('undefined');
return dep.name ? b.callExpression(b.identifier('require'), [b.literal(dep.name)]) : undef;
}) %);
} else {
// Browser globals (root is window)
this[name] = factory(%= deps.map(function (dep) {
return dep.global ? b.memberExpression(b.thisExpression(), b.identifier(dep.global), false) : undef;
// Browser globals
this.<%= b.identifier(exports) %> = factory(%= deps.map(function (dep) {
return dep.global ? b.identifier(dep.global) : undef;
}) %);
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pure-cjs",
"version": "1.18.1",
"version": "1.18.2",
"description": "Pure minimalistic CommonJS builder",
"bin": "./bin/pure-cjs.js",
"main": "./lib/index.js",
Expand Down

0 comments on commit 051e5dc

Please sign in to comment.