Skip to content

Commit 76eb4f0

Browse files
committed
up
1 parent 00ac75b commit 76eb4f0

File tree

8 files changed

+51
-93
lines changed

8 files changed

+51
-93
lines changed

packages/core/src/config.ts

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -542,50 +542,17 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
542542
}
543543
};
544544

545-
const resolveShims = (shims?: Shims) => {
546-
const resolvedShims = {
545+
const resolveShims = (shims?: Shims): ResolvedShims => {
546+
return {
547547
cjs: {
548-
'import.meta.url': true,
548+
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true,
549549
},
550550
esm: {
551-
__filename: true,
552-
__dirname: true,
553-
require: false,
551+
__filename: shims?.esm?.__filename ?? false,
552+
__dirname: shims?.esm?.__dirname ?? false,
553+
require: shims?.esm?.require ?? false,
554554
},
555555
};
556-
557-
if (!shims) {
558-
return resolvedShims;
559-
}
560-
561-
if (shims.cjs) {
562-
if (typeof shims.cjs === 'boolean') {
563-
if (shims.cjs === true) {
564-
resolvedShims.cjs['import.meta.url'] = true;
565-
} else {
566-
resolvedShims.cjs['import.meta.url'] = false;
567-
}
568-
} else {
569-
resolvedShims.cjs['import.meta.url'] =
570-
shims.cjs['import.meta.url'] ?? false;
571-
}
572-
}
573-
574-
if (shims.esm) {
575-
if (typeof shims.esm === 'boolean') {
576-
if (shims.esm === true) {
577-
resolvedShims.esm.__filename = true;
578-
resolvedShims.esm.__dirname = true;
579-
resolvedShims.esm.require = true;
580-
}
581-
} else {
582-
resolvedShims.esm.__filename = shims.esm.__filename ?? false;
583-
resolvedShims.esm.__dirname = shims.esm.__dirname ?? false;
584-
resolvedShims.esm.require = shims.esm.require ?? false;
585-
}
586-
}
587-
588-
return resolvedShims;
589556
};
590557

591558
const composeShimsConfig = (

packages/core/src/types/config/index.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,14 @@ export type BannerAndFooter = {
4949
};
5050

5151
export type Shims = {
52-
cjs?:
53-
| boolean
54-
| {
55-
'import.meta.url'?: boolean;
56-
};
57-
esm?:
58-
| boolean
59-
| {
60-
__filename?: boolean;
61-
__dirname?: boolean;
62-
require?: boolean;
63-
};
52+
cjs?: {
53+
'import.meta.url'?: boolean;
54+
};
55+
esm?: {
56+
__filename?: boolean;
57+
__dirname?: boolean;
58+
require?: boolean;
59+
};
6460
};
6561

6662
export type ResolvedShims = {

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
112112
},
113113
},
114114
"node": {
115-
"__dirname": "node-module",
116-
"__filename": "node-module",
115+
"__dirname": false,
116+
"__filename": false,
117117
},
118118
"optimization": {
119119
"concatenateModules": true,

tests/integration/shims/cjs/rslib.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineConfig({
66
generateBundleEsmConfig(),
77
generateBundleCjsConfig({
88
shims: {
9-
cjs: true,
9+
cjs: { 'import.meta.url': true },
1010
},
1111
}),
1212
],

tests/integration/shims/esm/rslib.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { generateBundleEsmConfig } from 'test-helper';
44
export default defineConfig({
55
lib: [
66
generateBundleEsmConfig({
7-
shims: { esm: true },
7+
shims: { esm: { __dirname: true, __filename: true } },
88
source: {
99
entry: {
1010
index: './src/index.ts',
@@ -17,7 +17,7 @@ export default defineConfig({
1717
},
1818
}),
1919
generateBundleEsmConfig({
20-
shims: { esm: true },
20+
shims: { esm: { __dirname: true, __filename: true } },
2121
syntax: 'esnext',
2222
source: {
2323
entry: {
@@ -31,7 +31,7 @@ export default defineConfig({
3131
},
3232
}),
3333
generateBundleEsmConfig({
34-
shims: { esm: true },
34+
shims: { esm: { __dirname: true, __filename: true } },
3535
syntax: 'esnext',
3636
source: {
3737
entry: {

tests/integration/shims/esm/rslibShimsDisabled.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import config from './rslib.config';
33

44
export default defineConfig({
55
...config,
6-
lib: [config.lib[2]!].map((libConfig) => {
6+
lib: [config.lib[0]!, config.lib[2]!].map((libConfig) => {
77
libConfig.output!.distPath!.root =
88
libConfig.output!.distPath!.root!.replace(
99
'./dist/enabled',

tests/integration/shims/index.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ describe('ESM shims disabled', async () => {
6161
fixturePath,
6262
configPath: './rslibShimsDisabled.config.ts',
6363
});
64+
65+
expect(entries.esm0).not.toContain('fileURLToPath');
66+
6467
const context = vm.createContext({});
65-
const module = new vm.SourceTextModule(entries.esm, {
68+
const module = new vm.SourceTextModule(entries.esm1!, {
6669
context,
6770
});
6871

website/docs/en/config/lib/shims.mdx

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,39 @@
22

33
- **Type:**
44

5-
```ts
6-
type Shims = {
7-
cjs?:
8-
| boolean
9-
| {
10-
'import.meta.url'?: boolean;
11-
};
12-
esm?:
13-
| boolean
14-
| {
15-
__filename?: boolean;
16-
__dirname?: boolean;
17-
require?: boolean;
18-
};
19-
};
20-
```
5+
```ts
6+
type Shims = {
7+
cjs?: {
8+
'import.meta.url'?: boolean;
9+
};
10+
esm?: {
11+
__filename?: boolean;
12+
__dirname?: boolean;
13+
require?: boolean;
14+
};
15+
};
16+
```
2117

2218
- **Default:**
2319

24-
```js
25-
{
26-
cjs: {
27-
'import.meta.url': true,
28-
},
29-
esm: {
30-
__filename: true,
31-
__dirname: true,
32-
require: false,
33-
},
34-
}
35-
```
20+
```js
21+
{
22+
cjs: {
23+
'import.meta.url': true,
24+
},
25+
esm: {
26+
__filename: false,
27+
__dirname: false,
28+
require: false,
29+
},
30+
}
31+
```
3632

3733
Used to configure the shims for CommonJS and ESM output.
3834

3935
## shims.cjs
4036

41-
- set to `true` to enable all shims for CommonJS output.
42-
- set to `false` to disable all shims for CommonJS output.
43-
- set the fields to `true` to enable the corresponding shims for CommonJS output.
37+
Set the fields to `true` to enable the corresponding shims for CommonJS output.
4438

4539
### shims.cjs['import.meta.url']
4640

@@ -80,9 +74,7 @@ Options:
8074
8175
## shims.esm
8276
83-
- set to `true` to enable all shims for ESM output.
84-
- set to `false` to disable all shims for ESM output.
85-
- set the fields to `true` to enable the corresponding shims for ESM output.
77+
Set the fields to `true` to enable the corresponding shims for ESM output.
8678
8779
### shims.esm.\_\_filename
8880

0 commit comments

Comments
 (0)