Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6040974

Browse files
committed
- added Coffee-Script support closes jhnns#8 (jashkenas/coffeescript#2707)
- renamed internalRewire.js to simply rewire.js - moved all the stuff related to manipulating the module environment to moduleEnv.js
1 parent 5fcb70f commit 6040974

File tree

6 files changed

+159
-90
lines changed

6 files changed

+159
-90
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var rewireModule = require("./internalRewire.js");
1+
var rewireModule = require("./rewire.js");
22

33
/**
44
* Adds a special setter and getter to the module located at filename. After the module has been rewired, you can

lib/internalRewire.js

Lines changed: 0 additions & 88 deletions
This file was deleted.

lib/moduleEnv.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"use strict";
2+
3+
var Module = require("module"),
4+
fs = require("fs");
5+
6+
// caching original wrapper
7+
var moduleWrapper0 = Module.wrapper[0],
8+
moduleWrapper1 = Module.wrapper[1],
9+
originalExtensions = {},
10+
nodeRequire,
11+
currentModule;
12+
13+
function load(targetModule) {
14+
nodeRequire = targetModule.require;
15+
targetModule.require = requireProxy;
16+
currentModule = targetModule;
17+
18+
registerExtensions();
19+
targetModule.load(targetModule.id);
20+
21+
// This is only necessary if nothing has been required within the module
22+
reset();
23+
}
24+
25+
function reset() {
26+
Module.wrapper[0] = moduleWrapper0;
27+
Module.wrapper[1] = moduleWrapper1;
28+
restoreExtensions();
29+
}
30+
31+
function inject(prelude, appendix) {
32+
Module.wrapper[0] = moduleWrapper0 + prelude;
33+
Module.wrapper[1] = appendix + moduleWrapper1;
34+
}
35+
36+
/**
37+
* Proxies the first require call in order to draw back all changes to the Module.wrapper.
38+
* Thus our changes don't influence other modules
39+
*
40+
* @param {!String} path
41+
*/
42+
function requireProxy(path) {
43+
reset();
44+
currentModule.require = nodeRequire;
45+
return nodeRequire.call(currentModule, path); // node's require only works when "this" points to the module
46+
}
47+
48+
function registerExtensions() {
49+
originalExtensions.coffee = require.extensions[".coffee"];
50+
require.extensions[".coffee"] = coffeeExtension;
51+
}
52+
53+
function restoreExtensions() {
54+
require.extensions[".coffee"] = originalExtensions.coffee;
55+
}
56+
57+
function coffeeExtension(module, filename) {
58+
var coffee = require("coffee-script"),
59+
content = stripBOM(fs.readFileSync(filename, "utf8"));
60+
61+
content = coffee.compile(content, {
62+
filename: filename,
63+
bare: true
64+
});
65+
module._compile(content, filename);
66+
}
67+
68+
/**
69+
* @see https://github.com/joyent/node/blob/master/lib/module.js
70+
*/
71+
function stripBOM(content) {
72+
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
73+
// because the buffer-to-string conversion in `fs.readFileSync()`
74+
// translates it to FEFF, the UTF-16 BOM.
75+
if (content.charCodeAt(0) === 0xFEFF) {
76+
content = content.slice(1);
77+
}
78+
return content;
79+
}
80+
81+
exports.load = load;
82+
exports.inject = inject;

lib/rewire.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var Module = require("module"),
2+
fs = require("fs"),
3+
__get__ = require("./__get__.js"),
4+
__set__ = require("./__set__.js"),
5+
getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
6+
detectStrictMode = require("./detectStrictMode.js"),
7+
moduleEnv = require("./moduleEnv.js");
8+
9+
/**
10+
* Does actual rewiring the module. For further documentation @see index.js
11+
*/
12+
function internalRewire(parentModulePath, targetPath) {
13+
var targetModule,
14+
prelude,
15+
appendix,
16+
src;
17+
18+
// Checking params
19+
if (typeof targetPath !== "string") {
20+
throw new TypeError("Filename must be a string");
21+
}
22+
23+
// Resolve full filename relative to the parent module
24+
targetPath = Module._resolveFilename(targetPath, parentModulePath);
25+
26+
// Special support for older node versions that returned an array on Module._resolveFilename
27+
// @see https://github.com/joyent/node/blob/865b077819a9271a29f982faaef99dc635b57fbc/lib/module.js#L319
28+
// TODO Remove this switch on the next major release
29+
if (Array.isArray(targetPath)) {
30+
targetPath = targetPath[1];
31+
}
32+
33+
// Create testModule as it would be created by require()
34+
targetModule = new Module(targetPath, parentModulePath);
35+
36+
// We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
37+
prelude = getImportGlobalsSrc();
38+
39+
// We append our special setter and getter.
40+
appendix = "module.exports.__set__ = " + __set__.toString() + "; ";
41+
appendix += "module.exports.__get__ = " + __get__.toString() + "; ";
42+
43+
// Check if the module uses the strict mode.
44+
// If so we must ensure that "use strict"; stays at the beginning of the module.
45+
src = fs.readFileSync(targetPath, "utf8");
46+
if (detectStrictMode(src) === true) {
47+
prelude = ' "use strict"; ' + prelude;
48+
}
49+
50+
moduleEnv.inject(prelude, appendix);
51+
moduleEnv.load(targetModule);
52+
53+
return targetModule.exports;
54+
}
55+
56+
module.exports = internalRewire;

test/internalRewire.test.js renamed to test/rewire.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22
// In case this module was in strict mode, all other modules called by this would also be strict.
33
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
44

5-
describe("internalRewire", function () {
5+
var expect = require("expect.js");
6+
7+
var rewire;
8+
9+
describe("rewire", function () {
610
before(require("./testHelpers/createFakePackageJSON.js"));
711
after(require("./testHelpers/removeFakePackageJSON.js"));
812
it("should pass all shared test cases", function () {
913
require("./testModules/sharedTestCases.js");
1014
});
15+
it("should also work with CoffeeScript", function () {
16+
var coffeeModule;
17+
18+
rewire = require("rewire");
19+
coffeeModule = rewire("./testModules/module.coffee");
20+
coffeeModule.__set__("fs", {
21+
readFileSync: function () {
22+
return "It works!";
23+
}
24+
});
25+
expect(coffeeModule.readFileSync()).to.be("It works!");
26+
});
1127
});

test/testModules/module.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fs = require "fs"
2+
3+
exports.readFileSync = () -> fs.readFileSync()

0 commit comments

Comments
 (0)