Skip to content

Commit

Permalink
chore: generate type files in real time during the dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wang1212 committed Oct 23, 2024
1 parent 2702685 commit dbd86fb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"dev": "vite",
"demo": "http-server -c-1 -o ./demo/",
"build:types": "pnpm --recursive --sequential --if-present run build:types",
"build": "pnpm --recursive --sequential --if-present run build",
"bundle-viz": "cross-env BUNDLE_VIS=1 PACKAGE=g npm run build",
"contributor": "git-contributor",
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "ESNext",

"rootDir": "${configDir}/src",
"outDir": "${configDir}/dist",
Expand All @@ -20,7 +20,7 @@
"importHelpers": true,

"declaration": true,
"declarationMap": false,
"declarationMap": true,
"declarationDir": "${configDir}/types",
// "emitDeclarationOnly": true,

Expand All @@ -31,5 +31,5 @@
"skipLibCheck": true
},
"include": ["${configDir}/src/**/*"],
"exclude": ["dist", "node_modules", "test/types"]
"exclude": []
}
11 changes: 8 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",

"rootDir": ".",

"jsx": "react",
"moduleResolution": "Node",
"esModuleInterop": true,
"jsx": "react",

"skipLibCheck": true,
"types": ["node"],

"paths": {
"@antv/g": ["./packages/g/src"],
"@antv/g-mobile-webgl": ["./packages/g-mobile-webgl/src"],
Expand Down Expand Up @@ -86,5 +90,6 @@
"@antv/g-plugin-image-loader": ["./packages/g-plugin-image-loader/src"]
}
},
"include": ["packages/*/src/**/*"]
"include": ["packages/*/src/**/*", "__tests__/**/*"],
"exclude": []
}
46 changes: 45 additions & 1 deletion vite.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import process from 'process';
import { fileURLToPath, URL } from 'url';
import { exec } from 'child_process';
import { defineConfig } from 'vite';
import glslify from 'rollup-plugin-glslify';
import commonjs from '@rollup/plugin-commonjs';
Expand Down Expand Up @@ -38,7 +39,8 @@ export default defineConfig({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.es6', '.es', '.mjs'],
}),
commonjs({ sourceMap: true }),
typescript({ sourceMap: true }),
typescript({ sourceMap: true, noCheck: true }),
buildTypesPlugin(),
],
resolve: {
alias: {
Expand Down Expand Up @@ -112,3 +114,45 @@ export default defineConfig({
},
},
});

function buildTypesPlugin() {
return {
name: 'build-types-plugin',
/**
* @see https://vite.dev/guide/api-plugin.html#handlehotupdate
*/
handleHotUpdate({ file }) {
const relativePath = path.relative(__dirname, file);
const packagePath = relativePath.startsWith('packages/')
? relativePath.split('/').slice(0, 2).join('/')
: null;

if (!packagePath) {
return;
}

exec(
`cd ${packagePath} && npm run build:types`,

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
absolute path
.
(error, stdout, stderr) => {
const date = new Date();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');

if (error) {
console.error(
`\x1b[90m${hours}:${minutes}:${seconds} [build:types]: ${error}`,
'\x1b[0m',
);
return;
}

console.log(
`\x1b[90m${hours}:${minutes}:${seconds} [build:types] success`,
'\x1b[0m',
);
},
);
},
};
}

0 comments on commit dbd86fb

Please sign in to comment.