@@ -10,6 +10,7 @@ import replacePlugin from '@rollup/plugin-replace'
1010import { purgePolyfills } from 'unplugin-purge-polyfills'
1111
1212import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
13+ import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
1314import { spawnSync } from '@socketsecurity/registry/lib/spawn'
1415import { stripAnsi } from '@socketsecurity/registry/lib/strings'
1516
@@ -44,7 +45,9 @@ export const EXTERNAL_PACKAGES = [
4445]
4546
4647const builtinAliases = builtinModules . reduce ( ( o , n ) => {
47- o [ n ] = `node:${ n } `
48+ if ( ! n . startsWith ( 'node:' ) ) {
49+ o [ n ] = `node:${ n } `
50+ }
4851 return o
4952} , { } )
5053
@@ -81,13 +84,33 @@ function getSocketCliVersionHash() {
8184 return _socketVersionHash
8285}
8386
87+ const requiredToVarName = new Map ( )
88+ function getVarNameForRequireId ( filename , id , lookbehindContent ) {
89+ const key = `${ filename } :${ id } `
90+ let varName = requiredToVarName . get ( key )
91+ if ( varName ) {
92+ return varName
93+ }
94+ const varNameRegExp = new RegExp (
95+ `(?<=var +)[$\\w]+(?=\\s*=\\s*require[$\\w]*\\(["']${ escapeRegExp ( id ) } ["']\\))` ,
96+ )
97+ varName = varNameRegExp . exec ( lookbehindContent ) ?. [ 0 ] ?? ''
98+ if ( varName ) {
99+ requiredToVarName . set ( key , varName )
100+ }
101+ return varName
102+ }
103+
84104export default function baseConfig ( extendConfig = { } ) {
85105 // Lazily access constants path properties.
86106 const { configPath, rootPath } = constants
107+
87108 const nmPath = path . join ( rootPath , NODE_MODULES )
109+
88110 const extendPlugins = Array . isArray ( extendConfig . plugins )
89111 ? extendConfig . plugins . slice ( )
90112 : [ ]
113+
91114 const extractedPlugins = { __proto__ : null }
92115 if ( extendPlugins . length ) {
93116 for ( const pluginName of [
@@ -237,7 +260,7 @@ export default function baseConfig(extendConfig = {}) {
237260 } ) ,
238261 // Convert un-prefixed built-in imports into "node:"" prefixed forms.
239262 replacePlugin ( {
240- delimiters : [ '(?<=(?:require[$\\d ]*\\(|from\\s*)["\'])' , '(?=["\'])' ] ,
263+ delimiters : [ '(?<=(?:require[$\\w ]*\\(|from\\s*)["\'])' , '(?=["\'])' ] ,
241264 preventAssignment : false ,
242265 values : builtinAliases ,
243266 } ) ,
@@ -246,17 +269,17 @@ export default function baseConfig(extendConfig = {}) {
246269 // builds which causes 'tiny-colors' to be treated as an external, not bundled,
247270 // require.
248271 socketModifyPlugin ( {
249- find : / r e q u i r e [ $ \d ] * \( [ " ' ] t i n y - c o l o r s [ " ' ] \) / g,
272+ find : / r e q u i r e [ $ \w ] * \( [ " ' ] t i n y - c o l o r s [ " ' ] \) / g,
250273 replace : "require('yoctocolors-cjs')" ,
251274 } ) ,
252275 // Try to convert `require('u' + 'rl')` into something like `require$$2$3`.
253276 socketModifyPlugin ( {
254- find : / r e q u i r e [ $ \d ] * \( [ " ' ] u [ " ' ] \s * \+ \s * [ " ' ] r l [ " ' ] \) / g,
255- replace ( match ) {
277+ find : / r e q u i r e [ $ \w ] * \( [ " ' ] u [ " ' ] \s * \+ \s * [ " ' ] r l [ " ' ] \) / g,
278+ replace ( match , index ) {
279+ const { fileName } = this . chunk
280+ const beforeMatch = this . input . slice ( 0 , index )
256281 return (
257- / (?< = v a r + ) [ $ \w ] + (? = \s * = \s * r e q u i r e [ $ \d ] * \( [ " ' ] n o d e : u r l [ " ' ] \) ) / . exec (
258- this . input ,
259- ) ?. [ 0 ] ?? match
282+ getVarNameForRequireId ( fileName , 'node:url' , beforeMatch ) || match
260283 )
261284 } ,
262285 } ) ,
@@ -265,9 +288,21 @@ export default function baseConfig(extendConfig = {}) {
265288 // require('node:util')
266289 // require('graceful-fs')
267290 socketModifyPlugin ( {
268- find : / ^ \s * r e q u i r e [ $ \d ] * \( [ " ' ] .+ ?[ " ' ] \) ; ? \r ? \n / gm,
291+ find : / ^ \s * r e q u i r e [ $ \w ] * \( [ " ' ] .+ ?[ " ' ] \) ; ? \r ? \n / gm,
269292 replace : '' ,
270293 } ) ,
294+ // Reduce duplicate require('node:...') variable assignments.
295+ socketModifyPlugin ( {
296+ find : / v a r + ( [ $ \w ] + ) \s * = \s * r e q u i r e [ $ \w ] * \( [ " ' ] ( n o d e : .+ ?) [ " ' ] \) / g,
297+ replace ( match , currVarName , id , index ) {
298+ const { fileName } = this . chunk
299+ const beforeMatch = this . input . slice ( 0 , index )
300+ const prevVarName = getVarNameForRequireId ( fileName , id , beforeMatch )
301+ return ! prevVarName || currVarName === prevVarName
302+ ? match
303+ : `var ${ currVarName } = ${ prevVarName } `
304+ } ,
305+ } ) ,
271306 ...extendPlugins ,
272307 ] ,
273308 }
0 commit comments