@@ -11,6 +11,7 @@ import typescriptPlugin from '@rollup/plugin-typescript'
1111import { purgePolyfills } from 'unplugin-purge-polyfills'
1212
1313import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
14+ import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
1415import { spawnSync } from '@socketsecurity/registry/lib/spawn'
1516
1617import constants from '../scripts/constants.js'
@@ -42,7 +43,11 @@ const {
4243 VITEST
4344} = constants
4445
45- export const EXTERNAL_PACKAGES = [ '@socketsecurity/registry' , 'blessed' ]
46+ export const EXTERNAL_PACKAGES = [
47+ '@socketsecurity/registry' ,
48+ 'blessed' ,
49+ 'blessed-contrib'
50+ ]
4651
4752const builtinAliases = builtinModules . reduce ( ( o , n ) => {
4853 o [ n ] = `node:${ n } `
@@ -51,10 +56,6 @@ const builtinAliases = builtinModules.reduce((o, n) => {
5156
5257const danglingRequiresRegExp = / ^ \s * r e q u i r e \( [ " ' ] .+ ?[ " ' ] \) ; ? \r ? \n / gm
5358
54- // eslint-disable-next-line no-unused-vars
55- const blessedRequiresRegExp =
56- / (?< = r e q u i r e \( [ " ' ] ) b l e s s e d (?: \/ [ ^ " ' ] + ) ? (? = [ " ' ] \) ) / g
57-
5859const requireTinyColorsRegExp = / r e q u i r e \( [ " ' ] t i n y - c o l o r s [ " ' ] \) / g
5960
6061const requireUrlAssignmentRegExp =
@@ -101,8 +102,31 @@ export default function baseConfig(extendConfig = {}) {
101102 const shadowNpmInjectSrcPath = path . join ( rootSrcPath , 'shadow/npm/inject.ts' )
102103 const shadowNpmPathsSrcPath = path . join ( rootSrcPath , 'shadow/npm/paths.ts' )
103104
104- const extendPlugins = extendConfig . plugins ?? [ ]
105- const hasPlugin = name => ! ! extendPlugins . find ( p => p . name === name )
105+ const extendPlugins = Array . isArray ( extendConfig . plugins )
106+ ? extendConfig . plugins . slice ( )
107+ : [ ]
108+ const extractedPlugins = { __proto__ : null }
109+ if ( extendPlugins . length ) {
110+ for ( const pluginName of [
111+ 'babel' ,
112+ 'commonjs' ,
113+ 'json' ,
114+ 'node-resolve' ,
115+ 'typescript' ,
116+ 'unplugin-purge-polyfills'
117+ ] ) {
118+ for ( let i = 0 , { length } = extendPlugins ; i < length ; i += 1 ) {
119+ const p = extendPlugins [ i ]
120+ if ( p ?. name === pluginName ) {
121+ extractedPlugins [ pluginName ] = p
122+ // Remove from extendPlugins array.
123+ extendPlugins . splice ( i , 1 )
124+ length -= 1
125+ i -= 1
126+ }
127+ }
128+ }
129+ }
106130
107131 const config = {
108132 input : {
@@ -118,10 +142,17 @@ export default function baseConfig(extendConfig = {}) {
118142 : { } )
119143 } ,
120144 external ( id_ ) {
145+ const id = normalizeId ( id_ )
146+ if ( id . includes ( 'blessed' ) ) {
147+ console . log (
148+ id ,
149+ getPackageName ( id ) ,
150+ EXTERNAL_PACKAGES . includes ( getPackageName ( id ) )
151+ )
152+ }
121153 if ( id_ . endsWith ( ROLLUP_EXTERNAL_SUFFIX ) || isBuiltin ( id_ ) ) {
122154 return true
123155 }
124- const id = normalizeId ( id_ )
125156 return (
126157 id . endsWith ( '.d.cts' ) ||
127158 id . endsWith ( '.d.mts' ) ||
@@ -142,63 +173,42 @@ export default function baseConfig(extendConfig = {}) {
142173 } ,
143174 ...extendConfig ,
144175 plugins : [
145- ...( hasPlugin ( 'node-resolve' )
146- ? [ ]
147- : [
148- nodeResolve ( {
149- exportConditions : [ 'node' ] ,
150- extensions : [ '.mjs' , '.js' , '.json' , '.ts' ] ,
151- preferBuiltins : true
152- } )
153- ] ) ,
154- ...( hasPlugin ( 'json' ) ? [ ] : [ jsonPlugin ( ) ] ) ,
155- ...( hasPlugin ( 'typescript' )
156- ? [ ]
157- : [
158- typescriptPlugin ( {
159- include : [ 'src/**/*.ts' ] ,
160- noForceEmit : true ,
161- outputToFilesystem : true ,
162- // Lazily access constants.rootConfigPath.
163- tsconfig : path . join (
164- constants . rootConfigPath ,
165- 'tsconfig.rollup.json'
166- )
167- } )
168- ] ) ,
169- ...( hasPlugin ( 'commonjs' )
170- ? [ ]
171- : [
172- commonjsPlugin ( {
173- defaultIsModuleExports : true ,
174- extensions : [ '.cjs' , '.js' ] ,
175- ignoreDynamicRequires : true ,
176- ignoreGlobal : true ,
177- ignoreTryCatch : true ,
178- strictRequires : true
179- } )
180- ] ) ,
181- ...( hasPlugin ( 'babel' )
182- ? [ ]
183- : [
184- babelPlugin ( {
185- babelHelpers : 'runtime' ,
186- babelrc : false ,
187- // Lazily access constants.rootConfigPath.
188- configFile : path . join (
189- constants . rootConfigPath ,
190- 'babel.config.js'
191- ) ,
192- extensions : [ '.ts' , '.js' , '.cjs' , '.mjs' ]
193- } )
194- ] ) ,
195- ...( hasPlugin ( 'unplugin-purge-polyfills' )
196- ? [ ]
197- : [
198- purgePolyfills . rollup ( {
199- replacements : { }
200- } )
201- ] ) ,
176+ extractedPlugins [ 'node-resolve' ] ??
177+ nodeResolve ( {
178+ exportConditions : [ 'node' ] ,
179+ extensions : [ '.mjs' , '.js' , '.json' , '.ts' ] ,
180+ preferBuiltins : true
181+ } ) ,
182+ extractedPlugins [ 'json' ] ?? jsonPlugin ( ) ,
183+ extractedPlugins [ 'typescript' ] ??
184+ typescriptPlugin ( {
185+ include : [ 'src/**/*.ts' ] ,
186+ noForceEmit : true ,
187+ outputToFilesystem : true ,
188+ // Lazily access constants.rootConfigPath.
189+ tsconfig : path . join ( constants . rootConfigPath , 'tsconfig.rollup.json' )
190+ } ) ,
191+ extractedPlugins [ 'commonjs' ] ??
192+ commonjsPlugin ( {
193+ defaultIsModuleExports : true ,
194+ extensions : [ '.cjs' , '.js' ] ,
195+ ignoreDynamicRequires : true ,
196+ ignoreGlobal : true ,
197+ ignoreTryCatch : true ,
198+ strictRequires : true
199+ } ) ,
200+ extractedPlugins [ 'babel' ] ??
201+ babelPlugin ( {
202+ babelHelpers : 'runtime' ,
203+ babelrc : false ,
204+ // Lazily access constants.rootConfigPath.
205+ configFile : path . join ( constants . rootConfigPath , 'babel.config.js' ) ,
206+ extensions : [ '.ts' , '.js' , '.cjs' , '.mjs' ]
207+ } ) ,
208+ extractedPlugins [ 'unplugin-purge-polyfills' ] ??
209+ purgePolyfills . rollup ( {
210+ replacements : { }
211+ } ) ,
202212 // Inline process.env values.
203213 replacePlugin ( {
204214 delimiters : [ '(?<![\'"])\\b' , '(?![\'"])' ] ,
@@ -297,12 +307,19 @@ export default function baseConfig(extendConfig = {}) {
297307 find : danglingRequiresRegExp ,
298308 replace : ''
299309 } ) ,
300- // Replace require('blessed/lib/widgets/xyz') with require('../blessed/lib/widgets/xyz').
301- // socketModifyPlugin({
302- // find: blessedRequiresRegExp,
303- // replace: (id) => `./${id}`
304- // }),
305- ...( extendConfig . plugins ?? [ ] )
310+ // Replace requires like require('blessed/lib/widgets/screen') with
311+ // require('../blessed/lib/widgets/screen').
312+ ...[ /*'@socketsecurity/registry',*/ 'blessed' , 'blessed-contrib' ] . map ( n => {
313+ const requiresRegExp = new RegExp (
314+ `(?<=require\\(["'])${ escapeRegExp ( n ) } (?:=(?:\\/[^"']+)?["']\\))` ,
315+ 'g'
316+ )
317+ return socketModifyPlugin ( {
318+ find : requiresRegExp ,
319+ replace : id => `./${ id } `
320+ } )
321+ } ) ,
322+ ...extendPlugins
306323 ]
307324 }
308325
0 commit comments