montagejs/montage — pull request #1454 by kriskowal (2014-03-26 19:33:57+00:00)
Title: Use relative module identifiers

To open doors for Node.js interoperability.

```js
var config = require("../package.json");
var dependencies = Object.keys(config.dependencies || {})
.concat(Object.keys(config.devDependencies || {}));

var fs = require("fs");
var qfs = require("q-io/fs");
var LineInputStream = require("line-input-stream");
var Require = require("../../v2/mr");

var lines = new LineInputStream(process.stdin);
lines.on("line", function (path) {
if (path === "transform/transform.js")
return;
var packagePath = qfs.relativeFromDirectory(qfs.join(__dirname, ".."), qfs.absolute(path));
console.log("file", path);
var text = fs.readFileSync(path, "utf-8");
text = String(text).replace(/(^|[^\w\$_.])require\s*\(\s*["']([^"']*)["']\s*\)/g, function(_, lead, id) {
var original = id;
if (id === "montage") {
id = "core/core";
}
var top = Require.resolve(id, "") === id;
var external = false;
if (top) {
var parts = id.split("/");
var first = parts[0];
external = dependencies.indexOf(first) >= 0;
if (!external) {
id = qfs.relativeFromFile(packagePath, id);
if (id.indexOf(".", 0) < 0) {
id = "./" + id;
}
}
}
return lead + "require(" + JSON.stringify(id) + ")";
});
fs.writeFileSync(path, text, "utf-8");
});
```