From d5d73bdac40b1acfd796b5538b0cb8117370213d Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 17 Sep 2024 15:27:28 +0800 Subject: [PATCH] test(e2e): validate dev source map --- e2e/cases/source-map/index.test.ts | 46 ++++++++++++++++++-------- e2e/cases/source-map/rsbuild.config.ts | 14 ++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 e2e/cases/source-map/rsbuild.config.ts diff --git a/e2e/cases/source-map/index.test.ts b/e2e/cases/source-map/index.test.ts index 25adbfb7b9..86b18d7902 100644 --- a/e2e/cases/source-map/index.test.ts +++ b/e2e/cases/source-map/index.test.ts @@ -1,8 +1,8 @@ -import { build } from '@e2e/helper'; +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { build, dev } from '@e2e/helper'; import { expect, test } from '@playwright/test'; import type { Rspack } from '@rsbuild/core'; -import { pluginReact } from '@rsbuild/plugin-react'; - import sourceMap from 'source-map'; const fixtures = __dirname; @@ -30,22 +30,13 @@ async function validateSourceMap( async function testSourceMapType(devtool: Rspack.Configuration['devtool']) { const rsbuild = await build({ cwd: fixtures, - plugins: [pluginReact()], rsbuildConfig: { - dev: { - writeToDisk: true, - }, output: { sourceMap: { js: devtool, }, legalComments: 'none', }, - performance: { - chunkSplit: { - strategy: 'all-in-one', - }, - }, }, }); @@ -108,7 +99,6 @@ for (const devtool of productionDevtools) { test('should not generate source map by default in production build', async () => { const rsbuild = await build({ cwd: fixtures, - plugins: [pluginReact()], }); const files = await rsbuild.unwrapOutputJSON(false); @@ -122,3 +112,33 @@ test('should not generate source map by default in production build', async () = expect(jsMapFiles.length).toEqual(0); expect(cssMapFiles.length).toEqual(0); }); + +test('should generate source map correctly in development build', async ({ + page, +}) => { + const rsbuild = await dev({ + cwd: fixtures, + page, + }); + + const files = await rsbuild.unwrapOutputJSON(false); + + const jsMapFile = Object.keys(files).find((files) => + files.endsWith('.js.map'), + ); + expect(jsMapFile).not.toBeUndefined(); + + const jsContent = await readFileSync(jsMapFile!, 'utf-8'); + const jsMap = JSON.parse(jsContent); + expect(jsMap.sources.length).toBeGreaterThan(1); + expect(jsMap.file).toEqual('static/js/index.js'); + expect(jsMap.sourcesContent).toContain( + readFileSync(join(fixtures, 'src/App.jsx'), 'utf-8'), + ); + expect(jsMap.sourcesContent).toContain( + readFileSync(join(fixtures, 'src/index.js'), 'utf-8'), + ); + expect(jsMap.mappings).not.toBeUndefined(); + + await rsbuild.close(); +}); diff --git a/e2e/cases/source-map/rsbuild.config.ts b/e2e/cases/source-map/rsbuild.config.ts new file mode 100644 index 0000000000..af37570411 --- /dev/null +++ b/e2e/cases/source-map/rsbuild.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from '@rsbuild/core'; +import { pluginReact } from '@rsbuild/plugin-react'; + +export default defineConfig({ + plugins: [pluginReact()], + dev: { + writeToDisk: true, + }, + performance: { + chunkSplit: { + strategy: 'all-in-one', + }, + }, +});