From d2214589391c8fc8646c3e8ef2c6aa86fcdd02a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=8F=E5=8F=B3?= Date: Fri, 8 May 2020 17:49:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=93=A6=20Generate=20version.j?= =?UTF-8?q?s=20from=20package.json=20(#23957)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :package: generate version.js from package.json * components/version/version.ts * remove version.js * fix scripts * Add pretest and predist * clean up finalizeCompile * fix lint --- .antd-tools.config.js | 26 -------------------------- .gitignore | 1 + components/version/index.tsx | 6 ++++-- package.json | 11 ++++++++--- scripts/generate-version.js | 10 ++++++++++ 5 files changed, 23 insertions(+), 31 deletions(-) create mode 100644 scripts/generate-version.js diff --git a/.antd-tools.config.js b/.antd-tools.config.js index 1a220298e3..97d5728c32 100644 --- a/.antd-tools.config.js +++ b/.antd-tools.config.js @@ -1,7 +1,5 @@ const fs = require('fs'); const path = require('path'); -// eslint-disable-next-line import/no-extraneous-dependencies -const packageInfo = require('./package.json'); const defaultVars = require('./scripts/default-vars'); const darkVars = require('./scripts/dark-vars'); const compactVars = require('./scripts/compact-vars'); @@ -17,30 +15,6 @@ module.exports = { // We need compile additional content for antd user function finalizeCompile() { if (fs.existsSync(path.join(__dirname, './lib'))) { - // Build package.json version to lib/version/index.js - // prevent json-loader needing in user-side - const versionFilePath = path.join(process.cwd(), 'lib', 'version', 'index.js'); - const versionFileContent = fs.readFileSync(versionFilePath).toString(); - fs.writeFileSync( - versionFilePath, - versionFileContent.replace( - /require\(('|")\.\.\/\.\.\/package\.json('|")\)/, - `{ version: '${packageInfo.version}' }`, - ), - ); - // eslint-disable-next-line no-console - console.log('Wrote version into lib/version/index.js'); - - // Build package.json version to lib/version/index.d.ts - // prevent https://github.com/ant-design/ant-design/issues/4935 - const versionDefPath = path.join(process.cwd(), 'lib', 'version', 'index.d.ts'); - fs.writeFileSync( - versionDefPath, - `declare var _default: "${packageInfo.version}";\nexport default _default;\n`, - ); - // eslint-disable-next-line no-console - console.log('Wrote version into lib/version/index.d.ts'); - // Build a entry less file to dist/antd.less const componentsPath = path.join(process.cwd(), 'components'); let componentsLessContent = ''; diff --git a/.gitignore b/.gitignore index 6d18fbcaf7..bb8d5b7e86 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ site/theme/template/Resources/*.jsx site/theme/template/Resources/**/*.jsx site/theme/template/NotFound.jsx scripts/previewEditor/index.html +components/version/version.tsx diff --git a/components/version/index.tsx b/components/version/index.tsx index f1cf686bb1..36bbb7bee3 100644 --- a/components/version/index.tsx +++ b/components/version/index.tsx @@ -1,3 +1,5 @@ -import packageInfo from '../../package.json'; +/* eslint import/no-unresolved: 0 */ +// @ts-ignore +import version from './version'; -export default packageInfo.version; +export default version; diff --git a/package.json b/package.json index 6b60c50a09..e6b958a25e 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,9 @@ "build": "npm run compile && npm run dist", "bundlesize": "bundlesize", "check-commit": "node ./scripts/check-commit", + "precompile": "npm run version", + "pretest": "npm run version", + "predist": "npm run version", "compile": "antd-tools run compile", "changelog": "node ./scripts/print-changelog", "predeploy": "antd-tools run clean && npm run site && cp netlify.toml CNAME _site && cp -r .circleci/ .github/ _site && npm run site:test", @@ -71,7 +74,8 @@ "prettier": "prettier -c --write '**/*'", "pretty-quick": "pretty-quick", "pub": "antd-tools run pub", - "prepublish": "antd-tools run guard", + "postinstall": "npm run version", + "prepublishOnly": "antd-tools run guard", "site": "cross-env NODE_ICU_DATA=node_modules/full-icu concurrently \"bisheng build --ssr -c ./site/bisheng.config.js\" \"node ./scripts/generateColorLess.js\"", "sort": "npx sort-package-json", "sort-api": "antd-tools run sort-api-table", @@ -82,7 +86,8 @@ "test-all": "./scripts/test-all.sh", "test-node": "jest --config .jest.node.js --no-cache", "tsc": "tsc", - "site:test": "jest --config .jest.site.js --cache=false" + "site:test": "jest --config .jest.site.js --cache=false", + "version": "node ./scripts/generate-version" }, "husky": { "hooks": { @@ -273,7 +278,7 @@ "bundlesize": [ { "path": "./dist/antd.min.js", - "maxSize": "305 kB" + "maxSize": "300 kB" }, { "path": "./dist/antd.min.css", diff --git a/scripts/generate-version.js b/scripts/generate-version.js new file mode 100644 index 0000000000..0370a17df8 --- /dev/null +++ b/scripts/generate-version.js @@ -0,0 +1,10 @@ +const fs = require('fs-extra'); +const path = require('path'); + +const { version } = require('../package.json'); + +fs.writeFileSync( + path.join(__dirname, '..', 'components', 'version', 'version.tsx'), + `export default '${version}'`, + 'utf8', +);