Skip to content

Commit 91fd0b3

Browse files
committed
feat: add esModule option
1 parent feaa33d commit 91fd0b3

File tree

12 files changed

+96
-22
lines changed

12 files changed

+96
-22
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ console.log(example.hello); // 'world';
5050
console.log(example.foo); // { bar: 'baz' };
5151
```
5252

53+
## Options
54+
55+
### esModule
56+
57+
By default, `@rsbuild/plugin-toml` generates JS modules that use the ES modules syntax. If you want to generate a CommonJS module, you can set the `esModule` option to `false`.
58+
59+
- Type: `boolean`
60+
- Default: `true`
61+
- Example:
62+
63+
```js
64+
pluginToml({
65+
exModule: false,
66+
});
67+
```
68+
5369
## Type Declaration
5470

5571
When you import TOML files in TypeScript code, please create a `src/env.d.ts` file in your project and add the type declarations.

playground/src/a.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
foo = "bar"
2-
age = 1
1+
list = [1, 2]

playground/src/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { age } from './a.toml';
2-
import b from './b.toml';
1+
import a from './a.toml';
32

4-
window.age = age;
5-
window.b = JSON.stringify(b);
3+
window.a = JSON.stringify(a);
64

7-
console.log(age, b);
5+
console.log(a);

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import type { RsbuildPlugin } from '@rsbuild/core';
22

33
export const PLUGIN_TOML_NAME = 'rsbuild:toml';
44

5-
export const pluginToml = (): RsbuildPlugin => ({
5+
export type PluginTomlOptions = {
6+
esModule?: boolean;
7+
};
8+
9+
export const pluginToml = (options: PluginTomlOptions = {}): RsbuildPlugin => ({
610
name: PLUGIN_TOML_NAME,
711

812
async setup(api) {
913
const { parse } = await import('toml');
14+
const { esModule } = options;
1015

1116
api.transform({ test: /\.toml$/ }, ({ code }) => {
1217
const parsed = parse(code);
13-
return `module.exports = ${JSON.stringify(parsed, undefined, '\t')};`;
18+
const exportType = esModule ? 'export default' : 'module.exports =';
19+
return `${exportType} ${JSON.stringify(parsed, undefined, '\t')};`;
1420
});
1521
},
1622
});

test/basic/index.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ test('should render page as expected', async ({ page }) => {
1111
cwd: __dirname,
1212
rsbuildConfig: {
1313
plugins: [pluginToml()],
14+
server: {
15+
port: 3100,
16+
},
1417
},
1518
});
1619

1720
const { server, urls } = await rsbuild.startDevServer();
1821

1922
await page.goto(urls[0]);
20-
expect(await page.evaluate('window.age')).toBe(1);
21-
expect(await page.evaluate('window.b')).toBe('{"list":[1,2]}');
23+
expect(await page.evaluate('window.a')).toBe('{"list":[1,2]}');
2224

2325
await server.close();
2426
});
@@ -28,15 +30,17 @@ test('should build succeed', async ({ page }) => {
2830
cwd: __dirname,
2931
rsbuildConfig: {
3032
plugins: [pluginToml()],
33+
server: {
34+
port: 3100,
35+
},
3136
},
3237
});
3338

3439
await rsbuild.build();
3540
const { server, urls } = await rsbuild.preview();
3641

3742
await page.goto(urls[0]);
38-
expect(await page.evaluate('window.age')).toBe(1);
39-
expect(await page.evaluate('window.b')).toBe('{"list":[1,2]}');
43+
expect(await page.evaluate('window.a')).toBe('{"list":[1,2]}');
4044

4145
await server.close();
4246
});

test/basic/src/a.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
foo = "bar"
2-
age = 1
1+
list = [1, 2]

test/basic/src/b.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/basic/src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import { age } from './a.toml';
2-
import b from './b.toml';
1+
import a from './a.toml';
32

4-
window.age = age;
5-
window.b = JSON.stringify(b);
6-
7-
console.log(age, b);
3+
window.a = JSON.stringify(a);

test/commonjs/index.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { expect, test } from '@playwright/test';
4+
import { createRsbuild } from '@rsbuild/core';
5+
import { pluginToml } from '../../src';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
9+
test('should render page as expected', async ({ page }) => {
10+
const rsbuild = await createRsbuild({
11+
cwd: __dirname,
12+
rsbuildConfig: {
13+
plugins: [
14+
pluginToml({
15+
esModule: false,
16+
}),
17+
],
18+
},
19+
});
20+
21+
const { server, urls } = await rsbuild.startDevServer();
22+
23+
await page.goto(urls[0]);
24+
expect(await page.evaluate('window.age')).toBe(1);
25+
expect(await page.evaluate('window.b')).toBe('{"list":[1,2]}');
26+
27+
await server.close();
28+
});
29+
30+
test('should build succeed', async ({ page }) => {
31+
const rsbuild = await createRsbuild({
32+
cwd: __dirname,
33+
rsbuildConfig: {
34+
plugins: [
35+
pluginToml({
36+
esModule: false,
37+
}),
38+
],
39+
},
40+
});
41+
42+
await rsbuild.build();
43+
const { server, urls } = await rsbuild.preview();
44+
45+
await page.goto(urls[0]);
46+
expect(await page.evaluate('window.age')).toBe(1);
47+
expect(await page.evaluate('window.b')).toBe('{"list":[1,2]}');
48+
49+
await server.close();
50+
});

test/commonjs/src/a.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo = "bar"
2+
age = 1

0 commit comments

Comments
 (0)