a63e2fab963c1c90e5e23fb127a420a964e6d372.svn-base
4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addHook = addHook;
var _module = require('module');
var _module2 = _interopRequireDefault(_module);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _nodeModulesRegexp = require('node-modules-regexp');
var _nodeModulesRegexp2 = _interopRequireDefault(_nodeModulesRegexp);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Guard against poorly mocked module constructors.
var Module = module.constructor.length > 1 ? module.constructor : _module2.default; /* (c) 2015 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */
var HOOK_RETURNED_NOTHING_ERROR_MESSAGE = '[Pirates] A hook returned a non-string, or nothing at all! This is a' + ' violation of intergalactic law!\n' + '--------------------\n' + 'If you have no idea what this means or what Pirates is, let me explain: ' + 'Pirates is a module that makes is easy to implement require hooks. One of' + ' the require hooks you\'re using uses it. One of these require hooks' + ' didn\'t return anything from it\'s handler, so we don\'t know what to' + ' do. You might want to debug this.';
function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
if (typeof filename !== 'string') return false;
if (exts.indexOf(_path2.default.extname(filename)) === -1) return false;
var resolvedFilename = _path2.default.resolve(filename);
if (ignoreNodeModules && _nodeModulesRegexp2.default.test(resolvedFilename)) return false;
if (matcher && typeof matcher === 'function') return !!matcher(resolvedFilename);
return true;
}
/**
*
* Add a require hook.
*
* @param {Function} hook - The hook. Accepts the code of the module and the filename. Required.
* @param {Object} [opts] - Options
* @param {String[]} [opts.exts=['.js']] - The extensions to hook. Should start with '.' (ex. ['.js']).
* @param {Function(path)} [opts.matcher] - A matcher function, will be called with path to a file. Should return truthy if the file should be hooked, falsy otherwise.
* @param {Boolean} [opts.ignoreNodeModules=true] - Auto-ignore node_modules. Independent of any matcher.
* @returns {Function} revert - Reverts the hooks.
*/
function addHook(hook) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
// eslint-disable-line import/prefer-default-export
var reverted = false;
var loaders = [];
var oldLoaders = [];
var exts = void 0;
// We need to do this to fix #15. Basically, if you use a non-standard extension (ie. .jsx), then
// We modify the .js loader, then use the modified .js loader for as the base for .jsx.
// This prevents that.
var originalJSLoader = Module._extensions['.js'];
var matcher = opts.matcher || null;
var ignoreNodeModules = opts.ignoreNodeModules !== false;
exts = opts.extensions || opts.exts || opts.extension || opts.ext || ['.js'];
if (!Array.isArray(exts)) exts = [exts];
exts.forEach(function (ext) {
if (typeof ext !== 'string') throw new TypeError(`Invalid Extension: ${ext}`);
var oldLoader = Module._extensions[ext] || originalJSLoader;
oldLoaders[ext] = oldLoader;
loaders[ext] = Module._extensions[ext] = function newLoader(mod, filename) {
var compile = void 0;
if (!reverted) {
if (shouldCompile(filename, exts, matcher, ignoreNodeModules)) {
compile = mod._compile;
mod._compile = function _compile(code) {
// reset the compile immediately as otherwise we end up having the
// compile function being changed even though this loader might be reverted
// Not reverting it here leads to long useless compile chains when doing
// addHook -> revert -> addHook -> revert -> ...
// The compile function is also anyway created new when the loader is called a second time.
mod._compile = compile;
var newCode = hook(code, filename);
if (typeof newCode !== 'string') {
throw new Error(HOOK_RETURNED_NOTHING_ERROR_MESSAGE);
}
return mod._compile(newCode, filename);
};
}
}
oldLoader(mod, filename);
};
});
return function revert() {
if (reverted) return;
reverted = true;
exts.forEach(function (ext) {
// if the current loader for the extension is our loader then unregister it and set the oldLoader again
// if not we can not do anything as we cannot remove a loader from within the loader-chain
if (Module._extensions[ext] === loaders[ext]) {
Module._extensions[ext] = oldLoaders[ext];
}
});
};
}