UNPKG

antd-tools

Version:
59 lines (50 loc) 1.55 kB
const crypto = require('crypto'); const { createTransformer } = require('babel-jest'); const getBabelCommonConfig = require('../getBabelCommonConfig'); const rewriteSource = require('./rewriteSource'); const pkg = require('../../package.json'); const libDir = process.env.LIB_DIR || 'components'; function processDemo({ types: t }) { return { visitor: { ImportDeclaration(path) { rewriteSource(t, path, libDir); }, }, }; } module.exports = { canInstrument: true, process(src, path, config, transformOptions) { global.__clearBabelAntdPlugin && global.__clearBabelAntdPlugin(); // eslint-disable-line const babelConfig = getBabelCommonConfig(); babelConfig.plugins = [...babelConfig.plugins]; if (/\/demo\//.test(path)) { babelConfig.plugins.push(processDemo); } babelConfig.plugins.push([ require.resolve('babel-plugin-import'), { libraryName: 'antd-mobile', libraryDirectory: '../../../../components', }, ]); const babelSupport = path.endsWith('.ts') || path.endsWith('.tsx') || path.endsWith('.js') || path.endsWith('.jsx'); const babelJest = createTransformer(babelConfig); const fileName = babelSupport ? path : 'file.js'; return babelJest.process(src, fileName, config, transformOptions); }, getCacheKey() { return crypto .createHash('md5') .update('\0', 'utf8') .update(libDir) .update('\0', 'utf8') .update(pkg.version) .digest('hex'); }, };