| | |
| | | |
| | | var _path = _interopRequireDefault(require("path")); |
| | | |
| | | var _nodeModulesRegexp = _interopRequireDefault(require("node-modules-regexp")); |
| | | |
| | | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| | | |
| | | /* (c) 2015 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */ |
| | | // Guard against poorly mocked module constructors. |
| | | const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/; // Guard against poorly mocked module constructors. |
| | | |
| | | const Module = module.constructor.length > 1 ? module.constructor : _module.default; |
| | | const 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.'; |
| | | /** |
| | | * @param {string} filename The filename to check. |
| | | * @param {string[]} exts The extensions to hook. Should start with '.' (ex. ['.js']). |
| | | * @param {Matcher|null} 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} ignoreNodeModules Auto-ignore node_modules. Independent of any matcher. |
| | | */ |
| | | |
| | | function shouldCompile(filename, exts, matcher, ignoreNodeModules) { |
| | | if (typeof filename !== 'string') { |
| | |
| | | |
| | | const resolvedFilename = _path.default.resolve(filename); |
| | | |
| | | if (ignoreNodeModules && _nodeModulesRegexp.default.test(resolvedFilename)) { |
| | | if (ignoreNodeModules && nodeModulesRegex.test(resolvedFilename)) { |
| | | return false; |
| | | } |
| | | |
| | |
| | | return true; |
| | | } |
| | | /** |
| | | * @callback Hook The hook. Accepts the code of the module and the filename. |
| | | * @param {string} code |
| | | * @param {string} filename |
| | | * @returns {string} |
| | | */ |
| | | |
| | | /** |
| | | * @callback Matcher A matcher function, will be called with path to a file. |
| | | * |
| | | * Should return truthy if the file should be hooked, falsy otherwise. |
| | | * @param {string} path |
| | | * @returns {boolean} |
| | | */ |
| | | |
| | | /** |
| | | * @callback RevertFunction Reverts the hook when called. |
| | | * @returns {void} |
| | | */ |
| | | |
| | | /** |
| | | * @typedef {object} Options |
| | | * @property {Matcher|null} [matcher=null] A matcher function, will be called with path to a file. |
| | | * |
| | | * Should return truthy if the file should be hooked, falsy otherwise. |
| | | * |
| | | * @property {string[]} [extensions=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']). |
| | | * @property {string[]} [exts=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']). |
| | | * |
| | | * @property {string[]} [extension=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']). |
| | | * @property {string[]} [ext=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']). |
| | | * |
| | | * @property {boolean} [ignoreNodeModules=true] Auto-ignore node_modules. Independent of any matcher. |
| | | */ |
| | | |
| | | /** |
| | | * 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. |
| | | * @param {Hook} hook The hook. Accepts the code of the module and the filename. Required. |
| | | * @param {Options} [opts] Options |
| | | * @returns {RevertFunction} The `revert` function. Reverts the hook when called. |
| | | */ |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | const oldLoader = Module._extensions[ext] || originalJSLoader; |
| | | oldLoaders[ext] = oldLoader; |
| | | oldLoaders[ext] = Module._extensions[ext]; |
| | | |
| | | loaders[ext] = Module._extensions[ext] = function newLoader(mod, filename) { |
| | | let compile; |
| | |
| | | // 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]; |
| | | if (!oldLoaders[ext]) { |
| | | delete Module._extensions[ext]; |
| | | } else { |
| | | Module._extensions[ext] = oldLoaders[ext]; |
| | | } |
| | | } |
| | | }); |
| | | }; |