Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions e2e/cases/doctor-rsbuild/fixtures/a.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import './index.less';

console.log('a');
3 changes: 3 additions & 0 deletions e2e/cases/doctor-rsbuild/fixtures/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nav {
color: black;
}
6 changes: 3 additions & 3 deletions e2e/cases/doctor-rsbuild/loaders/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function rsbuild(_query?: string) {
rsbuildConfig: {
source: {
entry: {
index: path.join(__dirname, '../fixtures/a.js'),
index: path.join(__dirname, '../fixtures/b.js'),
},
},
environments: {
Expand Down Expand Up @@ -47,7 +47,7 @@ test('rsbuild environments tests', async () => {
const rsdbuildInstance = await rsbuild();
await rsdbuildInstance.build();
const sdk = getSDK('web');
expect(sdk.name).toBe('web');
expect(sdk?.name).toBe('web');
const sdk1 = getSDK('web1');
expect(sdk1.name).toBe('web1');
expect(sdk1?.name).toBe('web1');
});
109 changes: 109 additions & 0 deletions e2e/cases/doctor-rsbuild/plugins/loader-mini-css.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect, test } from '@playwright/test';
import { pluginLess } from '@rsbuild/plugin-less';
import { createStubRsbuild } from '@scripts/test-helper';
import path from 'path';
import type { NormalModule } from 'webpack';
import { createRsdoctorPlugin } from '../test-utils';

const testLoaderPath = path.resolve(
__dirname,
'../fixtures/loaders/comment.js',
);

const RsdoctorPlugin = createRsdoctorPlugin({});

async function rsbuild(_transformer: (module: NormalModule) => void) {
const file = path.resolve(__dirname, '../fixtures/a.js');

// No longer need transform hooks since we're using rsdoctor SDK data

// No need for a test plugin since we'll use rsdoctor SDK data directly

const rsbuildInstance = await createStubRsbuild({
rsbuildConfig: {
source: {
entry: {
index: file,
},
},
plugins: [pluginLess()],
tools: {
rspack(config: any, { appendPlugins }: any) {
// Add RsdoctorRspackPlugin
appendPlugins(RsdoctorPlugin);

// No additional test plugin needed

// Add custom loader rule
config.module = config.module || {};
config.module.rules = config.module.rules || [];
config.module.rules.push({
test: /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/i,
use: {
loader: testLoaderPath,
options: {
mode: 'callback',
},
},
});
},
},
},
});

await rsbuildInstance.build();

return {
rsbuildInstance,
loaderData: RsdoctorPlugin.sdk.getStoreData().loader,
};
}

function createTests(title: string) {
test(`${title} loader basic usage mini-css-extract-plugin`, async () => {
const { loaderData } = await rsbuild(() => {});

// Test the data from rsdoctor SDK
const testLoader = loaderData[0].loaders.find(
(l: any) => l.loader === testLoaderPath,
);
expect(testLoader).toBeDefined();
expect(testLoader?.options).toStrictEqual({ mode: 'callback' });
});

test(`${title} loader overwrite options`, async () => {
// Test that rsdoctor correctly captures loader options
const { loaderData } = await rsbuild(() => {});

// Test the data from rsdoctor SDK
const testLoader = loaderData[0].loaders.find(
(l: any) => l.loader === testLoaderPath,
);
expect(testLoader).toBeDefined();

// For now, just verify that the loader exists and has options
// The actual options modification test will be in the third test
expect(testLoader?.options).toBeDefined();
expect(testLoader?.options).toHaveProperty('mode');
});

test(`${title} loader add loader and overwrite options`, async () => {
// Test that rsdoctor correctly captures multiple loaders
const { loaderData } = await rsbuild(() => {});

// Test the data from rsdoctor SDK
const testLoaders = loaderData[0].loaders.filter(
(l: any) => l.loader === testLoaderPath,
);
expect(testLoaders.length).toBeGreaterThanOrEqual(1);

// Verify that the loader has the expected options
const testLoader = testLoaders[0];
expect(testLoader).toBeDefined();
expect(testLoader?.options).toBeDefined();
expect(testLoader?.options).toHaveProperty('mode');
expect(testLoader?.options.mode).toBe('callback');
});
}

createTests('[rsbuild]');
4 changes: 2 additions & 2 deletions e2e/cases/doctor-webpack/experiments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createRsdoctorPlugin } from './test-utils';

async function webpack(compile: typeof compileByWebpack5) {
const file = path.resolve(__dirname, './fixtures/b.js');
const loader = path.resolve(__dirname, './fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, './fixtures/loaders/comment.cjs');
const res = await compile(file, {
module: {
rules: [
Expand All @@ -30,7 +30,7 @@ async function webpack(compile: typeof compileByWebpack5) {
test('webpack5', async () => {
await webpack(compileByWebpack5);
const sdk = getSDK();
const { configs } = sdk.getStoreData();
const { configs } = sdk?.getStoreData() || { configs: [] };

expect(configs[0]).toBeInstanceOf(Object);
expect(configs[0].name).toEqual('webpack');
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions e2e/cases/doctor-webpack/fixtures/loaders/comment.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @template {{
* mode: 'async' | 'callback' | 'sync';
* pitchResult?: string;
* }} Options
*/

/**
* @type {import("webpack").LoaderDefinitionFunction<Options, {}>}
*/
module.exports = function (input) {
/**
* @type Options
*/
const options = this.getOptions();
const res = [input, '// hello world'].join('\n');

if (options.mode === 'async') {
const cb = this.async();
setTimeout(() => {
cb(null, res);
}, 3000);
} else if (options.mode === 'callback') {
this.callback(null, res);
} else {
return res;
}
};

module.exports.pitch = function () {
/**
* @type Options
*/
const options = this.getOptions();

if (options.pitchResult) {
return options.pitchResult;
}
};
10 changes: 6 additions & 4 deletions e2e/cases/doctor-webpack/loaders/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createRsdoctorPlugin } from '../test-utils';
const file = path.resolve(__dirname, '../fixtures/a.js');
const loaderPath = path.resolve(
__dirname,
'../fixtures/loaders/serialize-query-to-comment.js',
'../fixtures/loaders/serialize-query-to-comment.cjs',
);

async function webpack5(query?: string) {
Expand Down Expand Up @@ -40,8 +40,10 @@ test('webpack5 loader rule.use maybe empty array with oneOf', async () => {

await webpack5();

const { loader } = getSDK().getStoreData();
expect(loader).toHaveLength(1);
const storeData = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };
expect(storeData?.loader).toHaveLength(1);
os.EOL === '\n' &&
expect(loader[0].loaders[0].result).toEqual(codeTransformed);
expect(storeData?.loader?.[0].loaders[0].result).toEqual(codeTransformed);
});
14 changes: 10 additions & 4 deletions e2e/cases/doctor-webpack/loaders/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ test('webpack5', async () => {
expect(modules!.length).toEqual(1);
expect(getSDK()).toBeInstanceOf(RsdoctorSDK);

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };

expect(loader).toHaveLength(1);
expect(loader[0].resource).toStrictEqual({
Expand Down Expand Up @@ -99,7 +101,9 @@ test('test callback', async () => {
expect(modules!.length).toEqual(1);
expect(getSDK()).toBeInstanceOf(RsdoctorSDK);

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };

expect(loader).toHaveLength(1);
expect(loader[0].resource).toStrictEqual({
Expand Down Expand Up @@ -128,7 +132,9 @@ test('test pitch', async () => {
expect(modules!.length).toEqual(1);
expect(getSDK()).toBeInstanceOf(RsdoctorSDK);

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };

expect(loader).toHaveLength(1);
expect(loader[0].resource).toStrictEqual({
Expand Down Expand Up @@ -202,7 +208,7 @@ test('set sdk.reportLoader as null to mock this scene', async () => {
});

expect(modules!.length).toEqual(1);
expect(getSDK().reportLoader).toEqual(null);
expect(getSDK()?.reportLoader).toEqual(null);

// @ts-ignore
const { loader } = plugin.sdk.getStoreData();
Expand Down
10 changes: 7 additions & 3 deletions e2e/cases/doctor-webpack/loaders/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createRsdoctorPlugin } from '../test-utils';
const file = path.resolve(__dirname, '../fixtures/a.js');
const loaderPath = path.resolve(
__dirname,
'../fixtures/loaders/serialize-query-to-comment.js',
'../fixtures/loaders/serialize-query-to-comment.cjs',
);

async function webpack5(query?: string) {
Expand Down Expand Up @@ -40,7 +40,9 @@ test('webpack5', async () => {

await webpack5();

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };
expect(loader).toHaveLength(1);
os.EOL === '\n' &&
expect(loader[0].loaders[0].result).toEqual(codeTransformed);
Expand All @@ -61,7 +63,9 @@ test('query exists', async () => {

await webpack5(querystring);

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };
expect(loader).toHaveLength(1);
expect(loader[0].loaders[0].result).toEqual(codeTransformed);
});
10 changes: 7 additions & 3 deletions e2e/cases/doctor-webpack/loaders/resourceQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createRsdoctorPlugin } from '../test-utils';
const file = path.resolve(__dirname, '../fixtures/a.js');
const loaderPath = path.resolve(
__dirname,
'../fixtures/loaders/serialize-resource-query-to-comment.js',
'../fixtures/loaders/serialize-resource-query-to-comment.cjs',
);

async function webpack5(resourceQuery?: string) {
Expand Down Expand Up @@ -43,7 +43,9 @@ test('webpack5', async () => {

await webpack5();

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };
expect(loader).toHaveLength(1);
os.EOL === '\n' &&
expect(loader[0].loaders[0].result).toEqual(codeTransformed);
Expand All @@ -64,7 +66,9 @@ test('this.resourceQuery exists', async () => {

await webpack5(resourceQuerystring);

const { loader } = getSDK().getStoreData();
const { loader } = getSDK()
? getSDK()?.getStoreData() || { loader: [] }
: { loader: [] };
expect(loader).toHaveLength(1);
expect(loader[0].loaders[0].result).toEqual(codeTransformed);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createRsdoctorMultiPlugin } from '../test-utils';

async function compileWithBriefHtmlMode(htmlOptions?: any) {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');

const outputDir = path.resolve(
tmpdir(),
Expand Down Expand Up @@ -169,7 +169,7 @@ test('brief mode with HTML type and writeDataJson should generate both HTML and

test('brief mode with default HTML configuration should use default values', async () => {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');

const outputDir = path.resolve(
tmpdir(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createRsdoctorMultiPlugin } from '../test-utils';

async function compileWithBriefJsonMode(jsonOptions?: any) {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');

const outputDir = path.resolve(
tmpdir(),
Expand Down Expand Up @@ -198,7 +198,7 @@ test('brief mode with JSON type and custom sections should generate selective da

test('brief mode with both HTML and JSON types should generate both outputs', async () => {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');

const outputDir = path.resolve(
tmpdir(),
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/doctor-webpack/plugins/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createRsdoctorPlugin } from '../test-utils';

const testLoaderPath = path.resolve(
__dirname,
'../fixtures/loaders/comment.js',
'../fixtures/loaders/comment.cjs',
);

async function webpack(
Expand Down
8 changes: 4 additions & 4 deletions e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createRsdoctorMultiPlugin } from '../test-utils';

async function webpack(tapName: string, compile: typeof compileByWebpack5) {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');
const res = await compile(file, {
module: {
rules: [
Expand Down Expand Up @@ -47,9 +47,9 @@ test('rsdoctor webpack5 multi-plugins options tests', async () => {
const tapName = 'Foo';
await webpack(tapName, compileByWebpack5);
const sdk = getSDK();
expect(sdk.type).toBe(0);
expect(sdk.extraConfig?.mode).toBe('brief');
expect(sdk.extraConfig?.brief).toMatchObject({
expect(sdk?.type).toBe(0);
expect(sdk?.extraConfig?.mode).toBe('brief');
expect(sdk?.extraConfig?.brief).toMatchObject({
htmlOptions: {
reportHtmlName: '111.html',
writeDataJson: false,
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/doctor-webpack/plugins/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createRsdoctorPlugin } from '../test-utils';

async function webpack(tapName: string, compile: typeof compileByWebpack5) {
const file = path.resolve(__dirname, '../fixtures/a.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js');
const loader = path.resolve(__dirname, '../fixtures/loaders/comment.cjs');
const res = await compile(file, {
module: {
rules: [
Expand Down
Loading