2017-07-30 05:35:12 +02:00
|
|
|
const fs = require('fs');
|
2017-07-30 07:04:23 +02:00
|
|
|
|
|
|
|
require('source-map-support').install();
|
2017-03-07 20:10:28 +01:00
|
|
|
|
2017-11-23 14:45:22 +01:00
|
|
|
process.env.TEST = true;
|
|
|
|
|
2017-07-30 09:20:15 +02:00
|
|
|
require.extensions['.js'] = function(module, filename) {
|
|
|
|
const exports = [];
|
|
|
|
|
|
|
|
var code = fs.readFileSync(filename, 'utf-8')
|
2018-12-16 01:18:03 +01:00
|
|
|
.replace(/^import \* as (\w+) from ['"]([^'"]+)['"];?/gm, 'var $1 = require("$2");')
|
|
|
|
.replace(/^import (\w+) from ['"]([^'"]+)['"];?/gm, 'var {default: $1} = require("$2");')
|
2017-07-30 09:20:15 +02:00
|
|
|
.replace(/^import {([^}]+)} from ['"](.+)['"];?/gm, 'var {$1} = require("$2");')
|
2018-12-16 01:18:03 +01:00
|
|
|
.replace(/^export default /gm, 'exports.default = ')
|
2017-07-30 09:20:15 +02:00
|
|
|
.replace(/^export (const|let|var|class|function) (\w+)/gm, (match, type, name) => {
|
|
|
|
exports.push(name);
|
|
|
|
return `${type} ${name}`;
|
|
|
|
})
|
2018-12-16 01:18:03 +01:00
|
|
|
.replace(/^export \{([^}]+)\}(?: from ['"]([^'"]+)['"];?)?/gm, (match, names, source) => {
|
2017-07-30 09:20:15 +02:00
|
|
|
names.split(',').filter(Boolean).forEach(name => {
|
2017-07-30 05:35:12 +02:00
|
|
|
exports.push(name);
|
2017-07-30 09:20:15 +02:00
|
|
|
});
|
|
|
|
|
2018-12-16 01:18:03 +01:00
|
|
|
return source ? `const { ${names} } = require("${source}");` : '';
|
2017-07-30 09:20:15 +02:00
|
|
|
})
|
|
|
|
.replace(/^export function (\w+)/gm, 'exports.$1 = function $1');
|
|
|
|
|
|
|
|
exports.forEach(name => {
|
|
|
|
code += `\nexports.${name} = ${name};`;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
return module._compile(code, filename);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(code); // eslint-disable-line no-console
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|