@rstest/adapter-rspack 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @rstest/adapter-rspack
2
+
3
+ Rstest adapter for [Rspack](https://rspack.rs) configuration. This package allows you to extend your Rstest configuration from Rspack config files.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @rstest/adapter-rspack -D
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### `withRspackConfig`
14
+
15
+ ```ts
16
+ import { defineConfig } from '@rstest/core';
17
+ import { withRspackConfig } from '@rstest/adapter-rspack';
18
+
19
+ export default defineConfig({
20
+ extends: withRspackConfig(),
21
+ // other rstest config options
22
+ });
23
+ ```
24
+
25
+ Automatically loads Rspack config from the current working directory and converts it to Rstest config.
@@ -0,0 +1,38 @@
1
+ import type { RspackOptions } from '@rspack/core';
2
+ import type { ExtendConfig, ExtendConfigFn } from '@rstest/core';
3
+ type RspackConfigName = string;
4
+ export interface WithRspackConfigOptions {
5
+ /**
6
+ * Working directory used to resolve config path.
7
+ * @default process.cwd()
8
+ */
9
+ cwd?: string;
10
+ /**
11
+ * Path to rspack config file.
12
+ * @default './rspack.config.ts'
13
+ */
14
+ configPath?: string;
15
+ /**
16
+ * Select named config when using multiple Rspack configs.
17
+ */
18
+ configName?: RspackConfigName;
19
+ /**
20
+ * Environment values passed to Rspack config function.
21
+ */
22
+ env?: Record<string, unknown> | string[];
23
+ /**
24
+ * `NODE_ENV` value used when loading config.
25
+ */
26
+ nodeEnv?: string;
27
+ /**
28
+ * Modify Rspack config before converting to rstest config.
29
+ */
30
+ modifyRspackConfig?: (config: RspackOptions) => RspackOptions;
31
+ }
32
+ export declare function toRstestConfig({ rspackConfig, configName, modifyRspackConfig, }: {
33
+ rspackConfig: RspackOptions;
34
+ configName?: RspackConfigName;
35
+ modifyRspackConfig?: (config: RspackOptions) => RspackOptions;
36
+ }): ExtendConfig;
37
+ export declare function withRspackConfig(options?: WithRspackConfigOptions): ExtendConfigFn;
38
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ import { existsSync } from "node:fs";
2
+ import node_path from "node:path";
3
+ import { RspackCLI } from "@rspack/cli";
4
+ const DEFAULT_CONFIG_BASENAME = 'rspack.config';
5
+ const DEFAULT_EXTENSIONS = [
6
+ '.js',
7
+ '.ts',
8
+ '.mjs',
9
+ '.mts',
10
+ '.cjs',
11
+ '.cts'
12
+ ];
13
+ const findDefaultConfig = (cwd)=>{
14
+ const basePath = node_path.resolve(cwd, DEFAULT_CONFIG_BASENAME);
15
+ for (const ext of DEFAULT_EXTENSIONS){
16
+ const candidate = `${basePath}${ext}`;
17
+ if (existsSync(candidate)) return candidate;
18
+ }
19
+ return null;
20
+ };
21
+ const normalizeTargets = (target)=>{
22
+ if (!target) return [];
23
+ if (Array.isArray(target)) return target.filter(Boolean);
24
+ if ('string' == typeof target) return [
25
+ target
26
+ ];
27
+ return [];
28
+ };
29
+ const isNodeTarget = (targets)=>targets.some((target)=>'async-node' === target || target.startsWith('node'));
30
+ const isHtmlRspackPlugin = (plugin)=>plugin && plugin.constructor ? 'HtmlRspackPlugin' === plugin.constructor.name : false;
31
+ const buildRspackToolConfig = (rspackConfig)=>{
32
+ const { lazyCompilation: _lazy, ...restConfig } = rspackConfig;
33
+ return (config)=>{
34
+ const nextConfig = {
35
+ ...config
36
+ };
37
+ if (restConfig.module) {
38
+ const baseModule = config.module || {};
39
+ const merged = {
40
+ ...baseModule,
41
+ ...restConfig.module
42
+ };
43
+ if (baseModule.rules || restConfig.module.rules) merged.rules = [
44
+ ...baseModule.rules || [],
45
+ ...restConfig.module.rules || []
46
+ ];
47
+ nextConfig.module = merged;
48
+ }
49
+ if (restConfig.plugins?.length) {
50
+ const plugins = restConfig.plugins.filter((plugin)=>plugin && !isHtmlRspackPlugin(plugin));
51
+ nextConfig.plugins = [
52
+ ...config.plugins || [],
53
+ ...plugins
54
+ ];
55
+ }
56
+ if (restConfig.experiments) nextConfig.experiments = {
57
+ ...config.experiments || {},
58
+ ...restConfig.experiments
59
+ };
60
+ if (restConfig.optimization) nextConfig.optimization = {
61
+ ...config.optimization || {},
62
+ ...restConfig.optimization
63
+ };
64
+ if (restConfig.output) {
65
+ const baseOutput = config.output || {};
66
+ const mergedOutput = {
67
+ ...baseOutput,
68
+ ...restConfig.output
69
+ };
70
+ if (baseOutput.path) mergedOutput.path = baseOutput.path;
71
+ nextConfig.output = mergedOutput;
72
+ }
73
+ if (void 0 !== restConfig.devtool) nextConfig.devtool = restConfig.devtool;
74
+ if (restConfig.watchOptions) nextConfig.watchOptions = {
75
+ ...config.watchOptions || {},
76
+ ...restConfig.watchOptions
77
+ };
78
+ if (void 0 !== restConfig.snapshot) nextConfig.snapshot = restConfig.snapshot;
79
+ if (restConfig.externalsPresets) nextConfig.externalsPresets = {
80
+ ...config.externalsPresets || {},
81
+ ...restConfig.externalsPresets
82
+ };
83
+ if (void 0 !== restConfig.cache) nextConfig.cache = restConfig.cache;
84
+ return nextConfig;
85
+ };
86
+ };
87
+ const selectRspackConfig = (config, configName)=>{
88
+ const configs = Array.isArray(config) ? config : [
89
+ config
90
+ ];
91
+ if (0 === configs.length) throw new Error('No Rspack configuration found.');
92
+ if (!configName) return configs[0] || {};
93
+ const found = configs.find((item)=>item?.name === configName);
94
+ if (!found) throw new Error(`Configuration with the name "${configName}" was not found.`);
95
+ return found;
96
+ };
97
+ const resolveConfigPath = (cwd, configPath)=>{
98
+ if (configPath) return node_path.isAbsolute(configPath) ? configPath : node_path.resolve(cwd, configPath);
99
+ return findDefaultConfig(cwd);
100
+ };
101
+ const loadRspackConfig = async (options)=>{
102
+ const cwd = options.cwd ?? process.cwd();
103
+ const configFile = resolveConfigPath(cwd, options.configPath);
104
+ if (!configFile || !existsSync(configFile)) return null;
105
+ const cli = new RspackCLI();
106
+ const { config } = await cli.loadConfig({
107
+ config: configFile,
108
+ env: options.env,
109
+ nodeEnv: options.nodeEnv
110
+ });
111
+ return config;
112
+ };
113
+ function toRstestConfig({ rspackConfig, configName, modifyRspackConfig }) {
114
+ const finalConfig = modifyRspackConfig ? modifyRspackConfig(rspackConfig) : rspackConfig;
115
+ const targets = normalizeTargets(finalConfig.target);
116
+ const testEnvironment = isNodeTarget(targets) ? 'node' : 'happy-dom';
117
+ const resolve = finalConfig.resolve;
118
+ const outputModule = finalConfig.output?.module;
119
+ const output = void 0 !== outputModule ? {
120
+ module: outputModule
121
+ } : void 0;
122
+ const tsConfig = finalConfig.resolve?.tsConfig;
123
+ const tsconfigPath = 'string' == typeof tsConfig ? tsConfig : tsConfig?.configFile;
124
+ const source = tsconfigPath ? {
125
+ tsconfigPath
126
+ } : void 0;
127
+ return {
128
+ name: configName ?? finalConfig.name,
129
+ ...output ? {
130
+ output
131
+ } : {},
132
+ ...source ? {
133
+ source
134
+ } : {},
135
+ ...resolve ? {
136
+ resolve
137
+ } : {},
138
+ plugins: [
139
+ {
140
+ name: 'remove-rsbuild-css',
141
+ remove: [
142
+ 'rsbuild:css',
143
+ 'rsbuild:less',
144
+ 'rsbuild:sass'
145
+ ],
146
+ setup: ()=>{}
147
+ }
148
+ ],
149
+ tools: {
150
+ rspack: buildRspackToolConfig(finalConfig)
151
+ },
152
+ testEnvironment
153
+ };
154
+ }
155
+ function withRspackConfig(options = {}) {
156
+ return async ()=>{
157
+ const config = await loadRspackConfig(options);
158
+ if (!config) return {};
159
+ const selectedConfig = selectRspackConfig(config, options.configName);
160
+ return toRstestConfig({
161
+ rspackConfig: selectedConfig,
162
+ configName: options.configName,
163
+ modifyRspackConfig: options.modifyRspackConfig
164
+ });
165
+ };
166
+ }
167
+ export { toRstestConfig, withRspackConfig };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@rstest/adapter-rspack",
3
+ "version": "0.2.0",
4
+ "description": "Rstest adapter for rspack configuration",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "rslib build",
19
+ "dev": "rslib build --watch",
20
+ "test": "rstest",
21
+ "typecheck": "tsc --noEmit"
22
+ },
23
+ "keywords": [
24
+ "rstest",
25
+ "rspack",
26
+ "adapter",
27
+ "configuration"
28
+ ],
29
+ "license": "MIT",
30
+ "peerDependencies": {
31
+ "@rspack/cli": ">=2.0.0-0",
32
+ "@rspack/core": ">=2.0.0-0",
33
+ "@rstest/core": ">=0.7.7"
34
+ },
35
+ "devDependencies": {
36
+ "@rspack/cli": "2.0.0-beta.6",
37
+ "@rspack/core": "2.0.0-beta.6",
38
+ "@rslib/core": "0.20.0",
39
+ "@rstest/core": "workspace:*",
40
+ "@rstest/tsconfig": "workspace:*"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/web-infra-dev/rstest/issues"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "https://github.com/web-infra-dev/rstest",
51
+ "directory": "packages/adapter-rspack"
52
+ }
53
+ }