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

Skip to content

Commit 76271ce

Browse files
committed
remove unneeded code from rewire for coderoad usage
1 parent 673c285 commit 76271ce

File tree

9 files changed

+74
-116
lines changed

9 files changed

+74
-116
lines changed

lib/__get__.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
*/
1212
function __get__(varName) {
1313
try {
14-
if (varName && typeof varName === "string") {
14+
if (varName && typeof varName === 'string') {
1515
return eval(varName);
1616
} else {
17-
throw new TypeError("__get__ expects a non-empty string");
17+
throw new TypeError('__get__ expects a non-empty string');
1818
}
1919
} catch(e) {
2020
// does not exist

lib/detectImports.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const import = /\bimport\s+(?:.+\s+from\s+)?[\'"]([^"\']+)["\']/;
2+
3+
// detects imports
4+
// adds imports to global scope of rewired object
5+
function detectImports(file) {
6+
7+
}
8+
9+
module.exports = detectImports;

lib/detectStrictMode.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var multiLineComment = /^\s*\/\*.*?\*\//;
2-
var singleLineComment = /^\s*\/\/.*?[\r\n]/;
3-
var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;
1+
const multiLineComment = /^\s*\/\*.*?\*\//;
2+
const singleLineComment = /^\s*\/\/.*?[\r\n]/;
3+
const strictMode = /^\s*(?:["']use strict["'])[ \t]*(?:[\r\n]|;)/;
44

55
/**
66
* Returns true if the source code is intended to run in strict mode. Does not detect
@@ -13,15 +13,17 @@ function detectStrictMode(src) {
1313
var singleLine;
1414
var multiLine;
1515

16-
while ((singleLine = singleLineComment.test(src)) || (multiLine = multiLineComment.test(src))) {
16+
while (
17+
(singleLine = singleLineComment.test(src))
18+
|| (multiLine = multiLineComment.test(src))
19+
) {
1720
if (singleLine) {
18-
src = src.replace(singleLineComment, "");
21+
src = src.replace(singleLineComment, '');
1922
}
2023
if (multiLine) {
21-
src = src.replace(multiLineComment, "");
24+
src = src.replace(multiLineComment, '');
2225
}
2326
}
24-
2527
return strictMode.test(src);
2628
}
2729

lib/fileExists.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var fs = require('fs');
2+
3+
function fileExists(path) {
4+
try {
5+
fs.accessSync(path, fs.F_OK);
6+
}
7+
catch (e) {
8+
if (e) {
9+
console.log(e);
10+
}
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
module.exports = fileExists;

lib/getDefinePropertySrc.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
"use strict";
1+
'use strict';
22

3-
var __get__ = require("./__get__.js");
3+
const __get__ = require("./__get__.js");
44

5-
var srcs = {
5+
const srcs = {
66
"__get__": __get__.toString(),
77
};
88

99
function getDefinePropertySrc() {
10-
var src = "if (typeof(module.exports) === 'function' || \n" +
11-
"(typeof(module.exports) === 'object' && module.exports !== null && Object.isExtensible(module.exports))) {\n";
12-
13-
src += Object.keys(srcs).reduce(function forEachSrc(preValue, value) {
14-
return preValue += "Object.defineProperty(module.exports, '" +
15-
value +
16-
"', {enumerable: false, value: " +
17-
srcs[value] +
18-
", "+
19-
"writable: true}); ";
20-
}, "");
21-
22-
src += "\n}";
23-
24-
return src;
10+
return `
11+
if (typeof(module.exports) === 'function' ||
12+
(typeof(module.exports) === 'object' && module.exports !== null && Object.isExtensible(module.exports))) {
13+
${Object.keys(srcs).reduce((preValue, value) => {
14+
return preValue += `Object.defineProperty(module.exports, '${value}', {enumerable: false, value: ${srcs[value]}, writable: true});`;
15+
}, '')
16+
}
17+
}`;
2518
}
2619

2720
module.exports = getDefinePropertySrc;

lib/getImportGlobalsSrc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
function getImportGlobalsSrc(ignore) {
1111
var key,
1212
value,
13-
src = "",
14-
globalObj = typeof global === "undefined"? window: global;
13+
src = '',
14+
globalObj = typeof global === 'undefined' ? window : global;
1515

1616
ignore = ignore || [];
1717
// global itself can't be overridden because it's the only reference to our real global objects
18-
ignore.push("global");
18+
ignore.push('global');
1919
// ignore 'module', 'exports' and 'require' on the global scope, because otherwise our code would
2020
// shadow the module-internal variables
2121
// @see https://github.com/jhnns/rewire-webpack/pull/6
22-
ignore.push("module", "exports", "require");
22+
ignore.push('module', 'exports', 'require');
2323

2424
for (key in globalObj) { /* jshint forin: false */
2525
if (ignore.indexOf(key) !== -1) {
@@ -29,8 +29,8 @@ function getImportGlobalsSrc(ignore) {
2929

3030
// key may be an invalid variable name (e.g. 'a-b')
3131
try {
32-
eval("var " + key + ";");
33-
src += "var " + key + " = global." + key + "; ";
32+
eval(`var ${key};`);
33+
src += `var ${key} = global.${key};`;
3434
} catch(e) {}
3535
}
3636

lib/index.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
var rewireModule = require("./rewire.js");
2-
var fs = require('fs');
3-
4-
function fileExists(path, silent) {
5-
if (silent === void 0) { silent = true; }
6-
try {
7-
fs.accessSync(path, fs.F_OK);
8-
}
9-
catch (e) {
10-
if (e) {
11-
if (!silent) {
12-
console.log(e);
13-
}
14-
return false;
15-
}
16-
}
17-
return true;
18-
}
1+
var rewireModule = require('./rewire.js');
2+
var fileExists = require('./fileExists');
193

204
/**
215
* Adds a special setter and getter to the module located at filename. After the module has been rewired, you can
@@ -28,7 +12,7 @@ function rewire(filename) {
2812
// is not a package path
2913
if (!filename.match(/^[a-zA-Z]/)) {
3014
// if the file doesn't exist yet,
31-
// set to get undefined
15+
// create a __get__ mock to prevent tests breaking
3216
if (!fileExists(filename)) {
3317
return {
3418
__get__: () => {}
@@ -41,4 +25,5 @@ function rewire(filename) {
4125

4226
module.exports = rewire;
4327

44-
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
28+
delete require.cache[__filename];
29+
// deleting self from module cache so the parent module is always up to date

lib/moduleEnv.js

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
"use strict";
1+
'use strict';
22

3-
var Module = require("module"),
4-
fs = require("fs"),
5-
coffee;
3+
var Module = require('module'),
4+
fs = require('fs');
65

76
// caching original wrapper
87
var moduleWrapper0 = Module.wrapper[0],
@@ -16,7 +15,6 @@ function load(targetModule) {
1615
targetModule.require = requireProxy;
1716
currentModule = targetModule;
1817

19-
registerExtensions();
2018
targetModule.load(targetModule.id);
2119

2220
// This is only necessary if nothing has been required within the module
@@ -26,7 +24,6 @@ function load(targetModule) {
2624
function reset() {
2725
Module.wrapper[0] = moduleWrapper0;
2826
Module.wrapper[1] = moduleWrapper1;
29-
restoreExtensions();
3027
}
3128

3229
function inject(prelude, appendix) {
@@ -46,49 +43,5 @@ function requireProxy(path) {
4643
return nodeRequire.call(currentModule, path); // node's require only works when "this" points to the module
4744
}
4845

49-
function registerExtensions() {
50-
var originalCoffeeExtension = require.extensions[".coffee"];
51-
52-
if (originalCoffeeExtension) {
53-
originalExtensions.coffee = originalCoffeeExtension;
54-
}
55-
require.extensions[".coffee"] = coffeeExtension;
56-
}
57-
58-
function restoreExtensions() {
59-
if ("coffee" in originalExtensions) {
60-
require.extensions[".coffee"] = originalExtensions.coffee;
61-
}
62-
}
63-
64-
function coffeeExtension(module, filename) {
65-
var content = stripBOM(fs.readFileSync(filename, "utf8"));
66-
67-
content = coffee.compile(content, {
68-
filename: filename,
69-
bare: true
70-
});
71-
module._compile(content, filename);
72-
}
73-
74-
/**
75-
* @see https://github.com/joyent/node/blob/master/lib/module.js
76-
*/
77-
function stripBOM(content) {
78-
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
79-
// because the buffer-to-string conversion in `fs.readFileSync()`
80-
// translates it to FEFF, the UTF-16 BOM.
81-
if (content.charCodeAt(0) === 0xFEFF) {
82-
content = content.slice(1);
83-
}
84-
return content;
85-
}
86-
87-
try {
88-
coffee = require("coffee-script");
89-
} catch (err) {
90-
// We are not able to provide coffee-script support, but that's ok as long as the user doesn't want it.
91-
}
92-
9346
exports.load = load;
9447
exports.inject = inject;

lib/rewire.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var Module = require("module"),
2-
fs = require("fs"),
3-
getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
4-
getDefinePropertySrc = require("./getDefinePropertySrc.js"),
5-
detectStrictMode = require("./detectStrictMode.js"),
6-
moduleEnv = require("./moduleEnv.js");
1+
var Module = require('module'),
2+
fs = require('fs'),
3+
getImportGlobalsSrc = require('./getImportGlobalsSrc.js'),
4+
getDefinePropertySrc = require('./getDefinePropertySrc.js'),
5+
detectStrictMode = require('./detectStrictMode.js'),
6+
moduleEnv = require('./moduleEnv.js');
77

88
/**
99
* Does actual rewiring the module. For further documentation @see index.js
@@ -15,8 +15,8 @@ function internalRewire(parentModulePath, targetPath) {
1515
src;
1616

1717
// Checking params
18-
if (typeof targetPath !== "string") {
19-
throw new TypeError("Filename must be a string");
18+
if (typeof targetPath !== 'string') {
19+
throw new TypeError('Filename must be a string');
2020
}
2121

2222
// Resolve full filename relative to the parent module
@@ -38,19 +38,19 @@ function internalRewire(parentModulePath, targetPath) {
3838

3939
// Wrap module src inside IIFE so that function declarations do not clash with global variables
4040
// @see https://github.com/jhnns/rewire/issues/56
41-
prelude += "(function () { ";
41+
prelude += '(function () { ';
4242

4343
// We append our special setter and getter.
44-
appendix = "\n" + getDefinePropertySrc();
44+
appendix = '\n' + getDefinePropertySrc();
4545

4646
// End of IIFE
47-
appendix += "})();";
47+
appendix += '})();';
4848

4949
// Check if the module uses the strict mode.
5050
// If so we must ensure that "use strict"; stays at the beginning of the module.
51-
src = fs.readFileSync(targetPath, "utf8");
51+
src = fs.readFileSync(targetPath, 'utf8');
5252
if (detectStrictMode(src) === true) {
53-
prelude = ' "use strict"; ' + prelude;
53+
prelude = ` "use strict"; ${prelude}`;
5454
}
5555

5656
moduleEnv.inject(prelude, appendix);

0 commit comments

Comments
 (0)