| 1 |
import { fileURLToPath } from 'url'; |
| 2 |
|
| 3 |
import type { NormalizedOutputOptions, RenderedChunk } from 'rollup'; |
| 4 |
import { hasOwnProperty, isObject, merge } from 'smob'; |
| 5 |
|
| 6 |
import type { Options } from './type'; |
| 7 |
import { WorkerPool } from './worker-pool'; |
| 8 |
|
| 9 |
export default function terser(input: Options = {}) { |
| 10 |
const { maxWorkers, ...options } = input; |
| 11 |
|
| 12 |
let workerPool: WorkerPool | null | undefined; |
| 13 |
let numOfChunks = 0; |
| 14 |
let numOfWorkersUsed = 0; |
| 15 |
|
| 16 |
return { |
| 17 |
name: 'terser', |
| 18 |
|
| 19 |
async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) { |
| 20 |
if (!workerPool) { |
| 21 |
workerPool = new WorkerPool({ |
| 22 |
filePath: fileURLToPath(import.meta.url), |
| 23 |
maxWorkers |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
numOfChunks += 1; |
| 28 |
|
| 29 |
const defaultOptions: Options = { |
| 30 |
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string' |
| 31 |
}; |
| 32 |
|
| 33 |
if (outputOptions.format === 'es') { |
| 34 |
defaultOptions.module = true; |
| 35 |
} |
| 36 |
|
| 37 |
if (outputOptions.format === 'cjs') { |
| 38 |
defaultOptions.toplevel = true; |
| 39 |
} |
| 40 |
|
| 41 |
try { |
| 42 |
const { |
| 43 |
code: result, |
| 44 |
nameCache, |
| 45 |
sourceMap |
| 46 |
} = await workerPool.addAsync({ |
| 47 |
code, |
| 48 |
options: merge({}, options || {}, defaultOptions) |
| 49 |
}); |
| 50 |
|
| 51 |
if (options.nameCache && nameCache) { |
| 52 |
let vars: Record<string, any> = { |
| 53 |
props: {} |
| 54 |
}; |
| 55 |
|
| 56 |
if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) { |
| 57 |
vars = merge({}, options.nameCache.vars || {}, vars); |
| 58 |
} |
| 59 |
|
| 60 |
if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) { |
| 61 |
vars = merge({}, nameCache.vars, vars); |
| 62 |
} |
| 63 |
|
| 64 |
// eslint-disable-next-line no-param-reassign |
| 65 |
options.nameCache.vars = vars; |
| 66 |
|
| 67 |
let props: Record<string, any> = {}; |
| 68 |
|
| 69 |
if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) { |
| 70 |
// eslint-disable-next-line prefer-destructuring |
| 71 |
props = options.nameCache.props; |
| 72 |
} |
| 73 |
|
| 74 |
if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) { |
| 75 |
props = merge({}, nameCache.props, props); |
| 76 |
} |
| 77 |
|
| 78 |
// eslint-disable-next-line no-param-reassign |
| 79 |
options.nameCache.props = props; |
| 80 |
} |
| 81 |
|
| 82 |
if ((!!defaultOptions.sourceMap || !!options.sourceMap) && isObject(sourceMap)) { |
| 83 |
return { |
| 84 |
code: result, |
| 85 |
map: sourceMap |
| 86 |
}; |
| 87 |
} |
| 88 |
return result; |
| 89 |
} catch (e) { |
| 90 |
return Promise.reject(e); |
| 91 |
} finally { |
| 92 |
numOfChunks -= 1; |
| 93 |
if (numOfChunks === 0) { |
| 94 |
numOfWorkersUsed = workerPool.numWorkers; |
| 95 |
workerPool.close(); |
| 96 |
workerPool = null; |
| 97 |
} |
| 98 |
} |
| 99 |
}, |
| 100 |
|
| 101 |
get numOfWorkersUsed() { |
| 102 |
return numOfWorkersUsed; |
| 103 |
} |
| 104 |
}; |
| 105 |
} |