11import { randomUUID } from 'node:crypto'
2- import { builtinModules , createRequire } from 'node:module'
2+ import { builtinModules } from 'node:module'
33import path from 'node:path'
44
5+ import { babel as babelPlugin } from '@rollup/plugin-babel'
56import commonjsPlugin from '@rollup/plugin-commonjs'
67import jsonPlugin from '@rollup/plugin-json'
78import { nodeResolve } from '@rollup/plugin-node-resolve'
89import replacePlugin from '@rollup/plugin-replace'
10+ import typescriptPlugin from '@rollup/plugin-typescript'
911import { purgePolyfills } from 'unplugin-purge-polyfills'
1012
1113import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
@@ -19,8 +21,6 @@ import {
1921 normalizeId
2022} from '../scripts/utils/packages.js'
2123
22- const require = createRequire ( import . meta. url )
23-
2424const {
2525 CONSTANTS ,
2626 INLINED_CYCLONEDX_CDXGEN_VERSION ,
@@ -32,7 +32,7 @@ const {
3232 INLINED_SOCKET_CLI_VERSION ,
3333 INLINED_SOCKET_CLI_VERSION_HASH ,
3434 INLINED_SYNP_VERSION ,
35- ROLLUP_ENTRY_SUFFIX ,
35+ INSTRUMENT_WITH_SENTRY ,
3636 ROLLUP_EXTERNAL_SUFFIX ,
3737 SHADOW_NPM_BIN ,
3838 SHADOW_NPM_INJECT ,
@@ -43,12 +43,7 @@ const {
4343} = constants
4444
4545export const EXTERNAL_PACKAGES = [
46- '@socketregistry/hyrious__bun.lockb' ,
47- '@socketregistry/indent-string' ,
48- '@socketregistry/is-interactive' ,
49- '@socketregistry/packageurl-js' ,
5046 '@socketsecurity/registry' ,
51- '@socketsecurity/sdk' ,
5247 'blessed'
5348]
5449
@@ -61,6 +56,7 @@ const builtinAliases = builtinModules.reduce((o, n) => {
6156
6257const customResolver = nodeResolve ( {
6358 exportConditions : [ 'node' ] ,
59+ extensions : [ '.mjs' , '.js' , '.json' , '.ts' ] ,
6460 preferBuiltins : true
6561} )
6662
@@ -128,11 +124,22 @@ export default function baseConfig(extendConfig = {}) {
128124 const shadowNpmInjectSrcPath = path . join ( rootSrcPath , 'shadow/npm/inject.ts' )
129125 const shadowNpmPathsSrcPath = path . join ( rootSrcPath , 'shadow/npm/paths.ts' )
130126
131- // Lazily access constants.babelConfigPath.
132- const babelConfig = require ( constants . babelConfigPath )
133- const tsPlugin = require ( 'rollup-plugin-ts' )
127+ const extendPlugins = extendConfig . plugins ?? [ ]
128+ const hasPlugin = name => ! ! extendPlugins . find ( p => p . name === name )
134129
135130 const config = {
131+ input : {
132+ cli : `${ rootSrcPath } /cli.ts` ,
133+ [ CONSTANTS ] : `${ rootSrcPath } /constants.ts` ,
134+ [ SHADOW_NPM_BIN ] : `${ rootSrcPath } /shadow/npm/bin.ts` ,
135+ [ SHADOW_NPM_INJECT ] : `${ rootSrcPath } /shadow/npm/inject.ts` ,
136+ // Lazily access constants.ENV[INLINED_SOCKET_CLI_SENTRY_BUILD].
137+ ...( constants . ENV [ INLINED_SOCKET_CLI_SENTRY_BUILD ]
138+ ? {
139+ [ INSTRUMENT_WITH_SENTRY ] : `${ rootSrcPath } /${ INSTRUMENT_WITH_SENTRY } .ts`
140+ }
141+ : { } )
142+ } ,
136143 external ( id_ ) {
137144 if ( id_ . endsWith ( ROLLUP_EXTERNAL_SUFFIX ) || isBuiltin ( id_ ) ) {
138145 return true
@@ -160,20 +167,54 @@ export default function baseConfig(extendConfig = {}) {
160167 } ,
161168 ...extendConfig ,
162169 plugins : [
163- customResolver ,
164- jsonPlugin ( ) ,
165- tsPlugin ( {
166- transpiler : 'babel' ,
167- browserslist : false ,
168- transpileOnly : true ,
169- exclude : [ '**/*.json' ] ,
170- babelConfig,
171- // Lazily access constants.tsconfigPath.
172- tsconfig : constants . tsconfigPath
173- } ) ,
174- purgePolyfills . rollup ( {
175- replacements : { }
176- } ) ,
170+ ...( hasPlugin ( 'node-resolve' ) ? [ ] : [ customResolver ] ) ,
171+ ...( hasPlugin ( 'json' ) ? [ ] : [ jsonPlugin ( ) ] ) ,
172+ ...( hasPlugin ( 'typescript' )
173+ ? [ ]
174+ : [
175+ typescriptPlugin ( {
176+ include : [ 'src/**/*.ts' ] ,
177+ noForceEmit : true ,
178+ // Lazily access constants.rootConfigPath.
179+ tsconfig : path . join (
180+ constants . rootConfigPath ,
181+ 'tsconfig.rollup.json'
182+ )
183+ } )
184+ ] ) ,
185+ ...( hasPlugin ( 'commonjs' )
186+ ? [ ]
187+ : [
188+ commonjsPlugin ( {
189+ defaultIsModuleExports : true ,
190+ extensions : [ '.cjs' , '.js' ] ,
191+ ignoreDynamicRequires : true ,
192+ ignoreGlobal : true ,
193+ ignoreTryCatch : true ,
194+ strictRequires : true
195+ } )
196+ ] ) ,
197+ ...( hasPlugin ( 'babel' )
198+ ? [ ]
199+ : [
200+ babelPlugin ( {
201+ babelHelpers : 'runtime' ,
202+ babelrc : false ,
203+ // Lazily access constants.rootConfigPath.
204+ configFile : path . join (
205+ constants . rootConfigPath ,
206+ 'babel.config.js'
207+ ) ,
208+ extensions : [ '.ts' , '.js' , '.cjs' , '.mjs' ]
209+ } )
210+ ] ) ,
211+ ...( hasPlugin ( 'unplugin-purge-polyfills' )
212+ ? [ ]
213+ : [
214+ purgePolyfills . rollup ( {
215+ replacements : { }
216+ } )
217+ ] ) ,
177218 // Inline process.env values.
178219 replacePlugin ( {
179220 delimiters : [ '(?<![\'"])\\b' , '(?![\'"])' ] ,
@@ -277,14 +318,6 @@ export default function baseConfig(extendConfig = {}) {
277318 // find: blessedRequiresRegExp,
278319 // replace: (id) => `../${id}`
279320 // }),
280- commonjsPlugin ( {
281- defaultIsModuleExports : true ,
282- extensions : [ '.cjs' , '.js' , '.ts' , `.ts${ ROLLUP_ENTRY_SUFFIX } ` ] ,
283- ignoreDynamicRequires : true ,
284- ignoreGlobal : true ,
285- ignoreTryCatch : true ,
286- strictRequires : true
287- } ) ,
288321 // Wrap require calls with SOCKET_INTEROP helper.
289322 socketModifyPlugin ( {
290323 find : requireAssignmentsRegExp ,
@@ -314,32 +347,36 @@ function ${SOCKET_INTEROP}(e) {
314347 ]
315348 }
316349
317- const output = (
318- Array . isArray ( config . output )
319- ? config . output
320- : config . output
321- ? [ config . output ]
322- : [ ]
323- ) . map ( o => ( {
324- ...o ,
325- chunkFileNames : '[name].js' ,
326- manualChunks : id_ => {
327- const id = normalizeId ( id_ )
328- switch ( id ) {
329- case constantsSrcPath :
330- return CONSTANTS
331- case shadowNpmBinSrcPath :
332- return SHADOW_NPM_BIN
333- case shadowNpmInjectSrcPath :
334- return SHADOW_NPM_INJECT
335- case shadowNpmPathsSrcPath :
336- return SHADOW_NPM_PATHS
337- default : {
338- return id . includes ( SLASH_NODE_MODULES_SLASH ) ? VENDOR : null
350+ const configOutputs = Array . isArray ( config . output )
351+ ? config . output
352+ : config . output
353+ ? [ config . output ]
354+ : [ ]
355+
356+ const output = configOutputs . map ( configOutput => {
357+ const o = {
358+ ...configOutput
359+ }
360+ if ( ! o . preserveModules ) {
361+ o . chunkFileNames = '[name].js'
362+ o . manualChunks = id_ => {
363+ const id = normalizeId ( id_ )
364+ switch ( id ) {
365+ case constantsSrcPath :
366+ return CONSTANTS
367+ case shadowNpmBinSrcPath :
368+ return SHADOW_NPM_BIN
369+ case shadowNpmInjectSrcPath :
370+ return SHADOW_NPM_INJECT
371+ case shadowNpmPathsSrcPath :
372+ return SHADOW_NPM_PATHS
373+ default :
374+ return id . includes ( SLASH_NODE_MODULES_SLASH ) ? VENDOR : null
339375 }
340376 }
341377 }
342- } ) )
378+ return o
379+ } )
343380
344381 // Replace hard-coded absolute paths in source with hard-coded relative paths.
345382 const replaceAbsPathsOutputPlugin = ( ( ) => {
0 commit comments