babel-plugin-transform-import-paths
Version:
Babel plugin to transform import paths
40 lines (39 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("./helpers");
function default_1({ types: t }) {
const visitor = {
CallExpression(path, state) {
if (path.node.callee.name !== 'require')
return;
const args = path.node.arguments;
if (!args.length)
return;
const firstArg = helpers_1.getArg(t, args[0]);
if (firstArg) {
firstArg.value = helpers_1.getPath(firstArg.value, state.opts, state.file.opts.filename);
}
},
ImportDeclaration(path, state) {
path.node.source.value = helpers_1.getPath(path.node.source.value, state.opts, state.file.opts.filename);
},
ExportNamedDeclaration(path, state) {
if (path.node.source) {
path.node.source.value = helpers_1.getPath(path.node.source.value, state.opts, state.file.opts.filename);
}
},
ExportAllDeclaration(path, state) {
if (path.node.source) {
path.node.source.value = helpers_1.getPath(path.node.source.value, state.opts, state.file.opts.filename);
}
}
};
return {
visitor: {
Program(path, state) {
path.traverse(visitor, state);
}
},
};
}
exports.default = default_1;