2023-09-03 10:59:33 +08:00
|
|
|
import execa from 'execa';
|
2022-05-19 00:40:55 +08:00
|
|
|
import chalk from 'chalk';
|
2023-09-03 10:59:33 +08:00
|
|
|
import path from 'path';
|
|
|
|
import {
|
|
|
|
PACKAGES_PATH,
|
|
|
|
getPluginPackages,
|
|
|
|
CORE_CLIENT,
|
|
|
|
CORE_APP,
|
|
|
|
getCjsPackages,
|
|
|
|
getPresetsPackages,
|
|
|
|
ROOT_PATH,
|
|
|
|
} from './constant';
|
|
|
|
import { buildClient } from './buildClient';
|
|
|
|
import { buildCjs } from './buildCjs';
|
|
|
|
import { buildPlugin } from './buildPlugin';
|
|
|
|
import { buildDeclaration } from './buildDeclaration';
|
2023-09-15 08:51:20 +08:00
|
|
|
import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfig } from './utils';
|
2023-09-03 10:59:33 +08:00
|
|
|
import { getPackages } from './utils/getPackages';
|
|
|
|
import { Package } from '@lerna/package';
|
2023-09-12 22:39:23 +08:00
|
|
|
import { tarPlugin } from './tarPlugin'
|
2023-09-03 10:59:33 +08:00
|
|
|
|
|
|
|
export async function build(pkgs: string[]) {
|
2023-09-12 22:39:23 +08:00
|
|
|
process.env.NODE_ENV = 'production';
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
const packages = getPackages(pkgs);
|
2023-09-15 08:51:20 +08:00
|
|
|
if (packages.length === 0) {
|
|
|
|
console.error(chalk.red(`[@nocobase/build]: '${pkgs.join(', ')}' not match any packages.`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
const pluginPackages = getPluginPackages(packages);
|
|
|
|
const cjsPackages = getCjsPackages(packages);
|
|
|
|
const presetsPackages = getPresetsPackages(packages);
|
|
|
|
|
|
|
|
// core/*
|
|
|
|
await buildPackages(cjsPackages, 'lib', buildCjs);
|
|
|
|
const clientCore = packages.find((item) => item.location === CORE_CLIENT);
|
|
|
|
if (clientCore) {
|
|
|
|
await buildPackage(clientCore, 'es', buildClient);
|
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// plugins/*、samples/*
|
|
|
|
await buildPackages(pluginPackages, 'dist', buildPlugin);
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// presets/*
|
|
|
|
await buildPackages(presetsPackages, 'lib', buildCjs);
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// core/app
|
|
|
|
const appClient = packages.find((item) => item.location === CORE_APP);
|
|
|
|
if (appClient) {
|
|
|
|
await runScript(['umi', 'build'], ROOT_PATH, {
|
|
|
|
APP_ROOT: path.join(CORE_APP, 'client'),
|
|
|
|
});
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
export async function buildPackages(
|
|
|
|
packages: Package[],
|
|
|
|
targetDir: string,
|
2023-09-15 08:51:20 +08:00
|
|
|
doBuildPackage: (cwd: string, userConfig: UserConfig, sourcemap: boolean, log?: PkgLog) => Promise<any>,
|
2023-09-03 10:59:33 +08:00
|
|
|
) {
|
|
|
|
for await (const pkg of packages) {
|
|
|
|
await buildPackage(pkg, targetDir, doBuildPackage);
|
2023-08-02 00:07:52 +08:00
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
export async function buildPackage(
|
|
|
|
pkg: Package,
|
|
|
|
targetDir: string,
|
2023-09-15 08:51:20 +08:00
|
|
|
doBuildPackage: (cwd: string, userConfig: UserConfig, sourcemap: boolean, log?: PkgLog) => Promise<any>,
|
2023-09-03 10:59:33 +08:00
|
|
|
) {
|
|
|
|
const sourcemap = process.argv.includes('--sourcemap');
|
|
|
|
const noDeclaration = process.argv.includes('--no-dts');
|
2023-09-12 22:39:23 +08:00
|
|
|
const hasTar = process.argv.includes('--tar');
|
|
|
|
const onlyTar = process.argv.includes('--only-tar');
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
const log = getPkgLog(pkg.name);
|
|
|
|
const packageJson = getPackageJson(pkg.location);
|
2023-09-12 22:39:23 +08:00
|
|
|
|
|
|
|
if (onlyTar) {
|
|
|
|
await tarPlugin(pkg.location, log);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
log(`${chalk.bold(toUnixPath(pkg.location.replace(PACKAGES_PATH, '').slice(1)))} build start`);
|
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
const userConfig = getUserConfig(pkg.location);
|
2023-09-03 10:59:33 +08:00
|
|
|
// prebuild
|
|
|
|
if (packageJson?.scripts?.prebuild) {
|
|
|
|
log('prebuild');
|
|
|
|
await runScript(['prebuild'], pkg.location);
|
|
|
|
await packageJson.prebuild(pkg.location);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2023-09-15 08:51:20 +08:00
|
|
|
if (userConfig.beforeBuild) {
|
|
|
|
log('beforeBuild');
|
|
|
|
await userConfig.beforeBuild(log);
|
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// build source
|
2023-09-15 08:51:20 +08:00
|
|
|
await doBuildPackage(pkg.location, userConfig, sourcemap, log);
|
2023-08-02 00:07:52 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// build declaration
|
|
|
|
if (!noDeclaration) {
|
|
|
|
log('build declaration');
|
|
|
|
await buildDeclaration(pkg.location, targetDir);
|
2023-08-02 00:07:52 +08:00
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
// postbuild
|
|
|
|
if (packageJson?.scripts?.postbuild) {
|
|
|
|
log('postbuild');
|
|
|
|
await runScript(['postbuild'], pkg.location);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2023-09-12 22:39:23 +08:00
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
if (userConfig.afterBuild) {
|
|
|
|
log('afterBuild');
|
|
|
|
await userConfig.afterBuild(log);
|
|
|
|
}
|
|
|
|
|
2023-09-12 22:39:23 +08:00
|
|
|
// tar
|
|
|
|
if (hasTar) {
|
|
|
|
await tarPlugin(pkg.location, log);
|
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
function runScript(args: string[], cwd: string, envs: Record<string, string> = {}) {
|
|
|
|
return execa('yarn', args, {
|
2022-05-19 00:40:55 +08:00
|
|
|
cwd,
|
2023-09-03 10:59:33 +08:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
...envs,
|
|
|
|
NODE_ENV: 'production',
|
|
|
|
},
|
2022-05-19 00:40:55 +08:00
|
|
|
});
|
|
|
|
}
|