Skip to content

Commit

Permalink
fix(types): fix the types of the entries and plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 9, 2020
1 parent 82b95e2 commit 2fb8054
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@
"site:build": "npm run site:clean && BABEL_ENV=site gatsby build --prefix-paths",
"site:clean": "BABEL_ENV=site gatsby clean",
"site:deploy": "npm run site:build && gh-pages -d public",
"build": "npm run build-lib && npm run build-es && npm run build-umd",
"build": "npm run build-lib && npm run build-es && npm run build-umd && npm run build-types",
"build-lib": "rm -rf lib && babel src --out-dir lib",
"build-es": "rm -rf es && BABEL_ENV=esm babel src --out-dir es",
"build-umd": "rm -rf dist && rollup --config",
"build-types": "node ./scripts/patch-types.js",
"ci": "npm run lint && npm run test && npm run test-types",
"compress": "sh ./bin/compress.sh",
"coverage": "jest --coverage",
Expand Down
56 changes: 56 additions & 0 deletions scripts/patch-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const fs = require('fs');
const { join } = require('path');
const pkg = require('../package.json');

/**
* @param {String} rootDir 根目录
*/
async function main(rootDir) {
// 给可能被使用的入口文件添加类型
const entryFiles = [
'dist/f2-all.js',
'dist/f2-all.min.js',
'dist/f2-simple.js',
'dist/f2-simple.min.js',
'dist/f2.js',
'dist/f2.min.js',
'es/index-all.js',
'es/index-simple.js',
'es/index.js',
'lib/index-all.js',
'lib/index-simple.js',
'lib/index.js'
].map(path => join(rootDir, path));
for (const entryFile of entryFiles) {
if (fs.existsSync(entryFile)) {
const entryTypesFile = entryFile.replace(/\.js$/, '.d.ts');
fs.writeFileSync(entryTypesFile, [
`import F2 from '${pkg.name}';`,
'export = F2;'
].join('\n'));
}
}

// 给插件添加类型
const pluginDirs = [
'es/plugin',
'lib/plugin'
].map(path => join(rootDir, path));
for (const pluginDir of pluginDirs) {
if (fs.existsSync(pluginDir)) {
const pluginFiles = fs.readdirSync(pluginDir)
.filter(file => file.endsWith('.js'))
.map(file => join(pluginDir, file));
for (const pluginFile of pluginFiles) {
const pluginTypesFile = pluginFile.replace(/\.js$/, '.d.ts');
fs.writeFileSync(pluginTypesFile, [
`import F2 from '${pkg.name}';`,
'declare const plugin: F2.Plugin;',
'export default plugin;'
].join('\n'));
}
}
}
}

main(join(__dirname, '..'));
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Point } from './Point';
import {
Data,
DataRecord,
DataValue,
DataField,
DataRecordScale,
DataFieldScale,
Expand Down Expand Up @@ -120,6 +121,7 @@ declare namespace F2 {
Point,
Data,
DataRecord,
DataValue,
DataField,
DataRecordScale,
DataFieldScale,
Expand Down Expand Up @@ -203,7 +205,7 @@ declare namespace F2 {
ChartInnerProps,
ChartPlugins,
Chart,
}
};
}

export {
Expand Down

0 comments on commit 2fb8054

Please sign in to comment.