From 051e5dc9816602a03b8fbf0e1ba8fa86e4ab6c0d Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 20 Nov 2014 01:32:05 +0200 Subject: [PATCH] Fixes for external deps and access to `global` variable. --- lib/astConsts.js | 4 +--- lib/index.js | 3 ++- lib/replacer.js | 2 +- lib/templates/external.js | 1 + lib/templates/preamble.js | 14 ++++++++------ lib/templates/umdWrapper.js | 10 +++++----- package.json | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 lib/templates/external.js diff --git a/lib/astConsts.js b/lib/astConsts.js index 7579e42..f72ff52 100644 --- a/lib/astConsts.js +++ b/lib/astConsts.js @@ -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 }); diff --git a/lib/index.js b/lib/index.js index 045064b..25897d2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,7 @@ 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 }); @@ -23,7 +24,7 @@ exports.transformAST = function (inOptions) { 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 diff --git a/lib/replacer.js b/lib/replacer.js index 242febd..48eb7d9 100644 --- a/lib/replacer.js +++ b/lib/replacer.js @@ -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, diff --git a/lib/templates/external.js b/lib/templates/external.js new file mode 100644 index 0000000..b61d973 --- /dev/null +++ b/lib/templates/external.js @@ -0,0 +1 @@ +module.exports = <%= externalId %>; \ No newline at end of file diff --git a/lib/templates/preamble.js b/lib/templates/preamble.js index 794e56d..83f8f4e 100644 --- a/lib/templates/preamble.js +++ b/lib/templates/preamble.js @@ -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; diff --git a/lib/templates/umdWrapper.js b/lib/templates/umdWrapper.js index 38f3097..6ecbd28 100644 --- a/lib/templates/umdWrapper.js +++ b/lib/templates/umdWrapper.js @@ -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 @@ -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; }) %); } }) \ No newline at end of file diff --git a/package.json b/package.json index 1be2ffc..90a2970 100644 --- a/package.json +++ b/package.json @@ -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",