Skip to content

Commit b93afa3

Browse files
committed
fix: use modern-module to output tree-shakable artifact
1 parent 09c5549 commit b93afa3

File tree

10 files changed

+141
-110
lines changed

10 files changed

+141
-110
lines changed

CONTRIBUTING.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Rslib Contributing Guide
2+
3+
## Setup the Environment
4+
5+
TODO
6+
7+
## Caveats
8+
9+
The project is still under development, so it possible dependents on unstable Rsbuild / Rspack versions.
10+
11+
Current unstable versions are:
12+
13+
| Package | Version | Link |
14+
| ------------ | ----------------------------------- | ------------------------------------------------------- |
15+
| @rspack/core | 0.7.5-canary-b89dbb3-20240620182932 | [PR](https://github.com/web-infra-dev/rspack/pull/6877) |

e2e/cases/define/index.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type RslibConfig, build } from '@rslib/core';
33
import { expect, test } from 'vitest';
44
import { globContentJSON } from '#helper';
55

6-
test.fails('define', async () => {
6+
test('define', async () => {
77
delete process.env.NODE_ENV;
88

99
const rslibConfig: RslibConfig = {
@@ -29,6 +29,9 @@ test.fails('define', async () => {
2929
entry: {
3030
main: join(__dirname, './js/src/index.js'),
3131
},
32+
define: {
33+
VERSION: JSON.stringify('1.0.0'),
34+
},
3235
},
3336
};
3437

e2e/cases/define/js/src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
import { a } from './value';
2+
3+
export { a };
4+
15
console.info(VERSION);

e2e/cases/define/js/src/value.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// TODO: Rspack only can handle exports from concatenated modules now.
2+
// Adding this file to let the compile pass as the moment.
3+
// Remove this file when Rspack could export from single module.`
4+
export const a = 1;

examples/basic/src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const hello: string = 'Hello';
2+
export const world: string = 'World';

examples/basic/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const message: string = 'Hello, Rslib';
2-
3-
console.log(message);
1+
import { hello, world } from './constants';
2+
const message: string = `${hello}, ${world}!`;
3+
export { hello, world, message as default };

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
1313
"test:artifact": "vitest run --project artifact",
1414
"test:e2e": "cd e2e && pnpm run test",
15-
"test:unit": "vitest run --project unit"
15+
"test:unit": "vitest run --project unit",
16+
"watch": "pnpm build --watch"
1617
},
1718
"simple-git-hooks": {
1819
"pre-commit": "npx nano-staged"
@@ -42,5 +43,10 @@
4243
"engines": {
4344
"node": ">=18.0.0",
4445
"pnpm": ">=9.0.0"
46+
},
47+
"pnpm": {
48+
"overrides": {
49+
"@rspack/core": "0.7.5-canary-b89dbb3-20240620182932"
50+
}
4551
}
4652
}

packages/core/src/config.ts

+44-37
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,10 @@ export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
7575
return defineRsbuildConfig({
7676
tools: {
7777
htmlPlugin: false,
78-
rspack: {
79-
output: {
80-
module: true,
81-
library: {
82-
type: 'module',
83-
},
84-
},
85-
optimization: {
86-
concatenateModules: true,
87-
},
88-
experiments: {
89-
outputModule: true,
90-
},
91-
},
9278
},
9379
output: {
9480
filenameHash: false,
81+
// TODO: easy to development at the moment
9582
minify: false,
9683
distPath: {
9784
js: './',
@@ -104,36 +91,54 @@ export function convertLibConfigToRsbuildConfig(
10491
libConfig: LibConfig,
10592
rsbuildConfig: RsbuildConfig,
10693
): RsbuildConfig {
107-
// TODO: Configuration mapping needs to be implemented according to features added in the future
108-
if (libConfig.format === 'cjs') {
109-
mergeRsbuildConfig(rsbuildConfig, {
110-
tools: {
111-
rspack: {
112-
output: {
113-
library: {
114-
type: 'commonjs',
94+
switch (libConfig.format) {
95+
case 'esm':
96+
return mergeRsbuildConfig(rsbuildConfig, {
97+
tools: {
98+
rspack: {
99+
output: {
100+
module: true,
101+
iife: false,
102+
library: {
103+
type: 'modern-module',
104+
},
105+
},
106+
optimization: {
107+
concatenateModules: true,
108+
},
109+
experiments: {
110+
outputModule: true,
115111
},
116112
},
117113
},
118-
},
119-
});
120-
}
121-
122-
if (libConfig.format === 'esm') {
123-
mergeRsbuildConfig(rsbuildConfig, {
124-
tools: {
125-
rspack: {
126-
output: {
127-
library: {
128-
type: 'module',
114+
});
115+
case 'cjs':
116+
return mergeRsbuildConfig(rsbuildConfig, {
117+
tools: {
118+
rspack: {
119+
output: {
120+
library: {
121+
type: 'commonjs',
122+
},
129123
},
130124
},
131125
},
132-
},
133-
});
126+
});
127+
case 'umd':
128+
return mergeRsbuildConfig(rsbuildConfig, {
129+
tools: {
130+
rspack: {
131+
output: {
132+
library: {
133+
type: 'umd',
134+
},
135+
},
136+
},
137+
},
138+
});
139+
default:
140+
return rsbuildConfig;
134141
}
135-
136-
return rsbuildConfig;
137142
}
138143

139144
export async function composeCreateRsbuildConfig(
@@ -151,6 +156,8 @@ export async function composeCreateRsbuildConfig(
151156
const composedRsbuildConfig = libConfigsArray.map((libConfig: LibConfig) => {
152157
const { format, ...overrideRsbuildConfig } = libConfig;
153158

159+
// Merge order matters, keep `internalRsbuildConfig` at the last position
160+
// to ensure that the internal config is not overridden by the user's config.
154161
const mergedRsbuildConfig = mergeRsbuildConfig(
155162
sharedRsbuildConfig,
156163
overrideRsbuildConfig,

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

+4-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
2727
"concatenateModules": true,
2828
},
2929
"output": {
30+
"iife": false,
3031
"library": {
31-
"type": "module",
32+
"type": "modern-module",
3233
},
3334
"module": true,
3435
},
@@ -57,17 +58,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
5758
"tools": {
5859
"htmlPlugin": false,
5960
"rspack": {
60-
"experiments": {
61-
"outputModule": true,
62-
},
63-
"optimization": {
64-
"concatenateModules": true,
65-
},
6661
"output": {
6762
"library": {
68-
"type": "module",
63+
"type": "commonjs",
6964
},
70-
"module": true,
7165
},
7266
},
7367
},
@@ -90,17 +84,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
9084
"tools": {
9185
"htmlPlugin": false,
9286
"rspack": {
93-
"experiments": {
94-
"outputModule": true,
95-
},
96-
"optimization": {
97-
"concatenateModules": true,
98-
},
9987
"output": {
10088
"library": {
101-
"type": "module",
89+
"type": "umd",
10290
},
103-
"module": true,
10491
},
10592
},
10693
},

0 commit comments

Comments
 (0)