Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

win32 fix and 'excludes' feature #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 50 additions & 22 deletions lib/stitch.js

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

110 changes: 65 additions & 45 deletions src/stitch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ try
module._compile content, filename
catch err

isWindows = process.platform is "win32"

exports.Package = class Package
constructor: (config) ->
@rootModuleName = config.rootModuleName
@identifier = config.identifier ? 'require'
@paths = config.paths ? ['lib']
@dependencies = config.dependencies ? []
@compilers = _.extend {}, compilers, config.compilers
@excludes = config.excludes ? []

@cache = config.cache ? true
@mtimeCache = {}
Expand All @@ -59,54 +62,69 @@ exports.Package = class Package

result = """
(function(/*! Stitch !*/) {
if (!this.#{@identifier}) {
var modules = {}, cache = {}, require = function(name, root) {
var path = expand(root, name), module = cache[path], fn;
if (module) {

var modules = {}, cache = {}, req = function(name, root) {
var path = expand(root, name), module = cache[path], fn;
if (module) {
return module.exports;
} else if (fn = modules[path] || modules[path = expand(path, './index')]) {
module = {id: path, exports: {}};
try {
cache[path] = module;
fn(module.exports, function(name) {
return req(name, dirname(path));
}, module);
return module.exports;
} else if (fn = modules[path] || modules[path = expand(path, './index')]) {
module = {id: path, exports: {}};
try {
cache[path] = module;
fn(module.exports, function(name) {
return require(name, dirname(path));
}, module);
return module.exports;
} catch (err) {
delete cache[path];
throw err;
}
} else {
throw 'module \\'' + name + '\\' not found';
} catch (err) {
delete cache[path];
throw err;
}
}, expand = function(root, name) {
var results = [], parts, part;
if (/^\\.\\.?(\\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
} else {
throw 'module \\'' + name + '\\' not found';
}
}, expand = function(root, name) {
var results = [], parts, part;
if (/^\\.\\.?(\\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
return results.join('/');
}, dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};

return function(bundle) {
for (var key in bundle){
modules[key] = bundle[key];
}

//UMD
if (typeof define === 'function' && define.amd) {
// AMD. Register as a named module
define([],function(){
return req('#{@rootModuleName}','');
});
} else {
// Browser globals
this.#{@rootModuleName} = req('#{@rootModuleName}','');

//define global require
if (!this.#{@identifier}) {
this.#{@identifier} = function(name) {
return req(name, '');
}
}
return results.join('/');
}, dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
this.#{@identifier} = function(name) {
return require(name, '');
}
this.#{@identifier}.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
}
return this.#{@identifier}.define;
};
}).call(this)({
"""

Expand Down Expand Up @@ -171,9 +189,11 @@ exports.Package = class Package
return callback err if err

for expandedPath in expandedPaths
base = expandedPath + "/"
base = expandedPath + (if isWindows then "\\" else "/")
if sourcePath.indexOf(base) is 0
return callback null, sourcePath.slice base.length
sp = sourcePath.slice base.length
if isWindows then sp = sp.replace /\\/g, "/" #replace backslashes with forward slashes
return callback null,sp
callback new Error "#{path} isn't in the require path"

compileFile: (path, callback) ->
Expand Down Expand Up @@ -208,8 +228,8 @@ exports.Package = class Package
return callback err if err

async.forEach files, (file, next) =>
return next() if file.match /^\./
filename = join directory, file
return next() if (filename in @excludes) or file.match /^\./

fs.stat filename, (err, stats) =>
@mtimeCache[filename] = stats?.mtime?.toString()
Expand Down