Skip to content

Commit 77318c6

Browse files
committed
refactor: split builders into separate modules
1 parent 5008b33 commit 77318c6

16 files changed

Lines changed: 356 additions & 305 deletions

File tree

packages/scaffold/src/constant.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ export const CACHE_DIR = `${BASE_DIR}/cache`;
33
export const DEFAULT_PROFILE_DIR = "";
44
export const DEFAULT_DATA_DIR = "";
55

6+
// Builder
7+
export const DEFAULT_DIST_TEMP_DIR = "addon";
8+
69
// Testser
710
export const TESTER_BASE_PATH = `${BASE_DIR}/test`;
811
export const TESTER_PROFILE_DIR = `${TESTER_BASE_PATH}/profile`;

packages/scaffold/src/core/builder.ts

Lines changed: 0 additions & 236 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { copy } from "fs-extra/esm";
2+
import { glob } from "tinyglobby";
3+
import { logger } from "../../utils/logger.js";
4+
import { toArray } from "../../utils/string.js";
5+
6+
// We should ignore node_modules/ by default, glob this folder will be very slow
7+
const DEFAULT_IGNORE = ["node_modules", ".git"];
8+
9+
// Copys files in `Config.build.assets` to `Config.dist`
10+
export default async function copyAssets(
11+
source: string | string[],
12+
dist: string,
13+
assets: string | string[],
14+
) {
15+
const sourceArr = toArray(source);
16+
const paths = await glob(assets, {
17+
ignore: [...DEFAULT_IGNORE, dist],
18+
});
19+
20+
for (const file of paths) {
21+
const newPath = getNewPath(sourceArr, dist, file);
22+
await copy(file, newPath);
23+
logger.debug(`Copy ${file} to ${newPath}`);
24+
}
25+
}
26+
27+
export function getNewPath(sources: string[], dist: string, path: string): string {
28+
const pattern = new RegExp(sources.join("|"));
29+
const relativePath = path.replace(pattern, "");
30+
return `${dist}/addon/${relativePath}`;
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default async function clean() {
2+
//
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { BuildConfig } from "../../types/config.js";
2+
import { resolve } from "node:path";
3+
import { build as buildAsync } from "esbuild";
4+
import { logger } from "../../utils/logger.js";
5+
6+
export function resolveConfig(dist: string, esbuildOptions: BuildConfig["esbuildOptions"]) {
7+
const distAbsolute = resolve(dist);
8+
9+
// ensure outfile and outdir are in dist folder
10+
return esbuildOptions.map((option, i) => {
11+
if (option.outfile && !resolve(option.outfile).startsWith(distAbsolute)) {
12+
logger.debug(`'outfile' of esbuildOptions[${i}] is not in dist folder, it will be overwritten.`);
13+
option.outfile = `${dist}/${option.outfile}`;
14+
}
15+
if (option.outdir && !resolve(option.outdir).startsWith(distAbsolute)) {
16+
logger.debug(`'outdir' of esbuildOptions[${i}] is not in dist folder, it will be overwritten.`);
17+
option.outdir = `${dist}/${option.outdir}`;
18+
}
19+
return option;
20+
});
21+
}
22+
23+
export default async function esbuild(dist: string, esbuildOptions: BuildConfig["esbuildOptions"]) {
24+
if (esbuildOptions.length === 0)
25+
return;
26+
27+
const options = resolveConfig(dist, esbuildOptions);
28+
29+
return await Promise.all(
30+
options.map(esbuildOption =>
31+
buildAsync(esbuildOption),
32+
),
33+
);
34+
}

0 commit comments

Comments
 (0)