Skip to content

Commit

Permalink
fix: add support for terser 5 under node 12 and higher
Browse files Browse the repository at this point in the history
  • Loading branch information
Aghassi committed Mar 29, 2021
1 parent c47b770 commit f85bb87
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/terser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,23 @@ function main() {
// Allow for user to override terser binary for testing.
let terserBinary = process.env.TERSER_BINARY;
try {
// If necessary, get the new `terser` binary, added for >=4.3.0
terserBinary = terserBinary || require.resolve('terser/bin/terser');
// Node 12 and above will respect exports field in package.json, Terser 5 added these
// `process.version` returns vx.x.x, here we strip the `v` and get the major number
if (process.version.split('.')[0].replace('v', '') <= '10') {
// If necessary, get the new `terser` binary, added for >=4.3.0 <5
terserBinary = terserBinary || require.resolve('terser/bin/terser');
} else {
// Terser 5 with Node 12 or higher breaks compatiability with how Rules NodeJS supports
// getting the terser bin. An issue has been filed https://github.com/terser/terser/pull/971
// This is a temporary work around to allow us to proceed without downgrading
// or changing versions
// This gets the full path that to node_modules
// Then we hardcode the path to the bin directory since that's what we are looking for
// This will return a path like
// /private/var/tmp/_bazel_david.aghassi/a7bb26caa05a7d74fdb20e24a0f896f3/external/npm/_/node_modules
terserBinary =
terserBinary || `${require.resolve('terser').split('/terser/')[0]}/terser/bin/terser`;
}
} catch (e) {
try {
// If necessary, get the old `uglifyjs` binary from <4.3.0
Expand Down

0 comments on commit f85bb87

Please sign in to comment.