Browse Source

refactor: 📦 Generate version.js from package.json (#23957)

* 📦 generate version.js from package.json

* components/version/version.ts

* remove version.js

* fix scripts

* Add pretest and predist

* clean up finalizeCompile

* fix lint
pull/23963/head
偏右 5 years ago
committed by GitHub
parent
commit
d221458939
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      .antd-tools.config.js
  2. 1
      .gitignore
  3. 6
      components/version/index.tsx
  4. 11
      package.json
  5. 10
      scripts/generate-version.js

26
.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 = '';

1
.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

6
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;

11
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",

10
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',
);
Loading…
Cancel
Save