Optimize building tools (#3824)

* fix: build support dir

* fix: add build retry

* fix: vite warning ignore
This commit is contained in:
jack zhang 2024-03-27 07:45:29 +08:00 committed by GitHub
parent 85b4afbe7c
commit e32666b191
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 5 deletions

View File

@ -14,16 +14,22 @@ import { buildClient } from './buildClient';
import { buildCjs } from './buildCjs';
import { buildPlugin } from './buildPlugin';
import { buildDeclaration } from './buildDeclaration';
import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfig } from './utils';
import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfig, writeToCache, readFromCache } from './utils';
import { getPackages } from './utils/getPackages';
import { Package } from '@lerna/package';
import { tarPlugin } from './tarPlugin'
const BUILD_ERROR = 'build-error';
export async function build(pkgs: string[]) {
const isDev = process.argv.includes('--development');
process.env.NODE_ENV = isDev ? 'development' : 'production';
const packages = getPackages(pkgs);
let packages = getPackages(pkgs);
const cachePkg = readFromCache(BUILD_ERROR);
if (process.argv.includes('--retry') && cachePkg?.pkg) {
packages = packages.slice(packages.findIndex((item) => item.name === cachePkg.pkg));
}
if (packages.length === 0) {
let msg = '';
if (pkgs.length) {
@ -59,6 +65,7 @@ export async function build(pkgs: string[]) {
APP_ROOT: path.join(CORE_APP, 'client'),
});
}
writeToCache(BUILD_ERROR, {});
}
export async function buildPackages(
@ -67,6 +74,7 @@ export async function buildPackages(
doBuildPackage: (cwd: string, userConfig: UserConfig, sourcemap: boolean, log?: PkgLog) => Promise<any>,
) {
for await (const pkg of packages) {
writeToCache(BUILD_ERROR, { pkg: pkg.name })
await buildPackage(pkg, targetDir, doBuildPackage);
}
}

View File

@ -26,6 +26,7 @@ export const EsbuildSupportExts = [
'.data',
];
export const ROOT_PATH = path.join(__dirname, '../../../../');
export const NODE_MODULES = path.join(ROOT_PATH, 'node_modules');
export const PACKAGES_PATH = path.join(ROOT_PATH, 'packages');
export const PLUGINS_DIR = ['plugins', 'samples', 'pro-plugins']
.concat((process.env.PLUGINS_DIRS || '').split(','))

View File

@ -10,6 +10,7 @@ import { toUnixPath } from './utils';
* npm
* @example
* yarn build packages/core/client @nocobase/acl => ['/home/xx/packages/core/client', '/home/xx/packages/core/acl']
* yarn build packages/plugins/* => ['/home/xx/packages/plugins/a', '/home/xx/packages/plugins/b']
* yarn build => all packages
*/
function getPackagesPath(pkgs: string[]) {
@ -24,7 +25,6 @@ function getPackagesPath(pkgs: string[]) {
return allPackageJson
.map(toUnixPath).map(item => path.dirname(item));
}
const allPackageInfo = allPackageJson
.map(packageJsonPath => ({ name: require(packageJsonPath).name, path: path.dirname(toUnixPath(packageJsonPath)) }))
.reduce((acc, cur) => {
@ -37,7 +37,9 @@ function getPackagesPath(pkgs: string[]) {
const relativePaths = pkgNames.length ? pkgs.filter(item => !pkgNames.includes(item)) : pkgs;
const pkgPaths = pkgs.map(item => allPackageInfo[item])
const absPaths = allPackagePaths.filter(absPath => relativePaths.some((relativePath) => absPath.endsWith(relativePath)));
return [...pkgPaths, ...absPaths];
const dirPaths = fg.sync(pkgs, { onlyDirectories: true, absolute: true, cwd: ROOT_PATH });
const dirMatchPaths = allPackagePaths.filter(pkgPath => dirPaths.some(dirPath => pkgPath.startsWith(dirPath)));
return [...new Set([...pkgPaths, ...absPaths, ...dirMatchPaths])];
}
export function getPackages(pkgs: string[]) {

View File

@ -1,10 +1,11 @@
import chalk from 'chalk';
import path from 'path';
import fs from 'fs-extra';
import fg from 'fast-glob';
import fs from 'fs-extra';
import { Options as TsupConfig } from 'tsup'
import { InlineConfig as ViteConfig } from 'vite'
import { register } from 'esbuild-register/dist/node';
import { NODE_MODULES } from '../constant';
let previousColor = '';
function randomColor() {
@ -79,3 +80,18 @@ export function getUserConfig(cwd: string) {
}
return config;
}
const CACHE_DIR = path.join(NODE_MODULES, '.cache', 'nocobase');
export function writeToCache(key: string, data: Record<string, any>) {
const cachePath = path.join(CACHE_DIR, `${key}.json`);
fs.ensureDirSync(path.dirname(cachePath));
fs.writeJsonSync(cachePath, data, { spaces: 2 });
}
export function readFromCache(key: string) {
const cachePath = path.join(CACHE_DIR, `${key}.json`);
if (fs.existsSync(cachePath)) {
return fs.readJsonSync(cachePath);
}
return {};
}

View File

@ -13,6 +13,7 @@ module.exports = (cli) => {
.argument('[packages...]')
.option('-v, --version', 'print version')
.option('-c, --compile', 'compile the @nocobase/build package')
.option('-r, --retry', 'retry the last failed package')
.option('-w, --watch', 'watch compile the @nocobase/build package')
.option('-s, --sourcemap', 'generate sourcemap')
.option('--no-dts', 'not generate dts')
@ -24,12 +25,14 @@ module.exports = (cli) => {
});
if (options.watch) return;
}
process.env['VITE_CJS_IGNORE_WARNING'] = 'true';
await run('nocobase-build', [
...pkgs,
options.version ? '--version' : '',
!options.dts ? '--no-dts' : '',
options.sourcemap ? '--sourcemap' : '',
options.retry ? '--retry' : '',
]);
buildIndexHtml(true);
});