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'
@@ -23,29 +29,48 @@ import {
2329
2430const {
2531 BABEL_RUNTIME ,
32+ CONSTANTS ,
33+ MODULE_SYNC ,
34+ REQUIRE ,
2635 ROLLUP_EXTERNAL_SUFFIX ,
36+ VENDOR ,
2737 depStatsPath,
2838 rootDistPath,
2939 rootPath,
3040 rootSrcPath
3141} = constants
3242
33- const CONSTANTS_JS = 'constants.js'
34- 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`
3546
3647const distConstantsPath = path . join ( rootDistPath , CONSTANTS_JS )
37- const distModuleSyncPath = path . join ( rootDistPath , 'module-sync' )
38- const distRequirePath = path . join ( rootDistPath , 'require' )
48+ const distModuleSyncPath = path . join ( rootDistPath , MODULE_SYNC )
49+ const distRequirePath = path . join ( rootDistPath , REQUIRE )
3950
4051const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
4152
4253const processEnvTapRegExp =
4354 / \b p r o c e s s \. e n v (?: \. T A P | \[ [ ' " ] T A P [ ' " ] \] ) ( \s * \? [ ^ : ] + : \s * ) ? / g
4455
45- function removeDtsFilesSync ( distPath ) {
46- for ( const filepath of tinyGlobSync ( [ '**/*.d.ts' ] , {
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,}` ] , {
4762 absolute : true ,
48- cwd : distPath
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,}` ] , {
72+ absolute : true ,
73+ cwd : srcPath
4974 } ) ) {
5075 rmSync ( filepath )
5176 }
@@ -129,20 +154,15 @@ export default () => {
129154 return true
130155 } ,
131156 plugins : [
132- // When process.env['TAP'] is found either remove it, if part of a ternary
133- // operation, or replace it with `false`.
134- socketModifyPlugin ( {
135- find : processEnvTapRegExp ,
136- replace : ( match , ternary ) => ( ternary ? '' : 'false' )
137- } ) ,
138157 {
139158 generateBundle ( _options , bundle ) {
140- const constantsBundle = bundle [ CONSTANTS_JS ]
141- if ( constantsBundle ) {
142- mkdirSync ( rootDistPath , { recursive : true } )
143- writeFileSync ( distConstantsPath , constantsBundle . code , 'utf8' )
144- bundle [ CONSTANTS_JS ] . code = CONSTANTS_STUB_CODE
159+ const data = bundle [ CONSTANTS_JS ]
160+ if ( data ?. type === 'chunk' ) {
161+ data . code = CONSTANTS_STUB_CODE
145162 }
163+ } ,
164+ writeBundle ( ) {
165+ removeDtsFilesSync ( CONSTANTS , distModuleSyncPath )
146166 }
147167 }
148168 ]
@@ -166,14 +186,33 @@ export default () => {
166186 }
167187 ] ,
168188 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+ } ) ,
169195 {
170196 generateBundle ( _options , bundle ) {
171- if ( bundle [ CONSTANTS_JS ] ) {
172- 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+ }
173211 }
174212 } ,
175213 writeBundle ( ) {
176- removeDtsFilesSync ( distRequirePath )
214+ moveDtsFilesSync ( CONSTANTS , distRequirePath , rootDistPath )
215+ removeDtsFilesSync ( '*' , distRequirePath )
177216 updateDepStatsSync ( requireConfig . meta . depStats )
178217 }
179218 }
0 commit comments