Skip to content

Commit

Permalink
Use worker threads for building targets
Browse files Browse the repository at this point in the history
  • Loading branch information
paescuj committed Dec 26, 2024
1 parent 4b26faf commit c5b1c48
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
42 changes: 20 additions & 22 deletions bin/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { Listr } from 'listr2';
import { pascalCase, snakeCase } from 'scule';
import Tinypool from 'tinypool';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -38,14 +39,12 @@ const targets = {
},
};

const cliTargets = [];

const tasks = new Listr(
[
{
title: 'Fetching icons',
task: async (ctx) => {
ctx.icons = {};
ctx.tasks = { global: { rootDir, defaultVariant }, icons: {} };

const iconsVariantsDirs = Object.fromEntries(
iconsVariants.map((variant) => [
Expand Down Expand Up @@ -74,32 +73,18 @@ const tasks = new Listr(
};
});

ctx.icons[variant] = icons;
ctx.tasks.icons[variant] = icons;
}

ctx.global = { defaultVariant };
},
},
{
title: 'Building targets',
task: (_, task) =>
task: (ctx, task) =>
task.newListr(
Object.entries(targets).map(([targetName, targetConfig]) => ({
title: targetConfig.title,
enabled: () =>
cliTargets.length === 0 || cliTargets.includes(targetName),
task: async (ctx) => {
const { default: task } = await import(
`./targets/${targetConfig.target || targetName}/index.js`
);

targetConfig.path = path.join(
rootDir,
...targetConfig.path.split(path.posix.sep),
);

return task(ctx, targetConfig);
},
enabled: () => ctx.cliTargets.length === 0 || ctx.cliTargets.includes(targetName),
task: async (ctx) => ctx.pool.run({ targetName, config: ctx.tasks, targetConfig }),
})),
{ concurrent: true, exitOnError: false },
),
Expand All @@ -113,6 +98,8 @@ const tasks = new Listr(
},
);

const cliTargets = [];

// Get targets from command line arguments
// (build all targets if no arguments given)
for (const arg of process.argv.slice(2)) {
Expand All @@ -131,4 +118,15 @@ for (const arg of process.argv.slice(2)) {
}
}

await tasks.run();
const pool = new Tinypool({
filename: new URL('./worker.js', import.meta.url).href,
minThreads: 0,
resourceLimits: {
// Vue target (Vite/Rollup) takes up a lot of memory
maxOldGenerationSizeMb: 8192,
},
});

await tasks.run({ cliTargets, pool });

await pool.destroy();
6 changes: 0 additions & 6 deletions bin/prepublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import process from 'node:process';
import { updateYamlKey } from '@atomist/yaml-updater';
import semver from 'semver';

const PACKAGE_BASE = '';

const newVersion = semver.valid(semver.coerce(process.env.TAG_NAME));
console.info('New version is %s', newVersion);

Expand All @@ -31,10 +29,6 @@ function publishNpmPackage(name) {
const contents = JSON.parse(fs.readFileSync(packageJsonPath).toString());
contents.version = newVersion;

if (PACKAGE_BASE) {
contents.name = `${PACKAGE_BASE}/${name}`;
}

fs.writeFileSync(packageJsonPath, JSON.stringify(contents, undefined, 2));
console.info('package.json updated');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"prettier": "^3.4.2",
"scule": "^1.3.0",
"semver": "^7.6.3",
"tinypool": "1.0.2",
"typescript": "~5.7.2",
"vite": "^6.0.4",
"vite-plugin-dts": "^4.4.0"
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5b1c48

Please sign in to comment.