@@ -162,8 +162,30 @@ function main() {
162162 // Allow for user to override terser binary for testing.
163163 let terserBinary = process . env . TERSER_BINARY ;
164164 try {
165- // If necessary, get the new `terser` binary, added for >=4.3.0
166- terserBinary = terserBinary || require . resolve ( 'terser/bin/terser' ) ;
165+ // Node 12 and above will respect exports field in package.json, Terser 5 added these
166+ // `process.version` returns vx.x.x, here we strip the `v` and get the major number
167+ if ( process . version . split ( '.' ) [ 0 ] . replace ( 'v' , '' ) <= '10' ) {
168+ // If necessary, get the new `terser` binary, added for >=4.3.0 <5
169+ terserBinary = terserBinary || require . resolve ( 'terser/bin/terser' ) ;
170+ } else {
171+ // Terser 5 with Node 12 or higher breaks compatiability with how Rules NodeJS supports
172+ // getting the terser bin. An issue has been filed https://github.com/terser/terser/pull/971
173+ // This is a temporary work around to allow us to proceed without downgrading
174+ // or changing versions
175+ // This gets the full path that to node_modules
176+ // Then we hardcode the path to the bin directory since that's what we are looking for
177+ // This will return a path like
178+ // /private/var/tmp/_bazel_david.aghassi/a7bb26caa05a7d74fdb20e24a0f896f3/external/npm/_/node_modules
179+ switch ( os . platform ( ) ) {
180+ case 'win32' :
181+ terserBinary = terserBinary ||
182+ `${ require . resolve ( 'terser' ) . split ( '\\terser\\' ) [ 0 ] } \\terser\\bin\\terser` ;
183+ break ;
184+ default :
185+ terserBinary =
186+ terserBinary || `${ require . resolve ( 'terser' ) . split ( '/terser/' ) [ 0 ] } /terser/bin/terser` ;
187+ }
188+ }
167189 } catch ( e ) {
168190 try {
169191 // If necessary, get the old `uglifyjs` binary from <4.3.0
0 commit comments