babel-plugin-zent
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -6,4 +6,5 @@ "use strict"; | ||
}); | ||
exports.default = foobar; | ||
const MODULE_NAME = 'zent'; // Errors: | ||
exports.default = babelPluginZent; | ||
// Errors: | ||
// import 'zent'; | ||
@@ -19,9 +20,9 @@ // import * as Zent from 'zent'; | ||
// require('zent-button') | ||
function foobar(babel) { | ||
function babelPluginZent(babel) { | ||
const t = babel.types; | ||
return { | ||
visitor: { | ||
CallExpression(path) { | ||
const node = path.node; // no require('zent') calls | ||
CallExpression(path, state) { | ||
const node = path.node; | ||
const libName = getLibraryName(state); // no require('zent') calls | ||
@@ -34,5 +35,5 @@ if (t.isIdentifier(node.callee, { | ||
if (t.isStringLiteral(source, { | ||
value: MODULE_NAME | ||
value: libName | ||
})) { | ||
throw path.buildCodeFrameError(`require('${MODULE_NAME}') is not allowed, use import { ... } from '${MODULE_NAME}'`); | ||
throw path.buildCodeFrameError(`require('${libName}') is not allowed, use import { ... } from '${libName}'`); | ||
} | ||
@@ -44,5 +45,6 @@ } | ||
const node = path.node; | ||
const libName = getLibraryName(state); | ||
if (t.isStringLiteral(node.source, { | ||
value: MODULE_NAME | ||
value: libName | ||
})) { | ||
@@ -53,3 +55,3 @@ const specifiers = node.specifiers; | ||
if (specifierCount === 0) { | ||
throw path.buildCodeFrameError(`Side-effect only import is not allowed in ${MODULE_NAME}.'`); | ||
throw path.buildCodeFrameError(`Side-effect only import is not allowed in ${libName}.'`); | ||
} | ||
@@ -60,3 +62,3 @@ | ||
if (t.isImportNamespaceSpecifier(sp)) { | ||
throw path.buildCodeFrameError(`Namespace import is not allowed in ${MODULE_NAME}, pick the components you need.`); | ||
throw path.buildCodeFrameError(`Namespace import is not allowed in ${libName}, pick the components you need.`); | ||
} // no import Zent from 'zent' | ||
@@ -66,7 +68,7 @@ | ||
if (t.isImportDefaultSpecifier(sp)) { | ||
throw path.buildCodeFrameError(`There is no default export in ${MODULE_NAME}.`); | ||
throw path.buildCodeFrameError(`There is no default export in ${libName}.`); | ||
} | ||
if (t.isImportSpecifier(sp)) { | ||
return r.concat(buildImportReplacement(sp, t, state, path)); | ||
r = r.concat(buildImportReplacement(sp, t, state, path)); | ||
} | ||
@@ -91,3 +93,3 @@ | ||
function buildImportReplacement(specifier, types, state, originalPath) { | ||
initModuleMappingAsNecessary(state); // import {Button as _Button} from 'zent' | ||
initModuleMappingAsNecessary(state, originalPath); // import {Button as _Button} from 'zent' | ||
// imported name is Button, but local name is _Button | ||
@@ -139,3 +141,3 @@ | ||
function initModuleMappingAsNecessary(state) { | ||
function initModuleMappingAsNecessary(state, path) { | ||
const options = state.opts; | ||
@@ -150,6 +152,13 @@ | ||
if (!data.MODULE_MAPPING) { | ||
const moduleMappingFile = options.moduleMappingFile || 'zent/dependency-graph.json'; // eslint-disable-next-line | ||
// options.moduleMappingFile is for internal use | ||
const moduleMappingFile = options.moduleMappingFile || getModuleMappingFile(getLibraryName(state)); | ||
data.MODULE_MAPPING = require(moduleMappingFile); // STYLE_IMPORT_MAPPING 是 css 和 style 公用的,因为两者只可能使用一种 | ||
try { | ||
// eslint-disable-next-line | ||
data.MODULE_MAPPING = require(moduleMappingFile); | ||
} catch (ex) { | ||
throw path.buildCodeFrameError(ex); | ||
} // STYLE_IMPORT_MAPPING 是 css 和 style 公用的,因为两者只可能使用一种 | ||
if (options.automaticStyleImport) { | ||
@@ -178,2 +187,11 @@ data.STYLE_IMPORT_MAPPING = {}; | ||
return `zent/${parentDir}/${component}${suffix}`; | ||
} | ||
function getLibraryName(state) { | ||
const options = state.opts; | ||
return options.libraryName || 'zent'; | ||
} | ||
function getModuleMappingFile(libName) { | ||
return `${libName}/dependency-graph.json`; | ||
} |
{ | ||
"name": "babel-plugin-zent", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -36,2 +36,3 @@ ## babel-plugin-zent | ||
- `libraryName`: Library to transform | ||
- `noModuleRewrite`: disable JavaScript module import rewrite,use with bundle tool's tree-shaking feature. | ||
@@ -44,2 +45,3 @@ - `automaticStyleImport`: `true` to enable styles imports for component. | ||
{ | ||
libraryName: 'zent', | ||
noModuleRewrite: false, | ||
@@ -46,0 +48,0 @@ automaticStyleImport: false, |
@@ -34,2 +34,3 @@ ## babel-plugin-zent | ||
- `libraryName`: 需要处理的模块名 | ||
- `noModuleRewrite`: 关闭 JavaScript 模块重写,一般配合打包工具的 tree-shaking 使用。 | ||
@@ -42,2 +43,3 @@ - `automaticStyleImport`: 设置为 `true` 启用样式自动引入。 | ||
{ | ||
libraryName: 'zent', | ||
noModuleRewrite: false, | ||
@@ -44,0 +46,0 @@ automaticStyleImport: false, |
7956
148
50