UNPKG

@ant-design/tools

Version:
109 lines (107 loc) 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _path = require("path"); var _crypto = _interopRequireDefault(require("crypto")); var _markTwain = _interopRequireDefault(require("mark-twain")); var JsonML = _interopRequireWildcard(require("jsonml.js/lib/utils")); var babel = _interopRequireWildcard(require("@babel/core")); var _getBabelCommonConfig = _interopRequireDefault(require("../getBabelCommonConfig")); var _rewriteSource = _interopRequireDefault(require("./rewriteSource")); var _package = _interopRequireDefault(require("../../package.json")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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'); } (0, _rewriteSource.default)(t, path, libDir); } } }; } function transform(src, pathFilename) { const markdown = (0, _markTwain.default)(src); src = getCode(markdown.content); global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line const babelConfig = (0, _getBabelCommonConfig.default)(); 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.default.createHash('md5').update(fileData).update('\0', 'utf8').update((0, _path.relative)(config.rootDir, filename)).update('\0', 'utf8').update(configString).update('\0', 'utf8').update(instrument ? 'instrument' : '').update('\0', 'utf8').update(libDir).update('\0', 'utf8').update(_package.default.version).digest('hex'); } var _default = exports.default = { process: transform, getCacheKey };