1- import { existsSync , mkdirSync , rmSync , writeFileSync } from 'node:fs'
1+ import {
2+ copyFileSync ,
3+ existsSync ,
4+ mkdirSync ,
5+ rmSync ,
6+ writeFileSync
7+ } from 'node:fs'
28import path from 'node:path'
39
410import { globSync as tinyGlobSync } from 'tinyglobby'
@@ -12,6 +18,7 @@ import { isRelative } from '@socketsecurity/registry/lib/path'
1218
1319import baseConfig from './rollup.base.config.mjs'
1420import constants from '../scripts/constants.js'
21+ import socketModifyPlugin from '../scripts/rollup/socket-modify-plugin.js'
1522import { readJsonSync } from '../scripts/utils/fs.js'
1623import { formatObject } from '../scripts/utils/objects.js'
1724import {
@@ -22,26 +29,48 @@ import {
2229
2330const {
2431 BABEL_RUNTIME ,
32+ CONSTANTS ,
33+ MODULE_SYNC ,
34+ REQUIRE ,
2535 ROLLUP_EXTERNAL_SUFFIX ,
36+ VENDOR ,
2637 depStatsPath,
2738 rootDistPath,
2839 rootPath,
2940 rootSrcPath
3041} = constants
3142
32- const CONSTANTS_JS = 'constants.js'
33- const CONSTANTS_STUB_CODE = `'use strict'\n\nmodule.exports = require('../${ CONSTANTS_JS } ')\n`
43+ const CONSTANTS_JS = `${ CONSTANTS } .js`
44+ const CONSTANTS_STUB_CODE = createStubCode ( `../${ CONSTANTS_JS } ` )
45+ const VENDOR_JS = `${ VENDOR } .js`
3446
3547const distConstantsPath = path . join ( rootDistPath , CONSTANTS_JS )
36- const distModuleSyncPath = path . join ( rootDistPath , 'module-sync' )
37- const distRequirePath = path . join ( rootDistPath , 'require' )
48+ const distModuleSyncPath = path . join ( rootDistPath , MODULE_SYNC )
49+ const distRequirePath = path . join ( rootDistPath , REQUIRE )
3850
3951const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
4052
41- function removeDtsFilesSync ( distPath ) {
42- for ( const filepath of tinyGlobSync ( [ '**/*.d.ts' ] , {
53+ const processEnvTapRegExp =
54+ / \b p r o c e s s \. e n v (?: \. T A P | \[ [ ' " ] T A P [ ' " ] \] ) ( \s * \? [ ^ : ] + : \s * ) ? / g
55+
56+ function createStubCode ( relFilepath ) {
57+ return `'use strict'\n\nmodule.exports = require('${ relFilepath } ')\n`
58+ }
59+
60+ function moveDtsFilesSync ( namePattern , srcPath , destPath ) {
61+ for ( const filepath of tinyGlobSync ( [ `**/${ namePattern } .d.ts{.map,}` ] , {
62+ absolute : true ,
63+ cwd : srcPath
64+ } ) ) {
65+ copyFileSync ( filepath , path . join ( destPath , path . basename ( filepath ) ) )
66+ rmSync ( filepath )
67+ }
68+ }
69+
70+ function removeDtsFilesSync ( namePattern , srcPath ) {
71+ for ( const filepath of tinyGlobSync ( [ `**/${ namePattern } .d.ts{.map,}` ] , {
4372 absolute : true ,
44- cwd : distPath
73+ cwd : srcPath
4574 } ) ) {
4675 rmSync ( filepath )
4776 }
@@ -127,12 +156,13 @@ export default () => {
127156 plugins : [
128157 {
129158 generateBundle ( _options , bundle ) {
130- const constantsBundle = bundle [ CONSTANTS_JS ]
131- if ( constantsBundle ) {
132- mkdirSync ( rootDistPath , { recursive : true } )
133- writeFileSync ( distConstantsPath , constantsBundle . code , 'utf8' )
134- bundle [ CONSTANTS_JS ] . code = CONSTANTS_STUB_CODE
159+ const data = bundle [ CONSTANTS_JS ]
160+ if ( data ?. type === 'chunk' ) {
161+ data . code = CONSTANTS_STUB_CODE
135162 }
163+ } ,
164+ writeBundle ( ) {
165+ removeDtsFilesSync ( CONSTANTS , distModuleSyncPath )
136166 }
137167 }
138168 ]
@@ -156,14 +186,33 @@ export default () => {
156186 }
157187 ] ,
158188 plugins : [
189+ // When process.env['TAP'] is found either remove it, if part of a ternary
190+ // operation, or replace it with `false`.
191+ socketModifyPlugin ( {
192+ find : processEnvTapRegExp ,
193+ replace : ( _match , ternary ) => ( ternary ? '' : 'false' )
194+ } ) ,
159195 {
160196 generateBundle ( _options , bundle ) {
161- if ( bundle [ CONSTANTS_JS ] ) {
162- bundle [ CONSTANTS_JS ] . code = CONSTANTS_STUB_CODE
197+ for ( const basename of Object . keys ( bundle ) ) {
198+ const data = bundle [ basename ]
199+ if ( data . type === 'chunk' ) {
200+ if ( basename === CONSTANTS_JS ) {
201+ mkdirSync ( rootDistPath , { recursive : true } )
202+ writeFileSync ( distConstantsPath , data . code , 'utf8' )
203+ data . code = CONSTANTS_STUB_CODE
204+ } else if (
205+ basename !== VENDOR_JS &&
206+ ! data . code . includes ( `'./${ VENDOR_JS } '` )
207+ ) {
208+ data . code = createStubCode ( `../${ MODULE_SYNC } /${ basename } ` )
209+ }
210+ }
163211 }
164212 } ,
165213 writeBundle ( ) {
166- removeDtsFilesSync ( distRequirePath )
214+ moveDtsFilesSync ( CONSTANTS , distRequirePath , rootDistPath )
215+ removeDtsFilesSync ( '*' , distRequirePath )
167216 updateDepStatsSync ( requireConfig . meta . depStats )
168217 }
169218 }
0 commit comments