Permalink
Browse files

Add ability to customize the base directory.

Fixes #1.
  • Loading branch information...
1 parent f601a3e commit b3ab7125f844eb07b31fb6bd0830938c0ba83017 @domenic committed Jan 21, 2013
Showing with 28 additions and 10 deletions.
  1. +2 −1 README.md
  2. +9 −9 lib/browserify-deoptimizer.js
  3. +17 −0 test/test.coffee
View
@@ -21,7 +21,8 @@ var bundle = browserify();
bundle.alias("jquery", "jquery-browserify");
bundle.addEntry("start.js");
-var deoptimized = deoptimize(bundle);
+var baseDirectory = process.cwd(); // module IDs will be determined relative to this
+var deoptimized = deoptimize(bundle, baseDirectory);
```
Your `deoptimized` variable will then look something like this:
@@ -7,8 +7,8 @@ var path = require("path");
var urlize = require("./utils").urlize;
var isCoreBuiltin = require("./utils").isCoreBuiltin;
-function getModuleId(file, filePath) {
- return file.target || urlize(path.relative(process.cwd(), filePath));
+function getModuleId(file, filePath, baseDir) {
+ return file.target || urlize(path.relative(baseDir, filePath));
}
function keyFromModuleId(moduleId) {
@@ -17,15 +17,15 @@ function keyFromModuleId(moduleId) {
return moduleId[0] === "/" ? moduleId.substring(1) : moduleId;
}
-function addFilesAndEntries(bundle, result) {
+function addFilesAndEntries(bundle, result, baseDir) {
// The `bundle.files` and `bundle.entries` properties map absolute file paths to unwrapped module bodies. Use them
// to create a map of module IDs to wrapped module bodies.
var allFiles = _.extend({}, bundle.files, bundle.entries);
Object.keys(allFiles).forEach(function (filePath) {
if (!isCoreBuiltin(filePath)) {
var file = allFiles[filePath];
- var moduleId = getModuleId(file, filePath);
+ var moduleId = getModuleId(file, filePath, baseDir);
result[keyFromModuleId(moduleId)] = bundle.wrap(moduleId, file.body);
}
});
@@ -90,29 +90,29 @@ function addAliases(bundle, result) {
result["browserify-aliases.js"] = tempBundle.bundle();
}
-function addEntryRequirer(bundle, result) {
+function addEntryRequirer(bundle, result, baseDir) {
if (Object.keys(bundle.entries).length === 0) {
return;
}
var entryRequires = Object.keys(bundle.entries).reduce(function (soFar, filePath) {
var file = bundle.entries[filePath];
- var moduleId = getModuleId(file, filePath);
+ var moduleId = getModuleId(file, filePath, baseDir);
return soFar + 'require("' + moduleId + '");\n';
}, "");
result["browserify-entry.js"] = entryRequires;
}
-module.exports = function (bundle) {
+module.exports = function (bundle, baseDir) {
var result = Object.create(null);
// The multi-file nature necessitates forcing all exports, otherwise subsequent files don't know what `require` is.
bundle.exports = true;
addPrelude(bundle, result);
addAliases(bundle, result);
- addFilesAndEntries(bundle, result);
- addEntryRequirer(bundle, result);
+ addFilesAndEntries(bundle, result, baseDir);
+ addEntryRequirer(bundle, result, baseDir);
return result;
};
View
@@ -19,6 +19,7 @@ fixtureText = (fileName) => fs.readFileSync(fixturePath(fileName))
wrapped = (moduleId, body) => new BrowserifyWrap({}).wrap(moduleId, body)
wrappedFixture = (fileName) => wrapped(fixtureId(fileName), fixtureText(fileName))
wrappedOther = (moduleId) => wrapped(moduleId, fs.readFileSync(moduleId.substring(1)))
+wrappedWithSpecificId = (moduleId, fileName) => wrapped(moduleId, fs.readFileSync(fileName))
describe "With a simple entry file that `require`s another file", =>
deoptimized = null
@@ -164,3 +165,19 @@ describe "With prepends", =>
preludeContents = deoptimized["browserify-prelude.js"]
expect(preludeContents).to.match(/^var x = 5;/)
+
+describe "With a specific baseDir", =>
+ deoptimized = null
+
+ beforeEach =>
+ bundle = browserify()
+ bundle.addEntry(fixturePath("requires1.js"))
+
+ deoptimized = deoptimize(bundle, __dirname)
+
+ it "should include the entry file and its dependent with a key relative to the baseDir", =>
+ requires1Contents = wrappedWithSpecificId("/fixtures/requires1.js", fixturePath("requires1.js"))
+ expect(deoptimized).to.have.property("fixtures/requires1.js").that.equals(requires1Contents)
+
+ oneContents = wrappedWithSpecificId("/fixtures/1.js", fixturePath("1.js"))
+ expect(deoptimized).to.have.property("fixtures/1.js").that.equals(oneContents)

0 comments on commit b3ab712

Please sign in to comment.