Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/*jshint node: true */
2
var inserted,
3
Module = require('module'),
4
fs = require('fs'),
5
existingExtFn = Module._extensions['.js'],
6
amdefineRegExp = /amdefine\.js/;
7
8
inserted = "if (typeof define !== 'function') {var define = require('amdefine')(module)}";
9
10
//From the node/lib/module.js source:
11
function stripBOM(content) {
12
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
13
// because the buffer-to-string conversion in `fs.readFileSync()`
14
// translates it to FEFF, the UTF-16 BOM.
15
if (content.charCodeAt(0) === 0xFEFF) {
16
content = content.slice(1);
17
}
18
return content;
19
}
20
21
//Also adapted from the node/lib/module.js source:
22
function intercept(module, filename) {
23
var content = stripBOM(fs.readFileSync(filename, 'utf8'));
24
25
if (!amdefineRegExp.test(module.id)) {
26
content = inserted + content;
27
}
28
29
module._compile(content, filename);
30
}
31
32
intercept._id = 'amdefine/intercept';
33
34
if (!existingExtFn._id || existingExtFn._id !== intercept._id) {
35
Module._extensions['.js'] = intercept;
36
}
37
38