-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (31 loc) · 1.63 KB
/
index.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
28
29
30
31
32
33
34
35
36
37
38
39
function expandImport(all, $1, $2) {
return $1.split(',')
.map(function(part) {
part = part.trim();
return 'var ' + part + ' = require("' + $2 + '").' + part + ';'
})
.join('\n');
}
function expandImport2(all, $1, $2, $3) {
return 'const '+$1+' = require("'+$3+'");\n' + expandImport(all, $2, $3);
}
function starImportNotSupported(all, $1) {
return 'console.error(\'import * is not supported:\', all);'
}
module.exports = function fakeImports (src) {
src = src.replace(/import\s*"(.*?)"/g, 'require("$1");\n');
src = src.replace(/import\s*'(.*?)'/g, 'require("$1");\n');
src = src.replace(/import\s*([^{]*?)\s*from\s*'(.*?)'/g, 'const $1 = require("$2");\n');
src = src.replace(/import\s*([^{]*?)\s*from\s*"(.*?)"/g, 'const $1 = require("$2");\n');
src = src.replace(/import\s*\*\sfrom\s'(.*?)'/g, starImportNotSupported);
src = src.replace(/import\s*\*\sfrom\s"(.*?)"/g, starImportNotSupported);
src = src.replace(/import\s*{(.*?)}\s*from\s*"(.*?)"\s*/g, expandImport);
src = src.replace(/import\s*{(.*?)}\s*from\s*'(.*?)'\s*/g, expandImport);
src = src.replace(/import\s*([^{]*?)\s*,\s*{(.*?)}\s*from\s*"(.*?)"\s*/g, expandImport2);
src = src.replace(/import\s*([^{]*?)\s*,\s*{(.*?)}\s*from\s*'(.*?)'\s*/g, expandImport2);
src = src.replace(/export\s*default\s*([^ ]*)/g, 'module.exports = $1');
src = src.replace(/export\s*(var|let|const)\s*([a-zA-Z0-9_$]*)/g, '$1 $2 = module.exports.$2');
src = src.replace(/export\s*function\s*([a-zA-Z0-9_$]*)/g, 'var $1 = module.exports.$1 = function');
src = src.replace(/export\s*class\s*([a-zA-Z0-9_$]*)/g, 'var $1 = module.exports.$1 = class');
return src;
};