Skip to content

Commit

Permalink
feat: support JIT config experimentalJustInTimeCompile
Browse files Browse the repository at this point in the history
remove the unused codes
  • Loading branch information
xfsnowind committed Sep 14, 2024
1 parent c4568cf commit 9f340a0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
8 changes: 2 additions & 6 deletions dist/CypressCTRspackPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
Expand Down
6 changes: 4 additions & 2 deletions dist/makeDefaultRspackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
11 changes: 3 additions & 8 deletions src/CypressCTRspackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/makeDefaultRspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function makeCypressRspackConfig(config: CreateFinalRspackConfig): Config
const {
devServerConfig: {
cypressConfig: {
experimentalJustInTimeCompile,
projectRoot,
devServerPublicPathRoute,
supportFile,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/__snapshots__/makeDefaultRspackConfig.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
Expand Down
32 changes: 32 additions & 0 deletions test/makeDefaultRspackConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/)
})
})
})

0 comments on commit 9f340a0

Please sign in to comment.