Skip to content

Commit

Permalink
Enable core modules. Allow disabling some modules in specific UMD tar…
Browse files Browse the repository at this point in the history
…gets.

Fixes #14.
  • Loading branch information
RReverser committed Nov 17, 2014
1 parent c20c1cd commit 7faac14
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 45 deletions.
16 changes: 10 additions & 6 deletions lib/parseOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ module.exports = function (inOptions) {
options.deps = [];

for (var name in inOptions.external) {
var dep = inOptions.external[name];
if (dep === true) {
dep = {name: name};
var inDep = inOptions.external[name];
if (inDep === true) {
inDep = {};
}
dep.global = dep.global || dep.name.replace(/\W/g, '');
dep.amd = dep.amd || dep.name;
dep.id = b.identifier('__external_' + (dep.global || deps.length));
var globalName = inDep.global || name.replace(/\W/g, '');
var dep = {
name: inDep.name !== undefined ? inDep.name : name,
global: inDep.global !== undefined ? inDep.global : globalName,
amd: inDep.amd !== undefined ? inDep.amd : name
};
dep.id = b.identifier(inDep.id || ('__external_' + globalName));
options.deps.push(dep);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/pathUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var stdUtils = exports.std = require('path'),
nodeResolve = require('resolve'),
badPathSep = /\\/g,
localRegEx = /^\.\.?\//;
badPathSep = /\\/g;

exports.forceExt = function (path, ext) {
return path + (exports.ext(path) ? '' : '.' + ext);
Expand Down
3 changes: 1 addition & 2 deletions lib/replacer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var Promise = require('./promise'),
pathUtils = require('./pathUtils'),
isCoreModule = require('resolve').isCore,
astTypes = require('ast-types'),
astConsts = require('./astConsts'),
b = astTypes.builders,
Expand Down Expand Up @@ -59,7 +58,7 @@ Replacer.prototype.visit = function (ast) {
func = node.callee,
arg = node.arguments[0];

if (n.Identifier.check(func) && func.name === 'require' && n.Literal.check(arg) && !isCoreModule(arg.value)) {
if (n.Identifier.check(func) && func.name === 'require' && n.Literal.check(arg)) {
func.name = '_require';
replacer.getDependency(arg.value).referenceFrom(arg);
}
Expand Down
9 changes: 6 additions & 3 deletions lib/templates/umdWrapper.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<%
var undef = b.identifier('undefined');
%>
(function (name, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([%= deps.map(function (dep) {
return b.literal(dep.amd);
return dep.amd ? b.literal(dep.amd) : undef;
}) %], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(%= deps.map(function (dep) {
return b.callExpression(b.identifier('require'), [b.literal(dep.name)]);
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 b.memberExpression(b.thisExpression(), b.identifier(dep.global), false);
return dep.global ? b.memberExpression(b.thisExpression(), b.identifier(dep.global), false) : undef;
}) %);
}
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pure-cjs",
"version": "1.17.0",
"version": "1.18.0",
"description": "Pure minimalistic CommonJS builder",
"bin": "./bin/pure-cjs.js",
"main": "./lib/index.js",
Expand All @@ -16,7 +16,7 @@
"davy": "0.3.3",
"escodegen": "git+https://github.com/RReverser/escodegen.git",
"esprima": "^1.2.2",
"estemplate": "^0.3.0",
"estemplate": "^0.4.0",
"event-stream": "^3.1.7",
"resolve": "^1.0.0"
},
Expand Down
16 changes: 11 additions & 5 deletions test/suites/a (exports A with externals)/expected.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/suites/a (exports A with externals)/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion test/suites/a (exports A with externals)/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = {
map: true,
comments: true,
external: {
davy: true
davy: true,
url: {
amd: false,
global: false
}
}
};
13 changes: 8 additions & 5 deletions test/suites/a (exports A with map and comments)/expected.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7faac14

Please sign in to comment.