1
- import { chmodSync , writeFileSync } from 'node:fs'
1
+ import { chmodSync , existsSync , writeFileSync } from 'node:fs'
2
2
import path from 'node:path'
3
- import { fileURLToPath } from 'node:url'
4
3
5
4
import { toSortedObject } from '@socketsecurity/registry/lib/objects'
6
5
import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
@@ -12,15 +11,16 @@ import { readJsonSync } from '../scripts/utils/fs.js'
12
11
import { formatObject } from '../scripts/utils/objects.js'
13
12
import { normalizeId , isBuiltin } from '../scripts/utils/packages.js'
14
13
15
- const { ROLLUP_EXTERNAL_SUFFIX } = constants
14
+ const {
15
+ ROLLUP_EXTERNAL_SUFFIX ,
16
+ depStatsPath,
17
+ rootDistPath,
18
+ rootPath,
19
+ rootSrcPath
20
+ } = constants
16
21
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' )
24
24
25
25
const binBasenames = [ 'cli.js' , 'npm-cli.js' , 'npx-cli.js' ]
26
26
const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
@@ -31,111 +31,104 @@ function setBinPerm(filepath) {
31
31
}
32
32
33
33
export default ( ) => {
34
- const legacyConfig = baseConfig ( {
34
+ const moduleSyncConfig = baseConfig ( {
35
35
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`
40
40
} ,
41
41
output : [
42
42
{
43
- dir : 'dist-legacy' ,
43
+ dir : path . relative ( rootPath , distModuleSyncPath ) ,
44
44
entryFileNames : '[name].js' ,
45
45
format : 'cjs' ,
46
46
exports : 'auto' ,
47
47
externalLiveBindings : false ,
48
48
freeze : false
49
49
}
50
50
] ,
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
+ } ,
51
58
plugins : [
52
59
{
53
60
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
-
97
61
for ( const binBasename of binBasenames ) {
98
- setBinPerm ( path . join ( distLegacyPath , binBasename ) )
62
+ setBinPerm ( path . join ( distModuleSyncPath , binBasename ) )
99
63
}
100
64
}
101
65
}
102
66
]
103
67
} )
104
68
105
- const syncEsmConfig = baseConfig ( {
69
+ const requireConfig = baseConfig ( {
106
70
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`
111
75
} ,
112
76
output : [
113
77
{
114
- dir : 'dist' ,
78
+ dir : path . relative ( rootPath , distRequirePath ) ,
115
79
entryFileNames : '[name].js' ,
116
80
format : 'cjs' ,
117
81
exports : 'auto' ,
118
82
externalLiveBindings : false ,
119
83
freeze : false
120
84
}
121
85
] ,
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
- } ,
129
86
plugins : [
130
87
{
131
88
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 ( )
132
125
for ( const binBasename of binBasenames ) {
133
- setBinPerm ( path . join ( distPath , binBasename ) )
126
+ setBinPerm ( path . join ( distRequirePath , binBasename ) )
134
127
}
135
128
}
136
129
}
137
130
]
138
131
} )
139
132
140
- return [ legacyConfig , syncEsmConfig ]
133
+ return [ moduleSyncConfig , requireConfig ]
141
134
}
0 commit comments