babel-plugin-transform-assets-import-to-string
Version:
Babel plugin that transforms image assets import and requires to urls / cdn
26 lines (21 loc) • 680 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function getVariableName(node) {
if (node.specifiers && node.specifiers[0] && node.specifiers[0].local) {
return node.specifiers[0].local.name;
}
}
function replaceNode(scope, uri) {
const content = scope.types.StringLiteral(uri);
if (scope.callee === 'require') {
scope.path.replaceWith(content);
return;
}
const variableName = getVariableName(scope.path.node);
if (variableName) {
scope.path.replaceWith(scope.types.variableDeclaration('const', [scope.types.variableDeclarator(scope.types.identifier(variableName), content)]));
}
}
exports.default = replaceNode;