File tree Expand file tree Collapse file tree 7 files changed +60
-2
lines changed Expand file tree Collapse file tree 7 files changed +60
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { generateBundleEsmConfig } from '@e2e/helper' ;
2
+ import { defineConfig } from '@rslib/core' ;
3
+
4
+ export default defineConfig ( {
5
+ lib : [ generateBundleEsmConfig ( __dirname ) ] ,
6
+ output : {
7
+ target : 'node' ,
8
+ } ,
9
+ source : {
10
+ entry : {
11
+ main : './src/index.ts' ,
12
+ } ,
13
+ } ,
14
+ } ) ;
Original file line number Diff line number Diff line change
1
+ export const foo = ( ) => {
2
+ const d1 = __dirname ;
3
+ const d2 = __dirname ;
4
+ const f1 = __filename ;
5
+ const f2 = __filename ;
6
+ const importMetaUrl = import . meta. url ;
7
+ } ;
Original file line number Diff line number Diff line change
1
+ import { join } from 'node:path' ;
2
+ import { buildAndGetResults } from '@e2e/helper' ;
3
+ import { expect , test } from 'vitest' ;
4
+
5
+ test ( 'shims for __dirname and __filename in ESM' , async ( ) => {
6
+ const fixturePath = join ( __dirname , 'esm' ) ;
7
+ const { entries } = await buildAndGetResults ( fixturePath ) ;
8
+ for ( const shim of [
9
+ 'import {fileURLToPath as __webpack_fileURLToPath__} from "url";' ,
10
+ "var src_dirname = __webpack_fileURLToPath__(import.meta.url + '/..').slice(0, -1);" ,
11
+ 'var src_filename = __webpack_fileURLToPath__(import.meta.url);' ,
12
+ // import.meta.url should not be substituted
13
+ 'const importMetaUrl = import.meta.url;' ,
14
+ ] ) {
15
+ expect ( entries . esm ) . toContain ( shim ) ;
16
+ }
17
+ } ) ;
18
+
19
+ test . todo ( 'shims for import.meta.url in CJS' , async ( ) => { } ) ;
Original file line number Diff line number Diff line change @@ -186,6 +186,13 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
186
186
type : 'modern-module' ,
187
187
} ,
188
188
} ,
189
+ module : {
190
+ parser : {
191
+ javascript : {
192
+ importMeta : false ,
193
+ } ,
194
+ } ,
195
+ } ,
189
196
optimization : {
190
197
concatenateModules : true ,
191
198
} ,
@@ -430,6 +437,9 @@ const composeTargetConfig = (target = 'web'): RsbuildConfig => {
430
437
tools : {
431
438
rspack : {
432
439
target : [ 'node' ] ,
440
+ // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
441
+ // and leave them as-is in the rest of the cases.
442
+ // { node: { __dirname: ..., __filename: ... } }
433
443
} ,
434
444
} ,
435
445
output : {
Original file line number Diff line number Diff line change @@ -3,5 +3,6 @@ export { runCli } from './cli/commands';
3
3
export { defineConfig , loadConfig } from './config' ;
4
4
export { build } from './build' ;
5
5
export { logger } from './utils/logger' ;
6
+ export type * from './types' ;
6
7
7
8
export const version : string = RSLIB_VERSION ;
Original file line number Diff line number Diff line change 1
- export * from './config' ;
2
- export * from './utils' ;
1
+ export type * from './config' ;
2
+ export type * from './utils' ;
Original file line number Diff line number Diff line change @@ -43,6 +43,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
43
43
" outputModule" : true ,
44
44
},
45
45
" externalsType" : " module-import" ,
46
+ " module" : {
47
+ " parser" : {
48
+ " javascript" : {
49
+ " importMeta" : false ,
50
+ },
51
+ },
52
+ },
46
53
" optimization" : {
47
54
" concatenateModules" : true ,
48
55
},
You can’t perform that action at this time.
0 commit comments