-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #742
- Loading branch information
Showing
9 changed files
with
172 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
*.log | ||
dist | ||
.cache | ||
.cache | ||
playground |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { MinifyOptions } from 'terser' | ||
import { PrettyError } from '../errors' | ||
import { createLogger } from '../log' | ||
import { Format, Options } from '../options' | ||
import { Plugin } from '../plugin' | ||
import { localRequire } from '../utils' | ||
|
||
const logger = createLogger() | ||
|
||
export const terserPlugin = ({ | ||
minifyOptions, | ||
format, | ||
terserOptions = {}, | ||
}: { | ||
minifyOptions: Options['minify'] | ||
format: Format | ||
terserOptions?: MinifyOptions | ||
}): Plugin => { | ||
return { | ||
name: 'terser', | ||
|
||
async renderChunk(code, info) { | ||
if (minifyOptions !== 'terser' || !/\.(cjs|js|mjs)$/.test(info.path)) | ||
return | ||
|
||
const terser: typeof import('terser') | undefined = localRequire('terser') | ||
|
||
if (!terser) { | ||
throw new PrettyError( | ||
'terser is required for terser minification. Please install it with `npm install terser -D`' | ||
) | ||
} | ||
|
||
const { minify } = terser | ||
|
||
const defaultOptions: MinifyOptions = {} | ||
|
||
if (format === 'esm') { | ||
defaultOptions.module = true | ||
} else { | ||
defaultOptions.toplevel = true | ||
} | ||
|
||
try { | ||
const minifiedOutput = await minify( | ||
{ [info.path]: code }, | ||
{ ...defaultOptions, ...terserOptions } | ||
) | ||
|
||
logger.info('TERSER', 'Minifying with Terser') | ||
|
||
if (!minifiedOutput.code) { | ||
logger.error('TERSER', 'Failed to minify with terser') | ||
} | ||
|
||
logger.success('TERSER', 'Terser Minification success') | ||
|
||
return { code: minifiedOutput.code!, map: minifiedOutput.map } | ||
} catch (e) { | ||
logger.error('TERSER', 'Failed to minify with terser') | ||
logger.error('TERSER', e) | ||
} | ||
|
||
return { code, map: info.map } | ||
}, | ||
} | ||
} |