-
-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add new adapter for Rsbuild #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6aca40
feat: adapter-rsbuild
9aoy 47faca2
fix: cwd
9aoy 71a4689
fix: lint
9aoy 1eb5c8d
Update packages/adapter-rsbuild/README.md
9aoy 2c2a47b
Update website/docs/en/guide/integration/rsbuild.mdx
9aoy 140339d
chore: move API documentation
9aoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| import { pluginReact } from '@rsbuild/plugin-react'; | ||
| import { withRsbuildConfig } from '@rstest/adapter-rsbuild'; | ||
| import { defineConfig } from '@rstest/core'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [pluginReact()], | ||
| testEnvironment: 'jsdom', | ||
| extends: withRsbuildConfig({ cwd: __dirname }), | ||
| setupFiles: ['./rstest.setup.ts'], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # @rstest/adapter-rsbuild | ||
|
|
||
| Rstest adapter for [Rsbuild](https://rsbuild.rs) configuration. This package allows you to extend your Rstest configuration from Rsbuild config files. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @rstest/adapter-rsbuild -D | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { defineConfig } from '@rstest/core'; | ||
| import { withRsbuildConfig } from '@rstest/adapter-rsbuild'; | ||
|
|
||
| export default defineConfig({ | ||
| extends: withRsbuildConfig(), | ||
| // other rstest config options | ||
| }); | ||
| ``` | ||
|
|
||
| More advanced usage examples can be found in the [Rsbuild integration guide](https://rstest.rs/guide/integration/rsbuild). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { | ||
| "name": "@rstest/adapter-rsbuild", | ||
| "version": "0.1.0", | ||
| "description": "Rstest adapter for rsbuild configuration", | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/index.js", | ||
| "types": "./dist/index.d.ts" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "rslib build", | ||
| "dev": "rslib build --watch", | ||
| "test": "rstest" | ||
| }, | ||
| "keywords": [ | ||
| "rstest", | ||
| "rsbuild", | ||
| "adapter", | ||
| "configuration" | ||
| ], | ||
| "license": "MIT", | ||
| "peerDependencies": { | ||
| "@rsbuild/core": "*" | ||
| }, | ||
| "devDependencies": { | ||
| "@rslib/core": "^0.19.0", | ||
| "@rsbuild/core": "1.7.1", | ||
| "@rstest/core": "workspace:*", | ||
| "@rstest/tsconfig": "workspace:*" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/web-infra-dev/rstest/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/web-infra-dev/rstest", | ||
| "directory": "packages/adapter-rsbuild" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| { | ||
| format: 'esm', | ||
| dts: true, | ||
| bundle: true, | ||
| syntax: ['node 18.12.0'], | ||
| experiments: { | ||
| advancedEsm: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { defineConfig } from '@rstest/core'; | ||
|
|
||
| export default defineConfig({ | ||
| root: __dirname, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { | ||
| loadConfig, | ||
| mergeRsbuildConfig, | ||
| type RsbuildConfig, | ||
| } from '@rsbuild/core'; | ||
| import type { ExtendConfig, ExtendConfigFn } from '@rstest/core'; | ||
|
|
||
| export interface WithRsbuildConfigOptions { | ||
| /** | ||
| * `cwd` passed to loadConfig of Rsbuild | ||
| * @default process.cwd() | ||
| */ | ||
| cwd?: string; | ||
| /** | ||
| * Path to rsbuild config file | ||
| * @default './rsbuild.config.ts' | ||
| */ | ||
| configPath?: string; | ||
| /** | ||
| * The environment name in `environments` field to use, will be merged with the common config. | ||
| * Set to a string to use the environment config with matching name. | ||
| * @default undefined | ||
| */ | ||
| environmentName?: string; | ||
| /** | ||
| * Modify rsbuild config before converting to rstest config | ||
| */ | ||
| modifyRsbuildConfig?: (buildConfig: RsbuildConfig) => RsbuildConfig; | ||
| } | ||
|
|
||
| export function withRsbuildConfig( | ||
| options: WithRsbuildConfigOptions = {}, | ||
| ): ExtendConfigFn { | ||
| return async () => { | ||
| const { | ||
| configPath, | ||
| modifyRsbuildConfig, | ||
| environmentName, | ||
| cwd = process.cwd(), | ||
| } = options; | ||
|
|
||
| // Load rsbuild config | ||
| const { | ||
| content: { environments, ...rawBuildConfig }, | ||
| filePath, | ||
| } = await loadConfig({ | ||
| cwd, | ||
| path: configPath, | ||
| }); | ||
|
|
||
| if (!filePath) { | ||
| return {}; | ||
| } | ||
|
|
||
| const environmentConfig = environmentName | ||
| ? environments?.[environmentName] | ||
| : undefined; | ||
|
|
||
| const rsbuildConfig = environmentConfig | ||
| ? mergeRsbuildConfig<RsbuildConfig>( | ||
| rawBuildConfig as RsbuildConfig, | ||
| environmentConfig as RsbuildConfig, | ||
| ) | ||
| : (rawBuildConfig as RsbuildConfig); | ||
|
|
||
| // Allow modification of rsbuild config | ||
| const finalBuildConfig = modifyRsbuildConfig | ||
| ? modifyRsbuildConfig(rsbuildConfig) | ||
| : rsbuildConfig; | ||
|
|
||
| const { rspack, swc, bundlerChain } = finalBuildConfig.tools || {}; | ||
| const { cssModules, target, module } = finalBuildConfig.output || {}; | ||
| const { decorators, define, include, exclude, tsconfigPath } = | ||
| finalBuildConfig.source || {}; | ||
|
|
||
| // Convert rsbuild config to rstest config | ||
| const rstestConfig: ExtendConfig = { | ||
| // Copy over compatible configurations | ||
| root: finalBuildConfig.root, | ||
| name: environmentName, | ||
| plugins: finalBuildConfig.plugins, | ||
| source: { | ||
| decorators, | ||
| define, | ||
| include, | ||
| exclude, | ||
| tsconfigPath, | ||
| }, | ||
| resolve: finalBuildConfig.resolve, | ||
| output: { | ||
| cssModules, | ||
| module, | ||
| }, | ||
| tools: { | ||
| rspack, | ||
| swc, | ||
| bundlerChain, | ||
| } as ExtendConfig['tools'], | ||
|
|
||
| testEnvironment: target === 'node' ? 'node' : 'happy-dom', | ||
| }; | ||
|
|
||
| return rstestConfig; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { existsSync, unlinkSync, writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from '@rstest/core'; | ||
| import { withRsbuildConfig } from '../src'; | ||
|
|
||
| describe('withRsbuildConfig', () => { | ||
| const testConfigPath = join(__dirname, 'test-temp-rsbuild.config.ts'); | ||
| const testConfigContent = ` | ||
| import { defineConfig } from '@rsbuild/core'; | ||
|
|
||
| export default defineConfig({ | ||
| source: { | ||
| define: { | ||
| 'process.env.NODE_ENV': '"common"' | ||
| } | ||
| }, | ||
| resolve: { | ||
| alias: { | ||
| '@': './src' | ||
| } | ||
| }, | ||
| environments: { | ||
| test: { | ||
| source: { | ||
| define: { | ||
| 'process.env.NODE_ENV': '"test"' | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| `; | ||
|
|
||
| beforeEach(() => { | ||
| // Create a temporary rsbuild config file for testing | ||
| writeFileSync(testConfigPath, testConfigContent); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Clean up test config file | ||
| if (existsSync(testConfigPath)) { | ||
| unlinkSync(testConfigPath); | ||
| } | ||
| }); | ||
|
|
||
| it('should load and convert rsbuild config to rstest config', async () => { | ||
| const config = await withRsbuildConfig({ | ||
| configPath: testConfigPath, | ||
| })({}); | ||
|
|
||
| expect(config).toBeDefined(); | ||
| expect(config.source?.define).toEqual({ | ||
| 'process.env.NODE_ENV': '"common"', | ||
| }); | ||
| expect(config.resolve?.alias).toEqual({ | ||
| '@': './src', | ||
| }); | ||
| expect(config.testEnvironment).toBe('happy-dom'); | ||
| }); | ||
|
|
||
| it('should load and merge environment config', async () => { | ||
| const config = await withRsbuildConfig({ | ||
| configPath: testConfigPath, | ||
| environmentName: 'test', | ||
| })({}); | ||
|
|
||
| expect(config).toBeDefined(); | ||
| expect(config.name).toBe('test'); | ||
| expect(config.source?.define).toEqual({ | ||
| 'process.env.NODE_ENV': '"test"', | ||
| }); | ||
| expect(config.resolve?.alias).toEqual({ | ||
| '@': './src', | ||
| }); | ||
| }); | ||
|
|
||
| it('should allow modification of rsbuild config', async () => { | ||
| const config = await withRsbuildConfig({ | ||
| configPath: testConfigPath, | ||
| modifyRsbuildConfig: (rsbuildConfig) => ({ | ||
| ...rsbuildConfig, | ||
| source: { | ||
| ...rsbuildConfig.source, | ||
| define: { | ||
| ...rsbuildConfig.source?.define, | ||
| 'process.env.CUSTOM': '"custom-value"', | ||
| }, | ||
| }, | ||
| }), | ||
| })({}); | ||
|
|
||
| expect(config.source?.define).toEqual({ | ||
| 'process.env.NODE_ENV': '"common"', | ||
| 'process.env.CUSTOM': '"custom-value"', | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw error when config file not found', async () => { | ||
| await expect(() => | ||
| withRsbuildConfig({ | ||
| configPath: './non-existent.config.ts', | ||
| })({}), | ||
| ).rejects.toThrowError(/Cannot find config file/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "extends": "@rstest/tsconfig/base", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "baseUrl": "./", | ||
| "rootDir": "src", | ||
| "composite": true, | ||
| "isolatedDeclarations": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ["adapters", "rslib"] | ||
| ["adapters", "rslib", "rsbuild"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.