From 1ebe663b28a1bcc6317e110e79caeda3e5b10282 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Tue, 14 Dec 2021 13:52:03 +0800 Subject: [PATCH] fix: start dts build as soon as possible --- src/errors.ts | 2 +- src/index.ts | 45 ++++++++++++++++++++------------------------- src/rollup.ts | 10 ++++++++-- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/errors.ts b/src/errors.ts index 84155f18..07aa65f9 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -35,6 +35,6 @@ export function handleError(error: any) { } process.exitCode = 1 if (!isMainThread && parentPort) { - parentPort.postMessage('has-error') + parentPort.postMessage('error') } } diff --git a/src/index.ts b/src/index.ts index b17c149f..40485a81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -144,6 +144,25 @@ export async function build(_options: Options) { logger.info('CLI', 'Running in watch mode') } + if (options.dts) { + const worker = new Worker(path.join(__dirname, './rollup.js')) + worker.postMessage({ + configName: item?.name, + options: { + ...options, // functions cannot be cloned + esbuildPlugins: undefined, + esbuildOptions: undefined, + }, + }) + worker.on('message', (data) => { + if (data === 'error') { + process.exitCode = 1 + } else if (data === 'success') { + process.exitCode = 0 + } + }) + } + if (!options.dts?.only) { let existingOnSuccess: ChildProcess | undefined /** Files imported by the entry */ @@ -195,7 +214,7 @@ export async function build(_options: Options) { await runEsbuild(options, { pluginContainer, format, - css: (index === 0 || options.injectStyle) ? css : undefined, + css: index === 0 || options.injectStyle ? css : undefined, logger, buildDependencies, }) @@ -273,30 +292,6 @@ export async function build(_options: Options) { startWatcher() } - - if (options.dts) { - const hasTypescript = resolveFrom.silent(process.cwd(), 'typescript') - if (!hasTypescript) { - throw new Error(`You need to install "typescript" in your project`) - } - - const worker = new Worker(path.join(__dirname, './rollup.js')) - worker.postMessage({ - configName: item?.name, - options: { - ...options, // functions cannot be cloned - esbuildPlugins: undefined, - esbuildOptions: undefined, - }, - }) - worker.on('message', (data) => { - if (data === 'error') { - process.exitCode = 1 - } else if (data === 'success') { - process.exitCode = 0 - } - }) - } } ) ) diff --git a/src/rollup.ts b/src/rollup.ts index 5edb8a00..d01a3e05 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -11,6 +11,7 @@ import { createLogger, setSilent } from './log' import { getDeps } from './load' import path from 'path' import { reportSize } from './lib/report-size' +import resolveFrom from 'resolve-from' const logger = createLogger() @@ -196,7 +197,6 @@ async function runRollup(options: RollupConfig) { ) } catch (error) { logger.error('dts', 'Build error') - parentPort?.postMessage('error') handleError(error) } } @@ -219,7 +219,6 @@ async function watchRollup(options: { parentPort?.postMessage('success') } else if (event.code === 'ERROR') { logger.error('dts', 'Build failed') - parentPort?.postMessage('error') handleError(event.error) } }) @@ -237,5 +236,12 @@ const startRollup = async (options: NormalizedOptions) => { parentPort?.on('message', (data) => { logger.setName(data.configName) + const hasTypescript = resolveFrom.silent(process.cwd(), 'typescript') + if (!hasTypescript) { + logger.error('dts', `You need to install "typescript" in your project`) + parentPort?.postMessage('error') + parentPort?.close() + return + } startRollup(data.options) })