From c5b1c480a8f1ae6522f4c9d500c69aae3721c800 Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Thu, 26 Dec 2024 17:02:18 +0100 Subject: [PATCH] Use worker threads for building targets --- bin/build/index.js | 42 ++++++++++++++++++++---------------------- bin/prepublish.js | 6 ------ package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/bin/build/index.js b/bin/build/index.js index 224f796b..d9aa98d9 100644 --- a/bin/build/index.js +++ b/bin/build/index.js @@ -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)); @@ -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) => [ @@ -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 }, ), @@ -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)) { @@ -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(); diff --git a/bin/prepublish.js b/bin/prepublish.js index 5a20f2dd..da7dcae6 100644 --- a/bin/prepublish.js +++ b/bin/prepublish.js @@ -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); @@ -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'); } diff --git a/package.json b/package.json index 5268f6ec..5e0cdc5d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4584ac6..168d69cd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -55,6 +55,9 @@ importers: semver: specifier: ^7.6.3 version: 7.6.3 + tinypool: + specifier: 1.0.2 + version: 1.0.2 typescript: specifier: ~5.7.2 version: 5.7.2 @@ -6507,6 +6510,10 @@ packages: tinyexec@0.3.1: resolution: {integrity: sha512-WiCJLEECkO18gwqIp6+hJg0//p23HXp4S+gGtAKu3mI2F2/sXC4FvHvXvB0zJVVaTPhx1/tOwdbRsa1sOBIKqQ==} + tinypool@1.0.2: + resolution: {integrity: sha512-al6n+QEANGFOMf/dmUMsuS5/r9B06uwlyNjZZql/zv8J7ybHCgoihBNORZCY2mzUuAnomQa2JdhyHKzZxPCrFA==} + engines: {node: ^18.0.0 || >=20.0.0} + tmp@0.0.33: resolution: {integrity: sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==} engines: {node: '>=0.6.0'} @@ -14876,6 +14883,8 @@ snapshots: tinyexec@0.3.1: {} + tinypool@1.0.2: {} + tmp@0.0.33: dependencies: os-tmpdir: 1.0.2