Core functionality for creating and managing webpack dev server instances for Cypress component testing.
Main entry point that creates and configures a webpack dev server instance for Cypress component testing.
/**
* Creates & returns a WebpackDevServer for serving files related to Cypress Component Testing
* @param devServerConfig - Configuration object containing specs, Cypress config, and webpack options
* @returns Promise resolving to Cypress.ResolvedDevServerConfig with port and close method
*/
function devServer(devServerConfig: WebpackDevServerConfig): Promise<Cypress.ResolvedDevServerConfig>;
type WebpackDevServerConfig = {
/** Array of test spec files to be served */
specs: Cypress.Spec[];
/** Cypress configuration options including project root and support file */
cypressConfig: Cypress.PluginConfigOptions;
/** Event emitter for dev server lifecycle events */
devServerEvents: NodeJS.EventEmitter;
/** Optional callback when webpack config cannot be found */
onConfigNotFound?: (devServer: 'webpack', cwd: string, lookedIn: string[]) => void;
/** User's webpack configuration - can be object or function */
webpackConfig?: ConfigHandler;
} & FrameworkConfig;
type FrameworkConfig = {
/** Framework configuration for non-Angular frameworks */
framework?: Exclude<Frameworks, 'angular'>;
} | {
/** Angular framework configuration with specific options */
framework: 'angular';
options?: {
projectConfig: Cypress.AngularDevServerProjectConfig;
};
}
type ConfigHandler =
| Partial<Configuration>
| (() => Partial<Configuration> | Promise<Partial<Configuration>>);
type Frameworks = Extract<Cypress.DevServerConfigOptions, { bundler: 'webpack' }>['framework'];Usage Examples:
import { devServer } from '@cypress/webpack-dev-server';
import { defineConfig } from 'cypress';
// Basic usage with framework detection
export default defineConfig({
component: {
devServer(devServerConfig) {
return devServer({
...devServerConfig,
framework: 'react'
});
}
}
});
// Advanced usage with custom webpack config
export default defineConfig({
component: {
devServer(devServerConfig) {
return devServer({
...devServerConfig,
framework: 'vue',
webpackConfig: {
resolve: {
alias: {
'@': path.resolve(__dirname, 'src')
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
}
}
});
}
}
});
// Using function-based webpack config
export default defineConfig({
component: {
devServer(devServerConfig) {
return devServer({
...devServerConfig,
framework: 'react',
webpackConfig: async () => {
const baseConfig = await import('./webpack.config.js');
return {
...baseConfig.default,
mode: 'development'
};
}
});
}
}
});Internal method for synchronously creating the webpack server instance without starting it. Primarily used for testing.
/**
* Synchronously create the webpack server instance, without starting.
* Useful for testing
* @internal
* @param devServerConfig - Same configuration as main devServer function
* @returns Promise resolving to server instance with version info
*/
devServer.create(devServerConfig: WebpackDevServerConfig): Promise<DevServerCreateResult>;
interface DevServerCreateResult {
/** Webpack dev server version (currently always 5) */
version: 5;
/** Webpack dev server instance */
server: WebpackDevServer5;
/** Webpack compiler instance */
compiler: Compiler;
}Array of Cypress spec files to be served by the webpack dev server.
specs: Cypress.Spec[]Cypress configuration options containing project settings and paths.
cypressConfig: Cypress.PluginConfigOptionsKey properties used:
projectRoot: Root directory of the projectsupportFile: Path to support filedevServerPublicPathRoute: Public path for dev server assetsindexHtmlFile: Custom HTML template fileEvent emitter for dev server lifecycle events and communication.
devServerEvents: NodeJS.EventEmitterCallback function invoked when webpack configuration cannot be automatically detected.
onConfigNotFound?: (devServer: 'webpack', cwd: string, lookedIn: string[]) => voidParameters:
devServer: Always 'webpack' for this packagecwd: Current working directory where search was performedlookedIn: Array of paths that were searched for webpack configUser's webpack configuration that will be merged with Cypress-specific settings.
webpackConfig?: ConfigHandler
type ConfigHandler =
| Partial<Configuration>
| (() => Partial<Configuration> | Promise<Partial<Configuration>>);Can be:
Target framework for component testing. Determines which preset handlers to use.
framework?: Frameworks
type Frameworks = 'react' | 'vue' | 'svelte' | 'next' | 'angular' | string;Supported values:
'react': React framework with JSX support'vue': Vue.js framework with SFC support'svelte': Svelte framework support'next': Next.js framework with specialized webpack handling'angular': Angular framework with CLI integrationcypress-ct-* patternFramework-specific configuration options.
options?: {
projectConfig: Cypress.AngularDevServerProjectConfig;
}Currently only used for Angular framework configuration.
The devServer function returns a Promise that resolves to Cypress.ResolvedDevServerConfig:
interface Cypress.ResolvedDevServerConfig {
/** Port number where the dev server is running */
port: number;
/** Function to close the dev server (mainly for testing) */
close: (done?: () => void) => Promise<void>;
}The devServer function may reject with errors in the following cases:
Enable debug mode with webpack-bundle-analyzer for troubleshooting:
DEBUG=cypress-verbose:webpack-dev-server:bundle-analyzer cypress openThis will generate a webpack-bundle-analyzer report to help identify bundle size issues and chunk load errors.