1
- import { posix } from 'node:path' ;
2
- import { URL } from 'node:url' ;
3
1
import deepmerge from 'deepmerge' ;
4
2
import RspackChain from '../../compiled/rspack-chain' ;
5
- import { DEFAULT_ASSET_PREFIX } from '../constants' ;
6
3
import type {
7
4
FilenameConfig ,
8
5
NormalizedConfig ,
@@ -12,14 +9,7 @@ import type {
12
9
} from '../types' ;
13
10
import { color } from './vendors' ;
14
11
15
- export * from './fs' ;
16
- export * from './path' ;
17
- export * from './stats' ;
18
- export * from './vendors' ;
19
- export { RspackChain } ;
20
-
21
- // Lazy compilation was stabilized in Rspack v1.5.0
22
- export const rspackMinVersion = '1.5.0' ;
12
+ export { color , RspackChain } ;
23
13
24
14
export const getNodeEnv = ( ) : string => process . env . NODE_ENV || '' ;
25
15
export const setNodeEnv = ( env : string ) : void => {
@@ -59,149 +49,6 @@ export const cloneDeep = <T>(value: T): T => {
59
49
} ) ;
60
50
} ;
61
51
62
- const compareSemver = ( version1 : string , version2 : string ) => {
63
- const parts1 = version1 . split ( '.' ) . map ( Number ) ;
64
- const parts2 = version2 . split ( '.' ) . map ( Number ) ;
65
- const len = Math . max ( parts1 . length , parts2 . length ) ;
66
-
67
- for ( let i = 0 ; i < len ; i ++ ) {
68
- const item1 = parts1 [ i ] ?? 0 ;
69
- const item2 = parts2 [ i ] ?? 0 ;
70
- if ( item1 > item2 ) {
71
- return 1 ;
72
- }
73
- if ( item1 < item2 ) {
74
- return - 1 ;
75
- }
76
- }
77
-
78
- return 0 ;
79
- } ;
80
-
81
- /**
82
- * If the application overrides the Rspack version to a lower one,
83
- * we should check that the Rspack version is greater than the minimum
84
- * supported version.
85
- */
86
- export const isSatisfyRspackVersion = ( originalVersion : string ) : boolean => {
87
- let version = originalVersion ;
88
-
89
- // The nightly version of Rspack is to append `-canary-abc` to the current version
90
- if ( version . includes ( '-canary' ) ) {
91
- version = version . split ( '-canary' ) [ 0 ] ;
92
- }
93
-
94
- if ( version && / ^ [ \d . ] + $ / . test ( version ) ) {
95
- return compareSemver ( version , rspackMinVersion ) >= 0 ;
96
- }
97
-
98
- // ignore other unstable versions
99
- return true ;
100
- } ;
101
-
102
- export const removeLeadingSlash = ( s : string ) : string => s . replace ( / ^ \/ + / , '' ) ;
103
- export const removeTailingSlash = ( s : string ) : string => s . replace ( / \/ + $ / , '' ) ;
104
- export const addTrailingSlash = ( s : string ) : string =>
105
- s . endsWith ( '/' ) ? s : `${ s } /` ;
106
-
107
- export const formatPublicPath = (
108
- publicPath : string ,
109
- withSlash = true ,
110
- ) : string => {
111
- // 'auto' is a magic value in Rspack and we should not add trailing slash
112
- if ( publicPath === 'auto' ) {
113
- return publicPath ;
114
- }
115
-
116
- return withSlash
117
- ? addTrailingSlash ( publicPath )
118
- : removeTailingSlash ( publicPath ) ;
119
- } ;
120
-
121
- export const getPublicPathFromChain = (
122
- chain : RspackChain ,
123
- withSlash = true ,
124
- ) : string => {
125
- const publicPath : Rspack . PublicPath = chain . output . get ( 'publicPath' ) ;
126
-
127
- if ( typeof publicPath === 'string' ) {
128
- return formatPublicPath ( publicPath , withSlash ) ;
129
- }
130
-
131
- return formatPublicPath ( DEFAULT_ASSET_PREFIX , withSlash ) ;
132
- } ;
133
-
134
- export const getPublicPathFromCompiler = (
135
- compiler : Rspack . Compiler | Rspack . Compilation ,
136
- ) : string => {
137
- const { publicPath } = compiler . options . output ;
138
-
139
- if ( typeof publicPath === 'string' ) {
140
- // 'auto' is a magic value in Rspack and behave like `publicPath: ""`
141
- if ( publicPath === 'auto' ) {
142
- return '' ;
143
- }
144
- return publicPath . endsWith ( '/' ) ? publicPath : `${ publicPath } /` ;
145
- }
146
-
147
- // publicPath function is not supported yet, fallback to default value
148
- return DEFAULT_ASSET_PREFIX ;
149
- } ;
150
-
151
- export const urlJoin = ( base : string , path : string ) => {
152
- const [ urlProtocol , baseUrl ] = base . split ( '://' ) ;
153
- return `${ urlProtocol } ://${ posix . join ( baseUrl , path ) } ` ;
154
- } ;
155
-
156
- // Can be replaced with URL.canParse when we drop support for Node.js 18
157
- export const canParse = ( url : string ) : boolean => {
158
- try {
159
- new URL ( url ) ;
160
- return true ;
161
- } catch {
162
- return false ;
163
- }
164
- } ;
165
-
166
- export const ensureAssetPrefix = (
167
- url : string ,
168
- assetPrefix : Rspack . PublicPath = DEFAULT_ASSET_PREFIX ,
169
- ) : string => {
170
- // The use of an absolute URL without a protocol is technically legal,
171
- // however it cannot be parsed as a URL instance, just return it.
172
- // e.g. str is //example.com/foo.js
173
- if ( url . startsWith ( '//' ) ) {
174
- return url ;
175
- }
176
-
177
- // If str is an complete URL, just return it.
178
- // Only absolute url with hostname & protocol can be parsed into URL instance.
179
- // e.g. str is https://example.com/foo.js
180
- if ( canParse ( url ) ) {
181
- return url ;
182
- }
183
-
184
- // 'auto' is a magic value in Rspack and behave like `publicPath: ""`
185
- if ( assetPrefix === 'auto' ) {
186
- return url ;
187
- }
188
-
189
- // function is not supported by this helper
190
- if ( typeof assetPrefix === 'function' ) {
191
- return url ;
192
- }
193
-
194
- if ( assetPrefix . startsWith ( 'http' ) ) {
195
- return urlJoin ( assetPrefix , url ) ;
196
- }
197
-
198
- if ( assetPrefix . startsWith ( '//' ) ) {
199
- return urlJoin ( `https:${ assetPrefix } ` , url ) . replace ( 'https:' , '' ) ;
200
- }
201
-
202
- return posix . join ( assetPrefix , url ) ;
203
- } ;
204
-
205
52
export function getFilename (
206
53
config : NormalizedConfig | NormalizedEnvironmentConfig ,
207
54
type : 'js' ,
@@ -299,24 +146,9 @@ export function partition<T>(
299
146
return [ truthy , falsy ] ;
300
147
}
301
148
302
- export const applyToCompiler = (
303
- compiler : Rspack . Compiler | Rspack . MultiCompiler ,
304
- apply : ( c : Rspack . Compiler , index : number ) => void ,
305
- ) : void => {
306
- if ( isMultiCompiler ( compiler ) ) {
307
- compiler . compilers . forEach ( apply ) ;
308
- } else {
309
- apply ( compiler , 0 ) ;
310
- }
311
- } ;
312
-
313
149
export const upperFirst = ( str : string ) : string =>
314
150
str ? str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) : '' ;
315
151
316
- // Determine if the string is a URL
317
- export const isURL = ( str : string ) : boolean =>
318
- str . startsWith ( 'http' ) || str . startsWith ( '//:' ) ;
319
-
320
152
export const createVirtualModule = ( content : string ) =>
321
153
`data:text/javascript,${ content } ` ;
322
154
@@ -325,12 +157,6 @@ export function isWebTarget(target: RsbuildTarget | RsbuildTarget[]): boolean {
325
157
return targets . includes ( 'web' ) || targets . includes ( 'web-worker' ) ;
326
158
}
327
159
328
- export const isMultiCompiler = (
329
- compiler : Rspack . Compiler | Rspack . MultiCompiler ,
330
- ) : compiler is Rspack . MultiCompiler => {
331
- return 'compilers' in compiler && Array . isArray ( compiler . compilers ) ;
332
- } ;
333
-
334
160
export function pick < T , U extends keyof T > (
335
161
obj : T ,
336
162
keys : readonly U [ ] ,
@@ -384,15 +210,6 @@ export const isTTY = (type: 'stdin' | 'stdout' = 'stdout'): boolean => {
384
210
) ;
385
211
} ;
386
212
387
- export const addCompilationError = (
388
- compilation : Rspack . Compilation ,
389
- message : string ,
390
- ) : void => {
391
- compilation . errors . push (
392
- new compilation . compiler . webpack . WebpackError ( message ) ,
393
- ) ;
394
- } ;
395
-
396
213
export async function hash ( data : string ) : Promise < string > {
397
214
const crypto = await import ( 'node:crypto' ) ;
398
215
// Available in Node.js v20.12.0
0 commit comments