diff --git a/packages/core/build/src/build.ts b/packages/core/build/src/build.ts index b8f625c95..46be1144b 100755 --- a/packages/core/build/src/build.ts +++ b/packages/core/build/src/build.ts @@ -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, ) { for await (const pkg of packages) { + writeToCache(BUILD_ERROR, { pkg: pkg.name }) await buildPackage(pkg, targetDir, doBuildPackage); } } diff --git a/packages/core/build/src/constant.ts b/packages/core/build/src/constant.ts index 9b113f84d..f92072aff 100644 --- a/packages/core/build/src/constant.ts +++ b/packages/core/build/src/constant.ts @@ -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(',')) diff --git a/packages/core/build/src/utils/getPackages.ts b/packages/core/build/src/utils/getPackages.ts index 5bcd45f44..75231dfa1 100644 --- a/packages/core/build/src/utils/getPackages.ts +++ b/packages/core/build/src/utils/getPackages.ts @@ -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[]) { diff --git a/packages/core/build/src/utils/utils.ts b/packages/core/build/src/utils/utils.ts index aa616dda9..2af074868 100644 --- a/packages/core/build/src/utils/utils.ts +++ b/packages/core/build/src/utils/utils.ts @@ -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) { + 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 {}; +} diff --git a/packages/core/cli/src/commands/build.js b/packages/core/cli/src/commands/build.js index d08bd5d7c..267abd837 100644 --- a/packages/core/cli/src/commands/build.js +++ b/packages/core/cli/src/commands/build.js @@ -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); });