Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit76271ce

Browse files
committed
remove unneeded code from rewire for coderoad usage
1 parent673c285 commit76271ce

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&&typeofvarName==="string"){
14+
if(varName&&typeofvarName==='string'){
1515
returneval(varName);
1616
}else{
17-
thrownewTypeError("__get__ expects a non-empty string");
17+
thrownewTypeError('__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+
constimport=/\bimport\s+(?:.+\s+from\s+)?[\'"]([^"\']+)["\']/;
2+
3+
// detects imports
4+
// adds imports to global scope of rewired object
5+
functiondetectImports(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-
varmultiLineComment=/^\s*\/\*.*?\*\//;
2-
varsingleLineComment=/^\s*\/\/.*?[\r\n]/;
3-
varstrictMode=/^\s*(?:"usestrict"|'usestrict')[\t]*(?:[\r\n]|;)/;
1+
constmultiLineComment=/^\s*\/\*.*?\*\//;
2+
constsingleLineComment=/^\s*\/\/.*?[\r\n]/;
3+
conststrictMode=/^\s*(?:["']usestrict["'])[\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
varsingleLine;
1414
varmultiLine;
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
returnstrictMode.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+
varfs=require('fs');
2+
3+
functionfileExists(path){
4+
try{
5+
fs.accessSync(path,fs.F_OK);
6+
}
7+
catch(e){
8+
if(e){
9+
console.log(e);
10+
}
11+
returnfalse;
12+
}
13+
returntrue;
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-
varsrcs={
5+
constsrcs={
66
"__get__":__get__.toString(),
77
};
88

99
functiongetDefinePropertySrc(){
10-
varsrc="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(functionforEachSrc(preValue,value){
14-
returnpreValue+="Object.defineProperty(module.exports, '"+
15-
value+
16-
"', {enumerable: false, value: "+
17-
srcs[value]+
18-
", "+
19-
"writable: true}); ";
20-
},"");
21-
22-
src+="\n}";
23-
24-
returnsrc;
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+
returnpreValue+=`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
functiongetImportGlobalsSrc(ignore){
1111
varkey,
1212
value,
13-
src="",
14-
globalObj=typeofglobal==="undefined"?window:global;
13+
src='',
14+
globalObj=typeofglobal==='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(keyinglobalObj){/* 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-
varrewireModule=require("./rewire.js");
2-
varfs=require('fs');
3-
4-
functionfileExists(path,silent){
5-
if(silent===void0){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-
returnfalse;
15-
}
16-
}
17-
returntrue;
18-
}
1+
varrewireModule=require('./rewire.js');
2+
varfileExists=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-
//settoget undefined
15+
//create a __get__ mocktoprevent tests breaking
3216
if(!fileExists(filename)){
3317
return{
3418
__get__:()=>{}
@@ -41,4 +25,5 @@ function rewire(filename) {
4125

4226
module.exports=rewire;
4327

44-
deleterequire.cache[__filename];// deleting self from module cache so the parent module is always up to date
28+
deleterequire.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-
varModule=require("module"),
4-
fs=require("fs"),
5-
coffee;
3+
varModule=require('module'),
4+
fs=require('fs');
65

76
// caching original wrapper
87
varmoduleWrapper0=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
functionreset(){
2725
Module.wrapper[0]=moduleWrapper0;
2826
Module.wrapper[1]=moduleWrapper1;
29-
restoreExtensions();
3027
}
3128

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

49-
functionregisterExtensions(){
50-
varoriginalCoffeeExtension=require.extensions[".coffee"];
51-
52-
if(originalCoffeeExtension){
53-
originalExtensions.coffee=originalCoffeeExtension;
54-
}
55-
require.extensions[".coffee"]=coffeeExtension;
56-
}
57-
58-
functionrestoreExtensions(){
59-
if("coffee"inoriginalExtensions){
60-
require.extensions[".coffee"]=originalExtensions.coffee;
61-
}
62-
}
63-
64-
functioncoffeeExtension(module,filename){
65-
varcontent=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-
functionstripBOM(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-
returncontent;
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-
varModule=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+
varModule=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(typeoftargetPath!=="string"){
19-
thrownewTypeError("Filename must be a string");
18+
if(typeoftargetPath!=='string'){
19+
thrownewTypeError('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)

[8]ページ先頭

©2009-2025 Movatter.jp