Skip to content
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

fix: banner plugin switch condition #721

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion e2e/cases/doctor-rspack/linter-rule-render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Compiler } from '@rspack/core';
import path from 'path';
import fs from 'fs';
import { createRsdoctorPlugin } from './test-utils';
import { devtools } from 'vue';

let reportLoaderStartOrEndTimes = 0;
const ecmaVersion = 3;
Expand Down
123 changes: 123 additions & 0 deletions e2e/cases/doctor-rspack/tag-plugin-without-banner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { expect, test } from '@playwright/test';
import { getSDK, setSDK } from '@rsdoctor/core/plugins';
import { compileByRspack } from '@scripts/test-helper';
import { Compiler } from '@rspack/core';
import path from 'path';
import { createRsdoctorPlugin } from './test-utils';
import { parseBundle } from '../../node_modules/@rsdoctor/core/dist/build-utils/build/utils/parseBundle';
easy1090 marked this conversation as resolved.
Show resolved Hide resolved

let reportLoaderStartOrEndTimes = 0;

async function rspackCompile(
_tapName: string,
compile: typeof compileByRspack,
) {
const file = path.resolve(__dirname, './fixtures/a.js');
const loader = path.resolve(__dirname, './fixtures/loaders/comment.js');

const esmLoader = path.resolve(
__dirname,
'./fixtures/loaders/esm-serialize-query-to-comment.mjs',
);

const res = await compile(file, {
resolve: {
extensions: ['.ts', '.js'],
},
output: {
path: path.join(__dirname, '../doctor-rspack/dist'),
},
module: {
rules: [
{
test: /\.js/,
use: loader,
},
{
test: /\.js/,
use: esmLoader,
},
{
test: /\.[jt]s$/,
use: {
loader: 'builtin:swc-loader',
options: {
sourceMap: true,
jsc: {
parser: {
syntax: 'typescript',
},
externalHelpers: true,
preserveAllComments: false,
},
},
},
type: 'javascript/auto',
},
],
},
plugins: [
// @ts-ignore
createRsdoctorPlugin({
supports: {
banner: true,
},
}),
{
name: 'Foo',
apply(compiler: Compiler) {
compiler.hooks.beforeRun.tapPromise(
{ name: 'Foo', stage: 99999 },
async () => {
const sdk = getSDK();
setSDK(
new Proxy(sdk, {
get(target, key, receiver) {
switch (key) {
case 'reportLoader':
return null;
case 'reportLoaderStartOrEnd':
return (_data: any) => {
reportLoaderStartOrEndTimes += 1;
};
default:
return Reflect.get(target, key, receiver);
}
},
set(target, key, value, receiver) {
return Reflect.set(target, key, value, receiver);
},
defineProperty(target, p, attrs) {
return Reflect.defineProperty(target, p, attrs);
},
}),
);
},
);
},
},
],
});

return res;
}

test('rspack banner plugin', async () => {
const tapName = 'Foo';
await rspackCompile(tapName, compileByRspack);
const sdk = getSDK();

// @ts-ignore
const bundle = parseBundle(
path.join(__dirname, './fixtures/rspack-banner-plugin.js'),
// @ts-ignore
sdk.getStoreData().moduleGraph.modules,
);

expect(JSON.stringify(bundle.modules)).toBe(
'{"":{"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 }"}}',
);
const res = sdk.getStoreData().chunkGraph;
expect(res.assets[0].content).toContain('RSDOCTOR_START');
expect(res.assets[0].content).toContain('RSDOCTOR_END');
});
5 changes: 3 additions & 2 deletions packages/core/src/inner-plugins/plugins/bundleTagPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export class InternalBundleTagPlugin<
},
async () => {
if (
!compilation.options.plugins
(!compilation.options.plugins
.map((p) => p && p.constructor.name)
.includes('BannerPlugin') ||
.includes('BannerPlugin') &&
supportBannerPlugin !== true) ||
supportBannerPlugin === false
) {
return;
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading