Skip to content

Commit

Permalink
fix: banner plugin switch condition (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 authored Jan 24, 2025
1 parent 08ac45e commit 343c727
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
6 changes: 3 additions & 3 deletions e2e/cases/doctor-rspack/banner-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, test } from '@playwright/test';
import { Utils } from '@rsdoctor/core/build-utils';
import { getSDK, setSDK } from '@rsdoctor/core/plugins';
import { compileByRspack } from '@scripts/test-helper';
import { BannerPlugin, Compiler } from '@rspack/core';
import { compileByRspack } from '@scripts/test-helper';
import path from 'path';
import { createRsdoctorPlugin } from './test-utils';
import { parseBundle } from '../../node_modules/@rsdoctor/core/dist/build-utils/build/utils/parseBundle';

let reportLoaderStartOrEndTimes = 0;

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

// @ts-ignore
const bundle = parseBundle(
const bundle = Utils.parseBundle(
path.join(__dirname, './fixtures/rspack-banner-plugin.js'),
// @ts-ignore
sdk.getStoreData().moduleGraph.modules,
Expand Down
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 { Utils } from '@rsdoctor/core/build-utils';
import { Compiler } from '@rspack/core';
import path from 'path';
import { createRsdoctorPlugin } from './test-utils';

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 = Utils.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.

0 comments on commit 343c727

Please sign in to comment.