-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
240 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { expect, test, webkit } 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 fs from 'fs'; | ||
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({ | ||
mode: 'brief', | ||
}), | ||
{ | ||
name: 'XXX', | ||
apply(compiler: Compiler) { | ||
compiler.hooks.beforeRun.tapPromise( | ||
{ name: 'XXX', 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 = 'XXX'; | ||
await rspackCompile(tapName, compileByRspack); | ||
|
||
const reportPath = path.join( | ||
__dirname, | ||
'./dist/.rsdoctor/rsdoctor-report.html', | ||
); | ||
|
||
fileExists(reportPath); | ||
|
||
const browser = await webkit.launch(); | ||
|
||
// Create a new browser context | ||
const context = await browser.newContext(); | ||
|
||
// Open a new page | ||
const page = await context.newPage(); | ||
|
||
// Navigate to a URL | ||
await page.goto(`file:///${reportPath}`); | ||
|
||
// Perform actions on the page | ||
const title = await page.title(); | ||
expect(title).toBe('Rsdoctor'); | ||
|
||
const titleContent = 'Bundle Overall'; | ||
|
||
const bundleTitleExists = await page | ||
.locator(`text=${titleContent}`) | ||
.first() | ||
.isVisible(); | ||
|
||
const compileTabExists = await page | ||
.locator(`text='Compile Analysis'`) | ||
.first() | ||
.isVisible(); | ||
|
||
const bundleTabExists = await page | ||
.locator(`text='Bundle Size'`) | ||
.first() | ||
.isVisible(); | ||
|
||
expect(bundleTitleExists).toBe(true); | ||
expect(compileTabExists).toBe(true); | ||
expect(bundleTabExists).toBe(true); | ||
|
||
// Close the page | ||
await page.close(); | ||
|
||
// Close the browser context | ||
await context.close(); | ||
|
||
// Close the browser | ||
await browser.close(); | ||
}); | ||
|
||
async function fileExists(filePath: string) { | ||
try { | ||
await fs.existsSync(filePath); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.