Skip to content

Commit

Permalink
fix: fix terser worker thread when vite is linked
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 30, 2020
1 parent c976d10 commit a28419b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ import { Worker } from 'okie'
import { Terser } from 'types/terser'

export function terserPlugin(options: Terser.MinifyOptions): Plugin {
const worker = new Worker((code: string, options: Terser.MinifyOptions) => {
// eslint-disable-next-line
return require('terser').minify(code, options) as Terser.MinifyOutput
})
const worker = new Worker(
(basedir: string, code: string, options: Terser.MinifyOptions) => {
// when vite is linked, the worker thread won't share the same resolve
// root with vite itself, so we have to pass in the basedir and resolve
// terser first.
// eslint-disable-next-line
const terserPath = require.resolve('terser', {
paths: [basedir]
})
return require(terserPath).minify(code, options) as Terser.MinifyOutput
}
)

return {
name: 'vite:terser',

async renderChunk(code, _chunk, outputOptions) {
const res = await worker.run(code, {
const res = await worker.run(__dirname, code, {
...options,
sourceMap: !!outputOptions.sourcemap,
module: outputOptions.format.startsWith('es'),
Expand Down

0 comments on commit a28419b

Please sign in to comment.