-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.js
27 lines (26 loc) · 781 Bytes
/
patch.js
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
const Module = require('module');
const embed = require('./embed');
module.exports = request => {
const { _compile } = Module.prototype;
Module.prototype._compile = function(content, path) {
const patchedContent =
path === require.resolve(request)
? content.replace(
/\bswitch\s*\(node\.type\)\s*{\n\s*case\s*['"]TemplateLiteral['"]:/,
`
$&
try {
let result = (${embed})(...arguments);
if (result !== undefined) {
return result;
}
} catch (error) {
console.error(error.message);
}
`,
)
: content;
return _compile.call(this, patchedContent, path);
};
return require(request);
};