You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/test/setup.js

38 lines
1.2 KiB

8 years ago
const fs = require('fs');
require('source-map-support').install();
process.env.TEST = true;
8 years ago
require.extensions['.js'] = function(module, filename) {
const exports = [];
var code = fs.readFileSync(filename, 'utf-8')
.replace(/^import \* as (\w+) from ['"]([^'"]+)['"];?/gm, 'var $1 = require("$2");')
.replace(/^import (\w+) from ['"]([^'"]+)['"];?/gm, 'var {default: $1} = require("$2");')
8 years ago
.replace(/^import {([^}]+)} from ['"](.+)['"];?/gm, 'var {$1} = require("$2");')
.replace(/^export default /gm, 'exports.default = ')
8 years ago
.replace(/^export (const|let|var|class|function) (\w+)/gm, (match, type, name) => {
exports.push(name);
return `${type} ${name}`;
})
.replace(/^export \{([^}]+)\}(?: from ['"]([^'"]+)['"];?)?/gm, (match, names, source) => {
8 years ago
names.split(',').filter(Boolean).forEach(name => {
8 years ago
exports.push(name);
8 years ago
});
return source ? `const { ${names} } = require("${source}");` : '';
8 years ago
})
.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;
}
};