mirror of
https://github.com/developit/web-worker.git
synced 2026-01-25 14:10:20 +00:00
37 lines
935 B
JavaScript
37 lines
935 B
JavaScript
module.exports = function (babel) {
|
|
const t = babel.types;
|
|
const importTpl = babel.template('const SPECIFIERS = require(SOURCE)');
|
|
const exportTpl = babel.template('module.exports = EXPORT');
|
|
return {
|
|
name: 'transform-modules-commonjs-simplified',
|
|
visitor: {
|
|
ImportDeclaration(path) {
|
|
const specs = path.node.specifiers;
|
|
let specifiers;
|
|
if (specs.length === 1 && t.isImportDefaultSpecifier(specs[0])) {
|
|
specifiers = t.clone(specs[0].local);
|
|
}
|
|
else {
|
|
specifiers = babel.types.objectPattern(
|
|
specs.map(s => t.objectProperty(
|
|
s.local,
|
|
s.imported || t.identifier('default'),
|
|
false,
|
|
true
|
|
))
|
|
);
|
|
}
|
|
path.replaceWith(importTpl({
|
|
SPECIFIERS: specifiers,
|
|
SOURCE: t.clone(path.node.source)
|
|
}));
|
|
},
|
|
ExportDefaultDeclaration(path) {
|
|
path.replaceWith(exportTpl({
|
|
EXPORT: t.clone(path.node.declaration)
|
|
}));
|
|
}
|
|
}
|
|
};
|
|
};
|