From 9f340a079bf4b7cd49200becdf93dd747981e0c2 Mon Sep 17 00:00:00 2001 From: xfsnowind Date: Sat, 14 Sep 2024 23:59:26 +0800 Subject: [PATCH] feat: support JIT config experimentalJustInTimeCompile remove the unused codes --- dist/CypressCTRspackPlugin.js | 8 ++--- dist/makeDefaultRspackConfig.js | 6 ++-- src/CypressCTRspackPlugin.ts | 11 ++----- src/makeDefaultRspackConfig.ts | 6 +++- .../makeDefaultRspackConfig.spec.ts.snap | 4 --- test/makeDefaultRspackConfig.spec.ts | 32 +++++++++++++++++++ 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/dist/CypressCTRspackPlugin.js b/dist/CypressCTRspackPlugin.js index b9c8845..ef93bd3 100644 --- a/dist/CypressCTRspackPlugin.js +++ b/dist/CypressCTRspackPlugin.js @@ -79,17 +79,13 @@ class CypressCTRspackPlugin { */ this.addCompilationHooks = (compilation) => { this.compilation = compilation; - /* istanbul ignore next */ - if ('NormalModule' in this.compilation.compiler.webpack) { - const loader = this.compilation.compiler.webpack.NormalModule.getCompilationHooks(compilation).loader; - loader.tap('CypressCTPlugin', this.addLoaderContext); - } + const loader = this.compilation.compiler.rspack.NormalModule.getCompilationHooks(compilation).loader; + loader.tap('CypressCTPlugin', this.addLoaderContext); }; this.files = options.files; this.supportFile = options.supportFile; this.projectRoot = options.projectRoot; this.devServerEvents = options.devServerEvents; - this.rspack = options.rspack; this.indexHtmlFile = options.indexHtmlFile; } /** diff --git a/dist/makeDefaultRspackConfig.js b/dist/makeDefaultRspackConfig.js index cae7b0e..744cebb 100644 --- a/dist/makeDefaultRspackConfig.js +++ b/dist/makeDefaultRspackConfig.js @@ -11,7 +11,7 @@ const OUTPUT_PATH = path_1.default.join(__dirname, 'dist'); const OsSeparatorRE = RegExp(`\\${path_1.default.sep}`, 'g'); const posixSeparator = '/'; function makeCypressRspackConfig(config) { - const { devServerConfig: { cypressConfig: { projectRoot, devServerPublicPathRoute, supportFile, indexHtmlFile, isTextTerminal: isRunMode, }, specs: files, devServerEvents, }, sourceRspackModulesResult: { rspack: { module: rspack }, }, } = config; + const { devServerConfig: { cypressConfig: { experimentalJustInTimeCompile, projectRoot, devServerPublicPathRoute, supportFile, indexHtmlFile, isTextTerminal: isRunMode, }, specs: files, devServerEvents, }, sourceRspackModulesResult: { rspack: { module: rspack }, }, } = config; const optimization = { // To prevent files from being tree shaken by rspack, we set optimization.sideEffects: false ensuring that // rspack does not recognize the sideEffects flag in the package.json and thus files are not unintentionally @@ -51,8 +51,10 @@ function makeCypressRspackConfig(config) { devtool: 'inline-source-map', }; if (isRunMode) { + // if experimentalJustInTimeCompile is configured, we need to watch for file changes as the spec entries are going to be updated per test + const ignored = experimentalJustInTimeCompile ? /node_modules/ : '**/*'; // Disable file watching when executing tests in `run` mode - finalConfig.watchOptions = { ignored: '**/*' }; + finalConfig.watchOptions = { ignored }; } return finalConfig; } diff --git a/src/CypressCTRspackPlugin.ts b/src/CypressCTRspackPlugin.ts index 52cc486..83535be 100644 --- a/src/CypressCTRspackPlugin.ts +++ b/src/CypressCTRspackPlugin.ts @@ -55,7 +55,6 @@ export class CypressCTRspackPlugin { private files: Cypress.Cypress['spec'][] = [] private supportFile: string | false private compilation: RspackCompilation | null = null - private rspack: Function private indexHtmlFile: string private readonly projectRoot: string @@ -66,7 +65,6 @@ export class CypressCTRspackPlugin { this.supportFile = options.supportFile this.projectRoot = options.projectRoot this.devServerEvents = options.devServerEvents - this.rspack = options.rspack this.indexHtmlFile = options.indexHtmlFile } @@ -140,13 +138,10 @@ export class CypressCTRspackPlugin { private addCompilationHooks = (compilation: RspackCompilation) => { this.compilation = compilation - /* istanbul ignore next */ - if ('NormalModule' in this.compilation.compiler.webpack) { - const loader = - this.compilation.compiler.webpack.NormalModule.getCompilationHooks(compilation).loader + const loader = + this.compilation.compiler.rspack.NormalModule.getCompilationHooks(compilation).loader - loader.tap('CypressCTPlugin', this.addLoaderContext) - } + loader.tap('CypressCTPlugin', this.addLoaderContext) } /** diff --git a/src/makeDefaultRspackConfig.ts b/src/makeDefaultRspackConfig.ts index e9fe453..5ca3fcd 100644 --- a/src/makeDefaultRspackConfig.ts +++ b/src/makeDefaultRspackConfig.ts @@ -15,6 +15,7 @@ export function makeCypressRspackConfig(config: CreateFinalRspackConfig): Config const { devServerConfig: { cypressConfig: { + experimentalJustInTimeCompile, projectRoot, devServerPublicPathRoute, supportFile, @@ -72,8 +73,11 @@ export function makeCypressRspackConfig(config: CreateFinalRspackConfig): Config } if (isRunMode) { + // if experimentalJustInTimeCompile is configured, we need to watch for file changes as the spec entries are going to be updated per test + const ignored = experimentalJustInTimeCompile ? /node_modules/ : '**/*' + // Disable file watching when executing tests in `run` mode - finalConfig.watchOptions = { ignored: '**/*' } + finalConfig.watchOptions = { ignored } } return finalConfig diff --git a/test/__snapshots__/makeDefaultRspackConfig.spec.ts.snap b/test/__snapshots__/makeDefaultRspackConfig.spec.ts.snap index f9e26a7..f9e9a8a 100644 --- a/test/__snapshots__/makeDefaultRspackConfig.spec.ts.snap +++ b/test/__snapshots__/makeDefaultRspackConfig.spec.ts.snap @@ -21,16 +21,12 @@ exports[`makeCypressRspackConfig should return a valid Configuration object 1`] "_events": {}, "_eventsCount": 0, "_maxListeners": undefined, - Symbol(shapeMode): false, Symbol(kCapture): false, }, "files": [], "indexHtmlFile": "path/to/indexHtmlFile", "onSpecsChange": [Function], "projectRoot": "path/to/project", - "rspack": { - "RspackDevServer": [Function], - }, "supportFile": "path/to/supportFile", }, ] diff --git a/test/makeDefaultRspackConfig.spec.ts b/test/makeDefaultRspackConfig.spec.ts index b343cf5..5587e67 100644 --- a/test/makeDefaultRspackConfig.spec.ts +++ b/test/makeDefaultRspackConfig.spec.ts @@ -3,6 +3,7 @@ import EventEmitter from 'events' import { CreateFinalRspackConfig } from '../src/createRspackDevServer' import { makeCypressRspackConfig } from '../src/makeDefaultRspackConfig' import { createModuleMatrixResult } from './test-helper/createModuleMatrixResult' +import { makeRspackConfig } from '../src/makeRspackConfig' describe('makeCypressRspackConfig', () => { // Returns a valid Configuration object with mode, optimization, output, plugins and devtool properties @@ -60,3 +61,34 @@ describe('makeCypressRspackConfig', () => { // path.sep = originalPathSep // }) }) + +describe('experimentalJustInTimeCompile', () => { + const devServerConfig: CreateFinalRspackConfig['devServerConfig'] = { + specs: [], + cypressConfig: { + projectRoot: '.', + devServerPublicPathRoute: '/test-public-path', + experimentalJustInTimeCompile: true, + baseUrl: null, + } as Cypress.PluginConfigOptions, + rspackConfig: { + entry: { main: 'src/index.js' }, + }, + devServerEvents: new EventEmitter(), + } + + describe('run mode', () => { + beforeEach(() => { + devServerConfig.cypressConfig.isTextTerminal = true + }) + + it('enables watching', async () => { + const actual = await makeRspackConfig({ + devServerConfig, + sourceRspackModulesResult: createModuleMatrixResult(), + }) + + expect(actual.watchOptions?.ignored).toStrictEqual(/node_modules/) + }) + }) +})