Support __filename and __dirname.

Part of #6055.
Fixes #6022.
This commit is contained in:
Ben Newman
2016-02-13 00:01:29 -05:00
parent 3ecc3e43a9
commit c368d98593
5 changed files with 28 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
{
"dependencies": {
"install": {
"version": "0.4.2"
"version": "0.4.4"
}
}
}

View File

@@ -7,7 +7,7 @@ Package.describe({
});
Npm.depends({
install: "0.4.2"
install: "0.4.4"
});
Package.onUse(function(api) {

View File

@@ -495,14 +495,30 @@ _.extend(File.prototype, {
_getClosureHeader() {
if (this._useMeteorInstall()) {
var header = "";
if (this.deps.length > 0) {
header += "[";
_.each(this.deps, dep => {
header += JSON.stringify(dep) + ",";
});
}
return header + "function(require,exports,module){";
const headerParts = [
header,
"function(require,exports,module"
];
if (this.source.match(/\b__dirname\b/)) {
headerParts.push(",__filename,__dirname");
} else if (this.source.match(/\b__filename\b/)) {
headerParts.push(",__filename");
}
headerParts.push("){");
return headerParts.join("");
}
return "(function(){";
},

View File

@@ -6,6 +6,7 @@
"dependencies": {
"events": "^1.1.0",
"moment": "2.11.1",
"path": "^0.12.7",
"regenerator": "^0.8.42",
"stream-browserify": "^2.0.1",
"util": "^0.10.3"

View File

@@ -90,6 +90,14 @@ describe("app modules", () => {
let foo = 1234;
delete foo;
});
it("should have access to filename and dirname", () => {
assert.strictEqual(require(__filename), exports);
assert.strictEqual(
require("path").relative(__dirname, __filename),
"tests.js"
);
});
});
describe("template modules", () => {