Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with global external dependency #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/pathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ exports.getNodePath = function (from, to, options) {

exports.ext = function (path) {
return stdUtils.extname(path).slice(1).toLowerCase();
};

//
// path for external dependencies
// I'm not sure if this is working by coincidence
exports.getNodePathExternal = function (from, to, options) {

var fromDir = from;
var opts = {
basedir: stdUtils.resolve('', fromDir),
extensions: (options.defaultExt !== 'js' ? ['.' + options.defaultExt] : []).concat(['.js', '.json']),
moduleDirectory: options.moduleDir
};


var resolved = nodeResolve.sync(to, opts);

return exports.normalizePath(resolved);

};
16 changes: 15 additions & 1 deletion lib/replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ function Replacer(options) {
this.refs = [];

if (options.externalId) {
this.promise = Promise.resolve([b.returnStatement(options.externalId)]);
// global external dependency should be assigned
// to module.exports.
// maybe this not work for amd?
this.promise = Promise.resolve(
[
b.expressionStatement(
b.assignmentExpression(
'=',
b.memberExpression(
b.identifier('module'),
b.identifier('exports'),
false
),
options.externalId
))]);
} else {
this.promise = this.map.getFileAST({
source: this.path,
Expand Down
13 changes: 11 additions & 2 deletions lib/replacerMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@ var Promise = require('./promise'),
pathUtils = require('./pathUtils');

function ReplacerMap(options) {

this.replacers = {};
this.promises = [];
this.getFileAST = options.getFileAST;
this.comments = options.comments;
this.defaultExt = options.defaultExt;
this.moduleDir = options.moduleDir;

this.depsMap = options.deps.reduce(function (obj, dep) {
obj[pathUtils.getNodePath('_', dep.name, this)] = dep;
if (!dep.global && !dep.amd) {
var path = pathUtils.getNodePath('_', dep.name, this);
obj[path] = dep;
} else {
// use different path for external dependencies?
obj[pathUtils.getNodePathExternal('_', dep.name, this)] = dep;
}

return obj;
}.bind(this), {});

}

ReplacerMap.prototype.get = function (path) {
if (path in this.replacers) {
return this.replacers[path];
}

var id = this.promises.length++,
replacer = this.replacers[path] = new Replacer({
map: this,
Expand Down