@ant-design/tools
Version:
tools for ant design
100 lines (99 loc) • 3.83 kB
JavaScript
import { relative } from 'path';
import crypto from 'crypto';
import markTwain from 'mark-twain';
import * as JsonML from 'jsonml.js/lib/utils';
import * as babel from '@babel/core';
import getBabelCommonConfig from "../getBabelCommonConfig";
import rewriteSource from "./rewriteSource";
import pkg from "../../package.json";
const libDir = process.env.LIB_DIR || 'components';
function getCode(tree) {
let code = '';
const find = node => {
if (code) return;
if (!JsonML.isElement(node)) return;
if (JsonML.getTagName(node) !== 'pre') {
JsonML.getChildren(node).forEach(find);
return;
}
if ((JsonML.getAttributes(node) || {}).lang !== 'diff') {
code = JsonML.getChildren(JsonML.getChildren(node)[0] || '')[0] || '';
}
};
find(tree);
return code;
}
function createDemo({
types: t
}) {
return {
visitor: {
Program(path) {
// Only insert `import React from 'react'` when not exist
const existReact = (path.node.body || []).some(importNode => {
if (importNode.type !== 'ImportDeclaration' || importNode.source.value !== 'react') {
return false;
}
return (importNode.specifiers || []).some(specifierNode => ['ImportDefaultSpecifier', 'ImportNamespaceSpecifier'].includes(specifierNode.type) && specifierNode.local.name === 'React');
});
if (!existReact) {
const importReact = t.importDeclaration([t.importDefaultSpecifier(t.identifier('React'))], t.stringLiteral('react'));
path.unshiftContainer('body', importReact);
}
},
CallExpression(path) {
const callee = path.node.callee;
if (callee.object && callee.object.name === 'ReactDOM' && callee.property.name === 'render') {
const app = t.variableDeclaration('const', [t.variableDeclarator(t.identifier('__Demo'), path.node.arguments[0])]);
path.scope.registerDeclaration(path.replaceWith(app)[0]);
const exportDefault = t.exportDefaultDeclaration(t.identifier('__Demo'));
path.insertAfter(exportDefault);
path.insertAfter(app);
path.remove();
}
},
ImportDeclaration(path) {
const libPattern = new RegExp('antd(-mobile)?/lib/.+');
if (libPattern.test(path.node.source.value)) {
path.node.source.value = path.node.source.value.replace(/antd(-mobile)?\/lib/, '../../../components');
}
rewriteSource(t, path, libDir);
}
}
};
}
function transform(src, pathFilename) {
const markdown = markTwain(src);
src = getCode(markdown.content);
global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line
const babelConfig = getBabelCommonConfig();
babelConfig.plugins = [...babelConfig.plugins];
babelConfig.plugins.push(createDemo);
if (libDir !== 'dist') {
babelConfig.plugins.push([require.resolve('babel-plugin-import'), {
libraryName: 'antd',
libraryDirectory: `../../../../${libDir}`
}, 'antd-import']);
babelConfig.plugins.push([require.resolve('babel-plugin-import'), {
libraryName: 'antd-mobile',
libraryDirectory: `../../../../${libDir}`
}, 'antd-mobile-import']);
}
babelConfig.filename = pathFilename;
src = babel.transform(src, babelConfig).code;
return {
code: src
};
}
function getCacheKey(fileData, filename, options) {
const {
instrument,
config,
configString
} = options;
return crypto.createHash('md5').update(fileData).update('\0', 'utf8').update(relative(config.rootDir, filename)).update('\0', 'utf8').update(configString).update('\0', 'utf8').update(instrument ? 'instrument' : '').update('\0', 'utf8').update(libDir).update('\0', 'utf8').update(pkg.version).digest('hex');
}
export default {
process: transform,
getCacheKey
};