Skip to content

Commit

Permalink
fix: start dts build as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 14, 2021
1 parent d0fb8df commit 1ebe663
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/errors.ts
Expand Up @@ -35,6 +35,6 @@ export function handleError(error: any) {
}
process.exitCode = 1
if (!isMainThread && parentPort) {
parentPort.postMessage('has-error')
parentPort.postMessage('error')
}
}
45 changes: 20 additions & 25 deletions src/index.ts
Expand Up @@ -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 */
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
}
})
}
}
)
)
Expand Down
10 changes: 8 additions & 2 deletions src/rollup.ts
Expand Up @@ -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()

Expand Down Expand Up @@ -196,7 +197,6 @@ async function runRollup(options: RollupConfig) {
)
} catch (error) {
logger.error('dts', 'Build error')
parentPort?.postMessage('error')
handleError(error)
}
}
Expand All @@ -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)
}
})
Expand All @@ -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)
})

0 comments on commit 1ebe663

Please sign in to comment.