From 3ea9aecbef4806380b690f0012425c4f35714c2d Mon Sep 17 00:00:00 2001 From: nyqykk <1327719263@qq.com> Date: Wed, 18 Dec 2024 14:44:13 +0800 Subject: [PATCH] feat: add outputDir for searching source map --- e2e/cases/doctor-rspack/plugin.test.ts | 16 +++++++++++++++- .../src/rules/rules/ecma-version-check/index.ts | 6 ++++-- .../src/rules/rules/ecma-version-check/types.ts | 4 +++- scripts/test-helper/src/rspack.ts | 4 ---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/e2e/cases/doctor-rspack/plugin.test.ts b/e2e/cases/doctor-rspack/plugin.test.ts index bb8fac06..569ca9e7 100644 --- a/e2e/cases/doctor-rspack/plugin.test.ts +++ b/e2e/cases/doctor-rspack/plugin.test.ts @@ -65,7 +65,18 @@ async function rspackCompile(tapName: string, compile: typeof compileByRspack) { }, plugins: [ // @ts-ignore - createRsdoctorPlugin({}), + createRsdoctorPlugin({ + linter: { + rules: { + 'ecma-version-check': [ + 'Error', + { + ecmaVersion: 3, + }, + ], + }, + }, + }), { name: 'Foo', apply(compiler: Compiler) { @@ -108,6 +119,7 @@ async function rspackCompile(tapName: string, compile: typeof compileByRspack) { }, }, ], + devtool: 'cheap-module-source-map', }); return res; @@ -140,6 +152,8 @@ test('rspack data store', async () => { const graphData = datas.moduleGraph; const configs = datas.configs; + const ecmaCheckError = datas.errors.some((e) => e.code === 'E1004'); + expect(ecmaCheckError).toBeTruthy(); os.EOL === '\n' ? expect( graphData.modules[0].webpackId.indexOf('/fixtures/a.js'), diff --git a/packages/core/src/rules/rules/ecma-version-check/index.ts b/packages/core/src/rules/rules/ecma-version-check/index.ts index e915244c..69ad501c 100644 --- a/packages/core/src/rules/rules/ecma-version-check/index.ts +++ b/packages/core/src/rules/rules/ecma-version-check/index.ts @@ -32,7 +32,8 @@ export const rule = defineRule(() => { path: root, env: 'production', }); - const { exclude, excludeOutput, targets, ecmaVersion } = ruleConfig; + const { exclude, excludeOutput, targets, ecmaVersion, outputDir } = + ruleConfig; const finalTargets = targets || browserslistConfig || []; // disable check syntax if (!finalTargets.length && !ecmaVersion) { @@ -45,7 +46,8 @@ export const rule = defineRule(() => { rootPath: root, targets: finalTargets, }); - await checkSyntax.check(asset.path, asset.content); + const assetPath = path.resolve(root, outputDir || 'dist', asset.path); + await checkSyntax.check(assetPath, asset.content); checkSyntax.errors.forEach((err) => { report({ message: `Find some syntax that does not match "ecmaVersion <= ${checkSyntax.ecmaVersion}"`, diff --git a/packages/core/src/rules/rules/ecma-version-check/types.ts b/packages/core/src/rules/rules/ecma-version-check/types.ts index b09c688c..5b07c0ae 100644 --- a/packages/core/src/rules/rules/ecma-version-check/types.ts +++ b/packages/core/src/rules/rules/ecma-version-check/types.ts @@ -1,3 +1,5 @@ import type { PluginCheckSyntaxOptions } from '@rsbuild/plugin-check-syntax'; -export interface Config extends PluginCheckSyntaxOptions {} +export interface Config extends PluginCheckSyntaxOptions { + outputDir?: string; +} diff --git a/scripts/test-helper/src/rspack.ts b/scripts/test-helper/src/rspack.ts index 4e69f956..9609c6c6 100644 --- a/scripts/test-helper/src/rspack.ts +++ b/scripts/test-helper/src/rspack.ts @@ -36,10 +36,6 @@ export function compileByRspack( const compiler = rspack({ entry: absPath, mode: 'none', - output: { - path: path.resolve(__dirname), - filename: 'bundle.js', - }, stats: 'normal', cache: false, ...options,