-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Idan Attias
committed
Nov 30, 2017
1 parent
ce30f51
commit 96e00cc
Showing
26 changed files
with
236 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "standard", | ||
"rules": { | ||
"node/no-deprecated-api": "off", | ||
"no-var": "error" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,84 @@ | ||
"use strict"; | ||
var Module = require('module'); | ||
var _ = require('lodash'); | ||
var path = require('path'); | ||
'use strict' | ||
const Module = require('module') | ||
const _ = require('lodash') | ||
const path = require('path') | ||
|
||
module.exports = function generateRequireForUserCode(scopedDirs, options) { | ||
options = _.defaults((options || {}), {autoDeleteCache: false}); | ||
module.exports = function generateRequireForUserCode (scopedDirs, options) { | ||
options = _.defaults((options || {}), {autoDeleteCache: false}) | ||
|
||
var forExtensions = Object.keys(require.extensions); | ||
var uniqueIdForThisScopedRequire = _.uniqueId("__dontExtendThisScopedRequire"); | ||
scopedDirs = _.map(scopedDirs, function(dir) {return path.resolve(dir)}); | ||
const forExtensions = Object.keys(require.extensions) | ||
const uniqueIdForThisScopedRequire = _.uniqueId('__dontExtendThisScopedRequire') | ||
scopedDirs = _.map(scopedDirs, function (dir) { return path.resolve(dir) }) | ||
|
||
var baseModule = require('./lib/stubmodule-that-does-the-require'); | ||
const baseModule = require('./lib/stubmodule-that-does-the-require') | ||
// so that it can be re-used again with another scoped-dir, I delete it from the cache | ||
delete Module._cache[baseModule.id]; | ||
delete Module._cache[baseModule.id] | ||
// make relative paths work when requiring | ||
baseModule.filename = path.resolve(scopedDirs[0], 'stubmodule-that-does-the-require.js'); | ||
baseModule.__scopedRequireModule = true; | ||
baseModule.filename = path.resolve(scopedDirs[0], 'stubmodule-that-does-the-require.js') | ||
baseModule.__scopedRequireModule = true | ||
|
||
function inUserCodeDirs(modulePath) { | ||
return _.some(scopedDirs, function(userCodeDir) {return modulePath.indexOf(userCodeDir) >= 0}); | ||
function inUserCodeDirs (modulePath) { | ||
return _.some(scopedDirs, function (userCodeDir) { return modulePath.indexOf(userCodeDir) >= 0 }) | ||
} | ||
|
||
function adjustPaths(m) { | ||
m.paths = _.filter(m.paths.concat(scopedDirs), function(modulePath) { return inUserCodeDirs(modulePath); }); | ||
function adjustPaths (m) { | ||
m.paths = _.filter(m.paths.concat(scopedDirs), function (modulePath) { return inUserCodeDirs(modulePath) }) | ||
} | ||
|
||
adjustPaths(baseModule); | ||
adjustPaths(baseModule) | ||
|
||
_.forEach(forExtensions, function(ext) { | ||
var original = require.extensions[ext]; | ||
if (original && original[uniqueIdForThisScopedRequire]) | ||
return; | ||
_.forEach(forExtensions, function (ext) { | ||
const original = require.extensions[ext] | ||
if (original && original[uniqueIdForThisScopedRequire]) { return } | ||
|
||
require.extensions[ext] = function requireThatAddsUserCodeDirs(m, filename) { | ||
if ((!m.parent && inUserCodeDirs(m.filename)) || | ||
m.parent && m.parent.__scopedRequireModule && inUserCodeDirs(m.filename)) { | ||
m.__scopedRequireModule = true; | ||
adjustPaths(m); | ||
require.extensions[ext] = function requireThatAddsUserCodeDirs (m, filename) { | ||
if (((!m.parent && inUserCodeDirs(m.filename)) || | ||
(m.parent && m.parent.__scopedRequireModule)) && inUserCodeDirs(m.filename)) { | ||
m.__scopedRequireModule = true | ||
adjustPaths(m) | ||
} | ||
|
||
return original(m, filename); | ||
}; | ||
Object.defineProperty(require.extensions[ext], uniqueIdForThisScopedRequire, {value: true}); | ||
}); | ||
return original(m, filename) | ||
} | ||
Object.defineProperty(require.extensions[ext], uniqueIdForThisScopedRequire, {value: true}) | ||
}) | ||
|
||
function deleteModuleFromCache(m) { | ||
delete Module._cache[m.id]; | ||
function deleteModuleFromCache (m) { | ||
delete Module._cache[m.id] | ||
_.forEach(m.children, function (subModule) { | ||
deleteModuleFromCache(subModule); | ||
}); | ||
m.children = []; | ||
deleteModuleFromCache(subModule) | ||
}) | ||
m.children = [] | ||
} | ||
|
||
return { | ||
require: !options.autoDeleteCache ? | ||
baseModule.require.bind(baseModule) : | ||
function(path) { | ||
var moduleExports = baseModule.require.apply(baseModule, arguments); | ||
require: !options.autoDeleteCache | ||
? baseModule.require.bind(baseModule) | ||
: function (path) { | ||
const moduleExports = baseModule.require.apply(baseModule, arguments) | ||
|
||
deleteModuleFromCache(baseModule); | ||
deleteModuleFromCache(baseModule) | ||
|
||
return moduleExports; | ||
return moduleExports | ||
}, | ||
scopedDirs: scopedDirs, | ||
clearCache: function () { | ||
deleteModuleFromCache(baseModule); | ||
deleteModuleFromCache(baseModule) | ||
}, | ||
loadCodeAsModule: function(code, filename) { | ||
if (filename && Module._cache[filename]) | ||
return Module._cache[filename]; | ||
|
||
var module = new Module(filename, baseModule); | ||
module.filename = filename; | ||
module.paths = baseModule.paths; | ||
module.__scopedRequireModule = true; | ||
loadCodeAsModule: function (code, filename) { | ||
if (filename && Module._cache[filename]) { return Module._cache[filename] } | ||
|
||
module._compile(code, module.filename || "filename-to-make-node6-happy"); | ||
const module = new Module(filename, baseModule) | ||
module.filename = filename | ||
module.paths = baseModule.paths | ||
module.__scopedRequireModule = true | ||
|
||
baseModule.children.push(module); | ||
if (options.autoDeleteCache) | ||
deleteModuleFromCache(baseModule); | ||
else if (filename && !options.autoDeleteCache) | ||
Module._cache[filename] = module; | ||
module._compile(code, module.filename || 'filename-to-make-node6-happy') | ||
|
||
baseModule.children.push(module) | ||
if (options.autoDeleteCache) { deleteModuleFromCache(baseModule) } else if (filename && !options.autoDeleteCache) { Module._cache[filename] = module } | ||
|
||
return module.exports; | ||
return module.exports | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = module; | ||
module.exports = module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
exports.scopedFunction2 = function() { | ||
return "scopedString2"; | ||
}; | ||
exports.scopedFunction2 = function () { | ||
return 'scopedString2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var subModule2 = require('afolder/asubmodule2'); | ||
const subModule2 = require('afolder/asubmodule2') | ||
|
||
module.exports = function subfunction() { | ||
return "substring and " + subModule2(); | ||
}; | ||
module.exports = function subfunction () { | ||
return 'substring and ' + subModule2() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = function subfunction() { | ||
return "substring2"; | ||
}; | ||
module.exports = function subfunction () { | ||
return 'substring2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require('./circular-ref-2'); | ||
require('./circular-ref-2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
require('./circular-ref-1'); | ||
global.moduleLoadSideEffect++; | ||
require('./circular-ref-1') | ||
global.moduleLoadSideEffect++ |
6 changes: 3 additions & 3 deletions
6
test/scoped-dir/module-importing-module-importing-from-scope2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = function() { | ||
return "1 and " + require('submodule-importing-from-scope2')(); | ||
}; | ||
module.exports = function () { | ||
return '1 and ' + require('submodule-importing-from-scope2')() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global.moduleLoadSideEffect++; | ||
global.moduleLoadSideEffect++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global.moduleLoadSideEffect3++; | ||
global.moduleLoadSideEffect3++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global.moduleLoadSideEffect4++; | ||
global.moduleLoadSideEffect4++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global.moduleLoadSideEffect++; | ||
global.moduleLoadSideEffect++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports.submodule = require('submodule-whose-load-side-effects'); | ||
module.exports.submodule = require('submodule-whose-load-side-effects') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports.submodule = require('afolder/circular-ref-1'); | ||
module.exports.submodule = require('afolder/circular-ref-1') |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var subModule = require('./afolder/asubmodule'); | ||
const subModule = require('./afolder/asubmodule') | ||
|
||
module.exports.aFunction = function() { | ||
return "astring and " + subModule(); | ||
}; | ||
module.exports.aFunction = function () { | ||
return 'astring and ' + subModule() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var subModule = require('afolder/asubmodule'); | ||
const subModule = require('afolder/asubmodule') | ||
|
||
module.exports.aFunction = function() { | ||
return "astring and " + subModule(); | ||
}; | ||
module.exports.aFunction = function () { | ||
return 'astring and ' + subModule() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var _ = require('lodash'); | ||
const _ = require('lodash') | ||
|
||
exports.scopedFunction = function() { | ||
return _.camelCase("Scoped Camel"); | ||
}; | ||
exports.scopedFunction = function () { | ||
return _.camelCase('Scoped Camel') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const scopedString = require('anNpmModule') | ||
|
||
exports.scopedFunction = function() { | ||
return scopedString; | ||
exports.scopedFunction = function () { | ||
return scopedString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = function() { | ||
return "2 and " + require('scoped-module-2').scopedFunction2(); | ||
}; | ||
module.exports = function () { | ||
return '2 and ' + require('scoped-module-2').scopedFunction2() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
global.moduleLoadSideEffect++; | ||
global.moduleLoadSideEffect++ |
Oops, something went wrong.