babel-plugin-on-demand-import
Version:
Babel plugin for importing components on demand
68 lines (59 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = onDemandImport;
function parseName(str) {
str = str[0].toLowerCase() + str.substr(1);
return str.replace(/([A-Z])/g, function ($1) {
return '-' + $1.toLowerCase();
});
}
function onDemandImportPlugin(path, t, opts) {
var libraryName = opts.libraryName,
_opts$libraryPath = opts.libraryPath,
libraryPath = _opts$libraryPath === undefined ? 'lib' : _opts$libraryPath,
_opts$stylePath = opts.stylePath,
stylePath = _opts$stylePath === undefined ? undefined : _opts$stylePath,
_opts$needImportStyle = opts.needImportStyle,
needImportStyle = _opts$needImportStyle === undefined ? false : _opts$needImportStyle;
if (!libraryName) {
console.error('libraryName should be provided in babel-plugin-import-on-demand');
return;
}
var node = path.node;
if (node && node.source.value === libraryName) {
node.specifiers.forEach(function (specifier) {
if (t.isImportSpecifier(specifier)) {
// Supports as operator(fix #1)
var importedName = specifier.imported.name;
var localName = specifier.local.name;
var finalName = importedName === localName ? importedName : localName;
path.insertBefore(t.importDeclaration([t.importDefaultSpecifier(t.identifier(finalName))], t.stringLiteral(libraryName + '/' + libraryPath + '/' + parseName(importedName))));
if (stylePath && needImportStyle) {
path.insertBefore(t.importDeclaration([], t.stringLiteral(libraryName + '/' + stylePath + '/' + parseName(importedName) + '.css')));
}
}
});
path.remove();
}
}
function onDemandImport(_ref) {
var t = _ref.types;
return {
name: 'on-demand-import',
visitor: {
ImportDeclaration: function ImportDeclaration(path, _ref2) {
var _ref2$opts = _ref2.opts,
opts = _ref2$opts === undefined ? {} : _ref2$opts;
if (Object.prototype.toString.call(opts) === '[object Array]') {
opts.forEach(function (opt) {
onDemandImportPlugin(path, t, opt);
});
} else {
onDemandImportPlugin(path, t, opts);
}
}
}
};
};