babel-plugin-zent
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -74,9 +74,34 @@ "use strict"; | ||
if (options.noModuleRewrite) { | ||
path.insertAfter(replacement); | ||
state.data.ops.insert.push({ | ||
path, | ||
replacement | ||
}); // path.insertAfter(replacement); | ||
} else { | ||
path.replaceWithMultiple(replacement); | ||
// path.replaceWithMultiple(replacement); | ||
state.data.ops.replace.push({ | ||
path, | ||
replacement | ||
}); | ||
} | ||
} | ||
}, | ||
Program: { | ||
exit(path, state) { | ||
const ops = state.data && state.data.ops; | ||
if (!ops) { | ||
return; | ||
} | ||
ops.insert.forEach(op => { | ||
op.path.insertAfter(op.replacement); | ||
}); | ||
ops.replace.forEach(op => { | ||
const rep = consolidateImports(t, op.replacement); | ||
op.path.replaceWithMultiple(rep); | ||
}); | ||
} | ||
} | ||
} | ||
@@ -87,3 +112,3 @@ }; | ||
function buildImportReplacement(specifier, types, state, originalPath) { | ||
initModuleMappingAsNecessary(state, originalPath); // import {Button as _Button} from 'zent' | ||
initModuleStateAsNecessary(state, originalPath); // import {Button as _Button} from 'zent' | ||
// imported name is Button, but local name is _Button | ||
@@ -122,3 +147,3 @@ | ||
} else { | ||
throw originalPath.buildCodeFrameError(`No export named '${importedName}' found in zent.`); | ||
replacement.push(types.importDeclaration(buildImportSpecifier(types, false, importedName, localName), types.stringLiteral(libName))); | ||
} | ||
@@ -137,3 +162,3 @@ | ||
function initModuleMappingAsNecessary(state, path) { | ||
function initModuleStateAsNecessary(state, path) { | ||
const options = state.opts; | ||
@@ -145,4 +170,10 @@ | ||
const data = state.data; | ||
const data = state.data; // Store all the paths that needs transform | ||
// We only flush these operations at Program.exit | ||
data.ops = { | ||
insert: [], | ||
replace: [] | ||
}; | ||
if (!data.MODULE_MAPPING) { | ||
@@ -166,2 +197,36 @@ // options.moduleMappingFile is for internal use | ||
function consolidateImports(types, nodes) { | ||
const rv = nodes.reduce((state, n) => { | ||
const mod = n.source.value; | ||
const consolidated = state.consolidated, | ||
moduleMap = state.moduleMap; | ||
const specifiers = n.specifiers; // side-effect import, e.g. import from 'zent/css/button.css'; | ||
if (specifiers.length === 0) { | ||
consolidated.push(n); | ||
} | ||
specifiers.forEach(sp => { | ||
// Consolidate import { x } from 'zent'; import { y } from 'zent'; only | ||
if (types.isImportSpecifier(sp)) { | ||
if (!moduleMap[mod]) { | ||
n.specifiers = []; | ||
moduleMap[mod] = n; | ||
consolidated.push(n); | ||
} | ||
const importNode = moduleMap[mod]; | ||
importNode.specifiers.push(sp); | ||
} else { | ||
consolidated.push(n); | ||
} | ||
}); | ||
return state; | ||
}, { | ||
moduleMap: {}, | ||
consolidated: [] | ||
}); | ||
return rv.consolidated; | ||
} | ||
function getJavaScriptPath(relativePath, libName) { | ||
@@ -168,0 +233,0 @@ const parentDir = 'es'; |
{ | ||
"name": "babel-plugin-zent", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
9769
206