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'
2
8
import path from 'node:path'
3
9
4
10
import { globSync as tinyGlobSync } from 'tinyglobby'
@@ -23,29 +29,48 @@ import {
23
29
24
30
const {
25
31
BABEL_RUNTIME ,
32
+ CONSTANTS ,
33
+ MODULE_SYNC ,
34
+ REQUIRE ,
26
35
ROLLUP_EXTERNAL_SUFFIX ,
36
+ VENDOR ,
27
37
depStatsPath,
28
38
rootDistPath,
29
39
rootPath,
30
40
rootSrcPath
31
41
} = constants
32
42
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`
35
46
36
47
const 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 )
39
50
40
51
const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
41
52
42
53
const processEnvTapRegExp =
43
54
/ \b p r o c e s s \. e n v (?: \. T A P | \[ [ ' " ] T A P [ ' " ] \] ) ( \s * \? [ ^ : ] + : \s * ) ? / g
44
55
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,}` ] , {
47
62
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
49
74
} ) ) {
50
75
rmSync ( filepath )
51
76
}
@@ -129,20 +154,15 @@ export default () => {
129
154
return true
130
155
} ,
131
156
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
- } ) ,
138
157
{
139
158
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
145
162
}
163
+ } ,
164
+ writeBundle ( ) {
165
+ removeDtsFilesSync ( CONSTANTS , distModuleSyncPath )
146
166
}
147
167
}
148
168
]
@@ -166,14 +186,33 @@ export default () => {
166
186
}
167
187
] ,
168
188
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
+ } ) ,
169
195
{
170
196
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
+ }
173
211
}
174
212
} ,
175
213
writeBundle ( ) {
176
- removeDtsFilesSync ( distRequirePath )
214
+ moveDtsFilesSync ( CONSTANTS , distRequirePath , rootDistPath )
215
+ removeDtsFilesSync ( '*' , distRequirePath )
177
216
updateDepStatsSync ( requireConfig . meta . depStats )
178
217
}
179
218
}
0 commit comments