babel-plugin-transform-import-extension
Version:
Change the file extension in require calls when transpiling import statements
27 lines (25 loc) • 737 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = plugin;
var defaultOpts = {
mjs: "js"
};
function plugin() {
return {
visitor: {
ImportDeclaration: function ImportDeclaration(path, state) {
var source = path.node.source;
var value = source.value;
var opts = state.opts;
var origExts = Object.keys(opts);
var newExtFrom = origExts.length ? opts : defaultOpts;
origExts = Object.keys(newExtFrom);
source.value = origExts.reduce(function (newValue, origExt) {
return newValue.replace(new RegExp("^(\\..*\\.)".concat(origExt, "$")), "$1".concat(newExtFrom[origExt]));
}, value);
}
}
};
}