Skip to content

Commit 343c727

Browse files
authored
fix: banner plugin switch condition (#721)
1 parent 08ac45e commit 343c727

File tree

5 files changed

+130
-7
lines changed

5 files changed

+130
-7
lines changed

e2e/cases/doctor-rspack/banner-plugin.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expect, test } from '@playwright/test';
2+
import { Utils } from '@rsdoctor/core/build-utils';
23
import { getSDK, setSDK } from '@rsdoctor/core/plugins';
3-
import { compileByRspack } from '@scripts/test-helper';
44
import { BannerPlugin, Compiler } from '@rspack/core';
5+
import { compileByRspack } from '@scripts/test-helper';
56
import path from 'path';
67
import { createRsdoctorPlugin } from './test-utils';
7-
import { parseBundle } from '../../node_modules/@rsdoctor/core/dist/build-utils/build/utils/parseBundle';
88

99
let reportLoaderStartOrEndTimes = 0;
1010

@@ -118,7 +118,7 @@ test('rspack banner plugin', async () => {
118118
const sdk = getSDK();
119119

120120
// @ts-ignore
121-
const bundle = parseBundle(
121+
const bundle = Utils.parseBundle(
122122
path.join(__dirname, './fixtures/rspack-banner-plugin.js'),
123123
// @ts-ignore
124124
sdk.getStoreData().moduleGraph.modules,

e2e/cases/doctor-rspack/linter-rule-render.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Compiler } from '@rspack/core';
55
import path from 'path';
66
import fs from 'fs';
77
import { createRsdoctorPlugin } from './test-utils';
8-
import { devtools } from 'vue';
98

109
let reportLoaderStartOrEndTimes = 0;
1110
const ecmaVersion = 3;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { expect, test } from '@playwright/test';
2+
import { getSDK, setSDK } from '@rsdoctor/core/plugins';
3+
import { compileByRspack } from '@scripts/test-helper';
4+
import { Utils } from '@rsdoctor/core/build-utils';
5+
import { Compiler } from '@rspack/core';
6+
import path from 'path';
7+
import { createRsdoctorPlugin } from './test-utils';
8+
9+
let reportLoaderStartOrEndTimes = 0;
10+
11+
async function rspackCompile(
12+
_tapName: string,
13+
compile: typeof compileByRspack,
14+
) {
15+
const file = path.resolve(__dirname, './fixtures/a.js');
16+
const loader = path.resolve(__dirname, './fixtures/loaders/comment.js');
17+
18+
const esmLoader = path.resolve(
19+
__dirname,
20+
'./fixtures/loaders/esm-serialize-query-to-comment.mjs',
21+
);
22+
23+
const res = await compile(file, {
24+
resolve: {
25+
extensions: ['.ts', '.js'],
26+
},
27+
output: {
28+
path: path.join(__dirname, '../doctor-rspack/dist'),
29+
},
30+
module: {
31+
rules: [
32+
{
33+
test: /\.js/,
34+
use: loader,
35+
},
36+
{
37+
test: /\.js/,
38+
use: esmLoader,
39+
},
40+
{
41+
test: /\.[jt]s$/,
42+
use: {
43+
loader: 'builtin:swc-loader',
44+
options: {
45+
sourceMap: true,
46+
jsc: {
47+
parser: {
48+
syntax: 'typescript',
49+
},
50+
externalHelpers: true,
51+
preserveAllComments: false,
52+
},
53+
},
54+
},
55+
type: 'javascript/auto',
56+
},
57+
],
58+
},
59+
plugins: [
60+
// @ts-ignore
61+
createRsdoctorPlugin({
62+
supports: {
63+
banner: true,
64+
},
65+
}),
66+
{
67+
name: 'Foo',
68+
apply(compiler: Compiler) {
69+
compiler.hooks.beforeRun.tapPromise(
70+
{ name: 'Foo', stage: 99999 },
71+
async () => {
72+
const sdk = getSDK();
73+
setSDK(
74+
new Proxy(sdk, {
75+
get(target, key, receiver) {
76+
switch (key) {
77+
case 'reportLoader':
78+
return null;
79+
case 'reportLoaderStartOrEnd':
80+
return (_data: any) => {
81+
reportLoaderStartOrEndTimes += 1;
82+
};
83+
default:
84+
return Reflect.get(target, key, receiver);
85+
}
86+
},
87+
set(target, key, value, receiver) {
88+
return Reflect.set(target, key, value, receiver);
89+
},
90+
defineProperty(target, p, attrs) {
91+
return Reflect.defineProperty(target, p, attrs);
92+
},
93+
}),
94+
);
95+
},
96+
);
97+
},
98+
},
99+
],
100+
});
101+
102+
return res;
103+
}
104+
105+
test('rspack banner plugin', async () => {
106+
const tapName = 'Foo';
107+
await rspackCompile(tapName, compileByRspack);
108+
const sdk = getSDK();
109+
110+
// @ts-ignore
111+
const bundle = Utils.parseBundle(
112+
path.join(__dirname, './fixtures/rspack-banner-plugin.js'),
113+
// @ts-ignore
114+
sdk.getStoreData().moduleGraph.modules,
115+
);
116+
117+
expect(JSON.stringify(bundle.modules)).toBe(
118+
'{"":{"size":313,"sizeConvert":"313 B","content":"function (\\n __unused_webpack_module,\\n __webpack_exports__,\\n __webpack_require__,\\n ) {\\n \'use strict\';\\n __webpack_require__.r(__webpack_exports__);\\n __webpack_require__.d(__webpack_exports__, {\\n a: function () {\\n return a;\\n },\\n });\\n var a = 1;\\n }"}}',
119+
);
120+
const res = sdk.getStoreData().chunkGraph;
121+
expect(res.assets[0].content).toContain('RSDOCTOR_START');
122+
expect(res.assets[0].content).toContain('RSDOCTOR_END');
123+
});

packages/core/src/inner-plugins/plugins/bundleTagPlugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export class InternalBundleTagPlugin<
2020
},
2121
async () => {
2222
if (
23-
!compilation.options.plugins
23+
(!compilation.options.plugins
2424
.map((p) => p && p.constructor.name)
25-
.includes('BannerPlugin') ||
25+
.includes('BannerPlugin') &&
26+
supportBannerPlugin !== true) ||
2627
supportBannerPlugin === false
2728
) {
2829
return;

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)