Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable const support #118

Merged
merged 9 commits into from
Nov 11, 2017
Merged
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
- "8"
- "9"

script:
- npm test
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ myModule.__with__({
Limitations
-----------

**Using `const`**<br>
It's not possible to rewire `const` (see [#79](https://github.com/jhnns/rewire/issues/79)). This can probably be solved with [proxies](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) someday but requires further research.

**Transpilers**<br>
Some transpilers, like babel, rename variables in order to emulate certain language features. Rewire will not work in these cases (see [#62](https://github.com/jhnns/rewire/issues/62)). A possible solution might be switching to [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire).

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function rewire(filename) {

module.exports = rewire;

delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
31 changes: 29 additions & 2 deletions lib/moduleEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

var Module = require("module"),
fs = require("fs"),
babelCore = require("babel-core"),
// Requiring the babel plugin here because otherwise it will be lazy-loaded by Babel during rewire()
transformBlockScoping = require("babel-plugin-transform-es2015-block-scoping"),
coffee;

// caching original wrapper
var moduleWrapper0 = Module.wrapper[0],
moduleWrapper1 = Module.wrapper[1],
originalExtensions = {},
matchCoffeeExt = /\.coffee$/,
nodeRequire,
currentModule;

Expand All @@ -26,7 +29,6 @@ function load(targetModule) {
function reset() {
Module.wrapper[0] = moduleWrapper0;
Module.wrapper[1] = moduleWrapper1;
restoreExtensions();
}

function inject(prelude, appendix) {
Expand All @@ -47,23 +49,48 @@ function requireProxy(path) {
}

function registerExtensions() {
var originalJsExtension = require.extensions[".js"];
var originalCoffeeExtension = require.extensions[".coffee"];

if (originalJsExtension) {
originalExtensions.js = originalJsExtension;
}
if (originalCoffeeExtension) {
originalExtensions.coffee = originalCoffeeExtension;
}
require.extensions[".js"] = jsExtension;
require.extensions[".coffee"] = coffeeExtension;
}

function restoreExtensions() {
if ("js" in originalExtensions) {
require.extensions[".js"] = originalExtensions.js;
}
if ("coffee" in originalExtensions) {
require.extensions[".coffee"] = originalExtensions.coffee;
}
}

function jsExtension(module, filename) {
var _compile = module._compile;

module._compile = function (content, filename) {
content = babelCore.transform(content, {
plugins: ["transform-es2015-block-scoping"],
retainLines: true,
filename: filename
}).code;
_compile.call(module, content, filename);
};

restoreExtensions();
originalExtensions.js(module, filename);
}

function coffeeExtension(module, filename) {
var content = stripBOM(fs.readFileSync(filename, "utf8"));

restoreExtensions();
content = coffee.compile(content, {
filename: filename,
bare: true
Expand Down
1 change: 1 addition & 0 deletions lib/rewire.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Module = require("module"),
fs = require("fs"),
path = require("path"),
getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
getDefinePropertySrc = require("./getDefinePropertySrc.js"),
detectStrictMode = require("./detectStrictMode.js"),
Expand Down
Loading