Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rollup:master' into master
- Loading branch information
Showing
13 changed files
with
362 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,8 @@ | ||
import type { NormalizedOutputOptions, RenderedChunk } from 'rollup'; | ||
import type { MinifyOptions } from 'terser'; | ||
import { minify } from 'terser'; | ||
import { runWorker } from './worker'; | ||
import terser from './module'; | ||
|
||
export default function terser(options?: MinifyOptions) { | ||
return { | ||
name: 'terser', | ||
runWorker(); | ||
|
||
async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) { | ||
const defaultOptions: MinifyOptions = { | ||
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string' | ||
}; | ||
export * from './type'; | ||
|
||
if (outputOptions.format === 'es') { | ||
defaultOptions.module = true; | ||
} | ||
|
||
if (outputOptions.format === 'cjs') { | ||
defaultOptions.toplevel = true; | ||
} | ||
|
||
return minify(code, { ...defaultOptions, ...(options || {}) }); | ||
} | ||
}; | ||
} | ||
export default terser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { NormalizedOutputOptions, RenderedChunk } from 'rollup'; | ||
import { hasOwnProperty, isObject, merge } from 'smob'; | ||
|
||
import type { Options } from './type'; | ||
import { WorkerPool } from './worker-pool'; | ||
|
||
export default function terser(options: Options = {}) { | ||
const workerPool = new WorkerPool({ | ||
filePath: __filename, | ||
maxWorkers: options.maxWorkers | ||
}); | ||
|
||
return { | ||
name: 'terser', | ||
|
||
async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) { | ||
const defaultOptions: Options = { | ||
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string' | ||
}; | ||
|
||
if (outputOptions.format === 'es') { | ||
defaultOptions.module = true; | ||
} | ||
|
||
if (outputOptions.format === 'cjs') { | ||
defaultOptions.toplevel = true; | ||
} | ||
|
||
try { | ||
const { code: result, nameCache } = await workerPool.addAsync({ | ||
code, | ||
options: merge({}, options || {}, defaultOptions) | ||
}); | ||
|
||
if (options.nameCache && nameCache) { | ||
let vars: Record<string, any> = { | ||
props: {} | ||
}; | ||
|
||
if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) { | ||
vars = merge({}, options.nameCache.vars || {}, vars); | ||
} | ||
|
||
if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) { | ||
vars = merge({}, nameCache.vars, vars); | ||
} | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
options.nameCache.vars = vars; | ||
|
||
let props: Record<string, any> = {}; | ||
|
||
if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) { | ||
// eslint-disable-next-line prefer-destructuring | ||
props = options.nameCache.props; | ||
} | ||
|
||
if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) { | ||
props = merge({}, nameCache.props, props); | ||
} | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
options.nameCache.props = props; | ||
} | ||
|
||
return result; | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { MinifyOptions } from 'terser'; | ||
|
||
export interface Options extends MinifyOptions { | ||
nameCache?: Record<string, any>; | ||
maxWorkers?: number; | ||
} | ||
|
||
export interface WorkerContext { | ||
code: string; | ||
options: Options; | ||
} | ||
|
||
export type WorkerCallback = (err: Error | null, output?: WorkerOutput) => void; | ||
|
||
export interface WorkerContextSerialized { | ||
code: string; | ||
options: string; | ||
} | ||
|
||
export interface WorkerOutput { | ||
code: string; | ||
nameCache?: Options['nameCache']; | ||
} | ||
|
||
export interface WorkerPoolOptions { | ||
filePath: string; | ||
maxWorkers?: number; | ||
} | ||
|
||
export interface WorkerPoolTask { | ||
context: WorkerContext; | ||
cb: WorkerCallback; | ||
} |
Oops, something went wrong.