Skip to content

Commit 0cf459b

Browse files
authored
fix: rsbuild environments problem. (#795)
1 parent e808849 commit 0cf459b

File tree

29 files changed

+734
-18
lines changed

29 files changed

+734
-18
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('a');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const ll = 1;
2+
3+
console.log('a');
4+
5+
function kk() {
6+
console.log('hhe: ', ll);
7+
return '111';
8+
}
9+
10+
kk();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @template {{
3+
* mode: 'async' | 'callback' | 'sync';
4+
* pitchResult?: string;
5+
* }} Options
6+
*/
7+
8+
/**
9+
* @type {import("webpack").LoaderDefinitionFunction<Options, {}>}
10+
*/
11+
module.exports = function (input) {
12+
/**
13+
* @type Options
14+
*/
15+
const options = this.getOptions();
16+
const res = [input, '// hello world'].join('\n');
17+
18+
if (options.mode === 'async') {
19+
const cb = this.async();
20+
setTimeout(() => {
21+
cb(null, res);
22+
}, 3000);
23+
} else if (options.mode === 'callback') {
24+
this.callback(null, res);
25+
} else {
26+
return res;
27+
}
28+
};
29+
30+
module.exports.pitch = function () {
31+
/**
32+
* @type Options
33+
*/
34+
const options = this.getOptions();
35+
36+
if (options.pitchResult) {
37+
return options.pitchResult;
38+
}
39+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { parseQuery } = require('loader-utils');
2+
3+
/**
4+
* @type {import("webpack").LoaderDefinitionFunction<{}, {}>}
5+
*/
6+
module.exports = function (input) {
7+
const res = [input, `// ${JSON.stringify(this.query)}`];
8+
9+
// Based on https://github.com/windicss/windicss-webpack-plugin/blob/main/src/loaders/windicss-template.ts#L42
10+
// test the loader query
11+
if (this.query !== '') {
12+
res.push(`// ${JSON.stringify(parseQuery(this.query))}`);
13+
}
14+
15+
return res.join('\n');
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { parseQuery } = require('loader-utils');
2+
3+
/**
4+
* @type {import("webpack").LoaderDefinitionFunction<{}, {}>}
5+
*/
6+
module.exports = function (input) {
7+
const res = [input, `// ${JSON.stringify(this.resourceQuery)}`];
8+
9+
// Based on https://github.com/windicss/windicss-webpack-plugin/blob/main/src/loaders/windicss-template.ts#L42
10+
// test the loader query
11+
if (this.resourceQuery !== '') {
12+
res.push(`// ${JSON.stringify(parseQuery(this.resourceQuery))}`);
13+
}
14+
15+
return res.join('\n');
16+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { RsdoctorWebpackPlugin } = require('../../dist/index');
2+
3+
const plugin = new RsdoctorWebpackPlugin({});
4+
5+
plugin.sdk.bootstrap().then(() => {
6+
console.log(plugin.sdk.server.port);
7+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expect, test } from '@playwright/test';
2+
import { getSDK } from '@rsdoctor/core/plugins';
3+
import { RsdoctorRspackPlugin } from '@rsdoctor/rspack-plugin';
4+
import { createStubRsbuild } from '@scripts/test-helper';
5+
import path from 'path';
6+
7+
const file = path.resolve(__dirname, '../fixtures/a.js');
8+
9+
async function rsbuild(_query?: string) {
10+
const res = await createStubRsbuild({
11+
rsbuildConfig: {
12+
source: {
13+
entry: {
14+
index: path.join(__dirname, '../fixtures/a.js'),
15+
},
16+
},
17+
environments: {
18+
web: {},
19+
web1: {},
20+
},
21+
tools: {
22+
rspack(config: any, { appendPlugins, environment }: any) {
23+
if (environment.name === 'node') {
24+
appendPlugins(
25+
new RsdoctorRspackPlugin({
26+
disableClientServer: true,
27+
output: {
28+
reportDir: path.join(__dirname, './doc_build/node/'),
29+
},
30+
}),
31+
);
32+
} else {
33+
appendPlugins(
34+
new RsdoctorRspackPlugin({
35+
disableClientServer: true,
36+
}),
37+
);
38+
}
39+
},
40+
},
41+
},
42+
});
43+
return res;
44+
}
45+
46+
test('rsbuild environments tests', async () => {
47+
const rsdbuildInstance = await rsbuild();
48+
await rsdbuildInstance.build();
49+
const sdk = getSDK('web');
50+
expect(sdk.name).toBe('web');
51+
const sdk1 = getSDK('web1');
52+
expect(sdk1.name).toBe('web1');
53+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { RsdoctorWebpackPluginOptions } from '@rsdoctor/core/types';
2+
import {
3+
RsdoctorWebpackPlugin,
4+
RsdoctorWebpackMultiplePlugin,
5+
} from '@rsdoctor/webpack-plugin';
6+
import { Linter } from '@rsdoctor/types';
7+
import { File } from '@rsdoctor/utils/build';
8+
import { tmpdir } from 'os';
9+
import path from 'path';
10+
11+
export function createRsdoctorPlugin<T extends Linter.ExtendRuleData[]>(
12+
options: RsdoctorWebpackPluginOptions<T> = {},
13+
) {
14+
const plugin = new RsdoctorWebpackPlugin({
15+
...options,
16+
disableClientServer:
17+
typeof options.disableClientServer === 'boolean'
18+
? options.disableClientServer
19+
: true,
20+
});
21+
22+
const outdir = path.resolve(
23+
tmpdir(),
24+
`./${Date.now()}/rsdoctor_webpack_plugin_test`,
25+
);
26+
27+
plugin.sdk.hooks.afterSaveManifest.tapPromise(
28+
{ name: 'REMOVE_TMP_DIR', stage: -9999 },
29+
async () => {
30+
plugin.sdk.setOutputDir(outdir);
31+
try {
32+
await File.fse.remove(plugin.sdk.outputDir);
33+
} catch (e) {
34+
console.error(e);
35+
}
36+
},
37+
);
38+
39+
return plugin;
40+
}
41+
42+
export function createRsdoctorMultiPlugin<T extends Linter.ExtendRuleData[]>(
43+
options: RsdoctorWebpackPluginOptions<T> = {},
44+
) {
45+
const plugin = new RsdoctorWebpackMultiplePlugin({
46+
...options,
47+
disableClientServer:
48+
typeof options.disableClientServer === 'boolean'
49+
? options.disableClientServer
50+
: true,
51+
});
52+
53+
const outdir = path.resolve(
54+
tmpdir(),
55+
`./${Date.now()}/rsdoctor_webpack_plugin_test`,
56+
);
57+
58+
plugin.sdk.hooks.afterSaveManifest.tapPromise(
59+
{ name: 'REMOVE_TMP_DIR', stage: -9999 },
60+
async () => {
61+
plugin.sdk.setOutputDir(outdir);
62+
try {
63+
await File.fse.remove(plugin.sdk.outputDir);
64+
} catch (e) {
65+
console.error(e);
66+
}
67+
},
68+
);
69+
70+
return plugin;
71+
}

e2e/cases/doctor-webpack/test-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function createRsdoctorPlugin<T extends Linter.ExtendRuleData[]>(
2121

2222
const outdir = path.resolve(
2323
tmpdir(),
24-
`./${Date.now()}/web_doctor_webpack_plugin_test`,
24+
`./${Date.now()}/rsdoctor_webpack_plugin_test`,
2525
);
2626

2727
plugin.sdk.hooks.afterSaveManifest.tapPromise(
@@ -52,7 +52,7 @@ export function createRsdoctorMultiPlugin<T extends Linter.ExtendRuleData[]>(
5252

5353
const outdir = path.resolve(
5454
tmpdir(),
55-
`./${Date.now()}/web_doctor_webpack_plugin_test`,
55+
`./${Date.now()}/rsdoctor_webpack_plugin_test`,
5656
);
5757

5858
plugin.sdk.hooks.afterSaveManifest.tapPromise(

examples/rspress-minimal/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Local
2+
.DS_Store
3+
*.local
4+
*.log*
5+
6+
# Dist
7+
node_modules
8+
dist/
9+
doc_build/
10+
11+
# IDE
12+
.vscode/*
13+
!.vscode/extensions.json
14+
.idea

examples/rspress-minimal/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Rspress website
2+
3+
## Setup
4+
5+
Install the dependencies:
6+
7+
```bash
8+
npm install
9+
```
10+
11+
## Get started
12+
13+
Start the dev server:
14+
15+
```bash
16+
npm run dev
17+
```
18+
19+
Build the website for production:
20+
21+
```bash
22+
npm run build
23+
```
24+
25+
Preview the production build locally:
26+
27+
```bash
28+
npm run preview
29+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"text": "Guide",
4+
"link": "/guide/",
5+
"activeMatch": "/guide/"
6+
},
7+
{
8+
"text": "Hello world",
9+
"link": "/hello/",
10+
"activeMatch": "/hello/"
11+
},
12+
{
13+
"text": "API",
14+
"link": "https://rspress.dev/api/index.html"
15+
}
16+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["index"]

0 commit comments

Comments
 (0)