-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(terser): update to terser v5 and use its esm build
- Loading branch information
1 parent
5d53630
commit 4b67c5a
Showing
13 changed files
with
160 additions
and
100 deletions.
There are no files selected for viewing
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
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,64 @@ | ||
import fs from 'fs-extra'; | ||
import { join } from 'path'; | ||
import type { BuildOptions } from '../../utils/options'; | ||
import { rollup, Plugin } from 'rollup'; | ||
import { minify } from 'terser'; | ||
|
||
export function terserPlugin(opts: BuildOptions): Plugin { | ||
return { | ||
name: 'terserPlugin', | ||
resolveId(id) { | ||
if (id === 'terser') { | ||
return id; | ||
} | ||
return null; | ||
}, | ||
async load(id) { | ||
if (id === 'terser') { | ||
return await bundleTerser(opts); | ||
} | ||
return null; | ||
}, | ||
}; | ||
} | ||
|
||
async function bundleTerser(opts: BuildOptions) { | ||
const fileName = `terser-${opts.terserVersion.replace(/\./g, '_')}-bundle-cache${opts.isProd ? '.min' : ''}.js`; | ||
const cacheFile = join(opts.scriptsBuildDir, fileName); | ||
|
||
try { | ||
return await fs.readFile(cacheFile, 'utf8'); | ||
} catch (e) {} | ||
|
||
const rollupBuild = await rollup({ | ||
input: join(opts.nodeModulesDir, 'terser', 'main.js'), | ||
external: ['source-map'], | ||
}); | ||
|
||
const { output } = await rollupBuild.generate({ | ||
format: 'es', | ||
preferConst: true, | ||
strict: false, | ||
}); | ||
|
||
let code = output[0].code; | ||
|
||
if (opts.isProd) { | ||
const minified = await minify(code, { | ||
ecma: 2018, | ||
compress: { | ||
ecma: 2018, | ||
passes: 2, | ||
}, | ||
format: { | ||
ecma: 2018, | ||
comments: false, | ||
}, | ||
}); | ||
code = minified.code; | ||
} | ||
|
||
await fs.writeFile(cacheFile, code); | ||
|
||
return code; | ||
} |
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
Oops, something went wrong.