1- import { chmodSync , writeFileSync } from 'node:fs'
1+ import { chmodSync , existsSync , writeFileSync } from 'node:fs'
22import path from 'node:path'
3- import { fileURLToPath } from 'node:url'
43
54import { toSortedObject } from '@socketsecurity/registry/lib/objects'
65import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
@@ -12,15 +11,16 @@ import { readJsonSync } from '../scripts/utils/fs.js'
1211import { formatObject } from '../scripts/utils/objects.js'
1312import { normalizeId , isBuiltin } from '../scripts/utils/packages.js'
1413
15- const { ROLLUP_EXTERNAL_SUFFIX } = constants
14+ const {
15+ ROLLUP_EXTERNAL_SUFFIX ,
16+ depStatsPath,
17+ rootDistPath,
18+ rootPath,
19+ rootSrcPath
20+ } = constants
1621
17- const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) )
18-
19- const rootPath = path . resolve ( __dirname , '..' )
20- const depStatsPath = path . join ( rootPath , '.dep-stats.json' )
21- const distPath = path . join ( rootPath , 'dist' )
22- const distLegacyPath = path . join ( rootPath , 'dist-legacy' )
23- const srcPath = path . join ( rootPath , 'src' )
22+ const distModuleSyncPath = path . join ( rootDistPath , 'module-sync' )
23+ const distRequirePath = path . join ( rootDistPath , 'require' )
2424
2525const binBasenames = [ 'cli.js' , 'npm-cli.js' , 'npx-cli.js' ]
2626const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
@@ -31,111 +31,104 @@ function setBinPerm(filepath) {
3131}
3232
3333export default ( ) => {
34- const legacyConfig = baseConfig ( {
34+ const moduleSyncConfig = baseConfig ( {
3535 input : {
36- cli : `${ srcPath } /cli.ts` ,
37- 'npm-cli' : `${ srcPath } /shadow/npm-cli.ts` ,
38- 'npx-cli' : `${ srcPath } /shadow/npx-cli.ts` ,
39- 'npm-injection' : `${ srcPath } /shadow/npm-injection.ts`
36+ cli : `${ rootSrcPath } /cli.ts` ,
37+ 'npm-cli' : `${ rootSrcPath } /shadow/npm-cli.ts` ,
38+ 'npx-cli' : `${ rootSrcPath } /shadow/npx-cli.ts` ,
39+ 'npm-injection' : `${ rootSrcPath } /shadow/npm-injection.ts`
4040 } ,
4141 output : [
4242 {
43- dir : 'dist-legacy' ,
43+ dir : path . relative ( rootPath , distModuleSyncPath ) ,
4444 entryFileNames : '[name].js' ,
4545 format : 'cjs' ,
4646 exports : 'auto' ,
4747 externalLiveBindings : false ,
4848 freeze : false
4949 }
5050 ] ,
51+ external ( id_ ) {
52+ if ( id_ . endsWith ( ROLLUP_EXTERNAL_SUFFIX ) || isBuiltin ( id_ ) ) {
53+ return true
54+ }
55+ const id = normalizeId ( id_ )
56+ return ! ( isRelative ( id ) || id . startsWith ( rootSrcPath ) )
57+ } ,
5158 plugins : [
5259 {
5360 writeBundle ( ) {
54- const { content : pkgJson } = editablePkgJson
55- const { '@cyclonedx/cdxgen' : cdxgenRange , synp : synpRange } =
56- pkgJson . dependencies
57- const { depStats } = legacyConfig . meta
58-
59- // Manually add @cyclonedx /cdxgen and synp as they are not directly
60- // referenced in the code but used through spawned processes.
61- depStats . dependencies [ '@cyclonedx/cdxgen' ] = cdxgenRange
62- depStats . dependencies . synp = synpRange
63- depStats . external [ '@cyclonedx/cdxgen' ] = cdxgenRange
64- depStats . external . synp = synpRange
65-
66- try {
67- // Remove transitives from dependencies
68- const oldDepStats = readJsonSync ( depStatsPath )
69- for ( const key of Object . keys ( oldDepStats . transitives ) ) {
70- if ( pkgJson . dependencies [ key ] ) {
71- depStats . transitives [ key ] = pkgJson . dependencies [ key ]
72- depStats . external [ key ] = pkgJson . dependencies [ key ]
73- delete depStats . dependencies [ key ]
74- }
75- }
76- } catch { }
77-
78- depStats . dependencies = toSortedObject ( depStats . dependencies )
79- depStats . devDependencies = toSortedObject ( depStats . devDependencies )
80- depStats . esm = toSortedObject ( depStats . esm )
81- depStats . external = toSortedObject ( depStats . external )
82- depStats . transitives = toSortedObject ( depStats . transitives )
83-
84- // Write dep stats
85- writeFileSync ( depStatsPath , `${ formatObject ( depStats ) } \n` , 'utf8' )
86-
87- // Update dependencies with additional inlined modules
88- editablePkgJson
89- . update ( {
90- dependencies : {
91- ...depStats . dependencies ,
92- ...depStats . transitives
93- }
94- } )
95- . saveSync ( )
96-
9761 for ( const binBasename of binBasenames ) {
98- setBinPerm ( path . join ( distLegacyPath , binBasename ) )
62+ setBinPerm ( path . join ( distModuleSyncPath , binBasename ) )
9963 }
10064 }
10165 }
10266 ]
10367 } )
10468
105- const syncEsmConfig = baseConfig ( {
69+ const requireConfig = baseConfig ( {
10670 input : {
107- cli : `${ srcPath } /cli.ts` ,
108- 'npm-cli' : `${ srcPath } /shadow/npm-cli.ts` ,
109- 'npx-cli' : `${ srcPath } /shadow/npx-cli.ts` ,
110- 'npm-injection' : `${ srcPath } /shadow/npm-injection.ts`
71+ cli : `${ rootSrcPath } /cli.ts` ,
72+ 'npm-cli' : `${ rootSrcPath } /shadow/npm-cli.ts` ,
73+ 'npx-cli' : `${ rootSrcPath } /shadow/npx-cli.ts` ,
74+ 'npm-injection' : `${ rootSrcPath } /shadow/npm-injection.ts`
11175 } ,
11276 output : [
11377 {
114- dir : 'dist' ,
78+ dir : path . relative ( rootPath , distRequirePath ) ,
11579 entryFileNames : '[name].js' ,
11680 format : 'cjs' ,
11781 exports : 'auto' ,
11882 externalLiveBindings : false ,
11983 freeze : false
12084 }
12185 ] ,
122- external ( id_ ) {
123- if ( id_ . endsWith ( ROLLUP_EXTERNAL_SUFFIX ) || isBuiltin ( id_ ) ) {
124- return true
125- }
126- const id = normalizeId ( id_ )
127- return ! ( isRelative ( id ) || id . startsWith ( srcPath ) )
128- } ,
12986 plugins : [
13087 {
13188 writeBundle ( ) {
89+ const { content : pkgJson } = editablePkgJson
90+ const oldDepStats = existsSync ( depStatsPath )
91+ ? readJsonSync ( depStatsPath )
92+ : undefined
93+ const { depStats } = requireConfig . meta
94+ Object . assign ( depStats . dependencies , {
95+ // Manually add @cyclonedx /cdxgen and synp as they are not directly
96+ // referenced in the code but used through spawned processes.
97+ '@cyclonedx/cdxgen' : pkgJson . dependencies [ '@cyclonedx/cdxgen' ] ,
98+ synp : pkgJson . dependencies . synp ,
99+ ...oldDepStats ?. dependencies
100+ } )
101+ // Remove transitives from dependencies
102+ for ( const key of Object . keys ( oldDepStats ?. transitives ?? { } ) ) {
103+ if ( pkgJson . dependencies [ key ] ) {
104+ depStats . transitives [ key ] = pkgJson . dependencies [ key ]
105+ depStats . external [ key ] = pkgJson . dependencies [ key ]
106+ delete depStats . dependencies [ key ]
107+ }
108+ }
109+ depStats . dependencies = toSortedObject ( depStats . dependencies )
110+ depStats . devDependencies = toSortedObject ( depStats . devDependencies )
111+ depStats . esm = toSortedObject ( depStats . esm )
112+ depStats . external = toSortedObject ( depStats . external )
113+ depStats . transitives = toSortedObject ( depStats . transitives )
114+ // Write dep stats
115+ writeFileSync ( depStatsPath , `${ formatObject ( depStats ) } \n` , 'utf8' )
116+ // Update dependencies with additional inlined modules
117+ editablePkgJson
118+ . update ( {
119+ dependencies : {
120+ ...depStats . dependencies ,
121+ ...depStats . transitives
122+ }
123+ } )
124+ . saveSync ( )
132125 for ( const binBasename of binBasenames ) {
133- setBinPerm ( path . join ( distPath , binBasename ) )
126+ setBinPerm ( path . join ( distRequirePath , binBasename ) )
134127 }
135128 }
136129 }
137130 ]
138131 } )
139132
140- return [ legacyConfig , syncEsmConfig ]
133+ return [ moduleSyncConfig , requireConfig ]
141134}
0 commit comments