From f3b28c496982b5a0d51d53a6ada73537d92703cd Mon Sep 17 00:00:00 2001 From: vagusX Date: Fri, 21 Feb 2020 09:15:41 +0800 Subject: [PATCH] feat: copy code from compatible pack to avoid load front end code in nodejs (#62) * feat: copy code from compatible pack to avoid load front end code in nodejs * feat: use lodash library * feat: update console --- package.json | 4 +- transforms/utils/compatible-icon-utils.js | 66 +++++++++++++++++++++++ transforms/utils/icon.js | 2 +- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 transforms/utils/compatible-icon-utils.js diff --git a/package.json b/package.json index b9b481b..d515857 100644 --- a/package.json +++ b/package.json @@ -37,16 +37,14 @@ "test": "jest" }, "dependencies": { - "@ant-design/compatible": "^0.0.1-rc.1", "@ant-design/icons": "^4.0.0-rc.0", - "@babel/runtime": "^7.8.3", "chalk": "^3.0.0", "execa": "^3.4.0", "globby": "^10.0.1", "is-git-clean": "^1.1.0", "jscodeshift": "^0.6.4", + "lodash": "^4.17.15", "papaparse": "^5.1.0", - "react": "^16.12.0", "update-check": "^1.5.3", "yargs-parser": "^16.1.0" }, diff --git a/transforms/utils/compatible-icon-utils.js b/transforms/utils/compatible-icon-utils.js new file mode 100644 index 0000000..a3e2782 --- /dev/null +++ b/transforms/utils/compatible-icon-utils.js @@ -0,0 +1,66 @@ +const { camelCase, upperFirst } = require('lodash'); + +// copied from https://github.com/ant-design/compatible/blob/master/src/icon/utils.ts +// "translate" these code to commonjs to avoid `babel` and `regenerator-runtime` + +const themeMap = { + filled: 'filled', + outlined: 'outlined', // default theme + twoTone: 'twoTone', +}; + +// moved from https://github.com/ant-design/ant-design/blob/master/components/icon/utils.ts +const fillTester = /-fill$/; +const outlineTester = /-o$/; +const twoToneTester = /-twotone$/; + +function withThemeSuffix(type, theme) { + const result = upperFirst(camelCase(type)); + const realTheme = upperFirst(themeMap[theme]); + + if (theme !== 'outlined' && !realTheme) { + console.warn(`This icon '${type}' has unknown theme '${theme}'`); + } + + return result + realTheme; +} + +function removeTypeTheme(type) { + return type + .replace(fillTester, '') + .replace(outlineTester, '') + .replace(twoToneTester, ''); +} + +// For alias or compatibility +function alias(type) { + let newType = type; + switch (type) { + case 'cross': + newType = 'close'; + break; + // https://github.com/ant-design/ant-design/issues/13007 + case 'interation': + newType = 'interaction'; + break; + // https://github.com/ant-design/ant-design/issues/16810 + case 'canlendar': + newType = 'calendar'; + break; + // https://github.com/ant-design/ant-design/issues/17448 + case 'colum-height': + newType = 'column-height'; + break; + default: + } + console.warn( + `Icon '${type}' was a typo and is now deprecated, please use '${newType}' instead.`, + ); + return newType; +} + +module.exports = { + withThemeSuffix, + removeTypeTheme, + alias, +}; diff --git a/transforms/utils/icon.js b/transforms/utils/icon.js index 3b51145..2b79e74 100644 --- a/transforms/utils/icon.js +++ b/transforms/utils/icon.js @@ -4,7 +4,7 @@ const { withThemeSuffix, removeTypeTheme, alias, -} = require('@ant-design/compatible/lib/icon/utils'); +} = require('./compatible-icon-utils'); const v4IconModulePath = path.dirname( require.resolve('@ant-design/icons/lib/icons'),