System for sourcing webpack-related dependencies from the user's project and creating optimized configurations.
The module sourcing system loads webpack and related dependencies from the user's project rather than bundling them, ensuring version compatibility and allowing users to control their build toolchain.
interface SourceRelativeWebpackResult {
/** Framework-specific dependency (if applicable) */
framework: SourcedDependency | null;
/** Sourced webpack module with version info */
webpack: SourcedWebpack;
/** Sourced webpack-dev-server module */
webpackDevServer: SourcedWebpackDevServer;
/** Sourced HTML webpack plugin */
htmlWebpackPlugin: SourcedHtmlWebpackPlugin;
}All sourced dependencies extend this base interface:
interface SourcedDependency {
/** Import path used to load the dependency */
importPath: string;
/** Package.json metadata for version and name info */
packageJson: PackageJson;
}
interface PackageJson {
name: string;
version: string;
}Sources the webpack compiler from the user's project:
interface SourcedWebpack extends SourcedDependency {
/** Webpack constructor function */
module: Function;
/** Major version number (4 or 5) */
majorVersion: 4 | 5;
}Usage:
Sources the webpack-dev-server from the user's project:
interface SourcedWebpackDevServer extends SourcedDependency {
/** WebpackDevServer constructor */
module: { new (...args: unknown[]): unknown };
/** Major version number (4 or 5) */
majorVersion: 4 | 5;
}Features:
Sources the HTML webpack plugin for template processing:
interface SourcedHtmlWebpackPlugin extends SourcedDependency {
/** HTML webpack plugin module */
module: unknown;
/** Major version number (4 or 5) */
majorVersion: 4 | 5;
}Purpose:
Enhanced Node.js Module interface for internal module resolution:
type ModuleClass = typeof Module & {
/** Internal Node.js module loader */
_load(id: string, parent: Module, isMain: boolean): any;
/** Internal filename resolution */
_resolveFilename(request: string, parent: Module, isMain: boolean, options?: { paths: string[] }): string;
/** Module cache for loaded modules */
_cache: Record<string, Module>;
};Usage:
The module sourcing system follows this resolution process:
The system automatically detects webpack configuration files:
const configFiles: string[] = [
'webpack.config.ts',
'webpack.config.js',
'webpack.config.mjs',
'webpack.config.cjs'
];Detection Order:
webpack.config.ts)webpack.config.js)webpack.config.mjs)webpack.config.cjs)Usage Examples:
// webpack.config.js - Standard configuration
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.jsx?$/,
use: 'babel-loader'
}
]
}
};
// webpack.config.ts - TypeScript configuration
import { Configuration } from 'webpack';
const config: Configuration = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader'
}
]
}
};
export default config;
// webpack.config.mjs - ES Module configuration
export default {
entry: './src/index.js',
mode: 'development'
};Built-in webpack-bundle-analyzer support for debugging bundle issues:
/** Debug namespace for webpack bundle analyzer */
const WBADebugNamespace: string = 'cypress-verbose:webpack-dev-server:bundle-analyzer';
/** Checks if webpack bundle analyzer debug mode is enabled */
function isWebpackBundleAnalyzerEnabled(): boolean;Usage:
# Enable bundle analysis
DEBUG=cypress-verbose:webpack-dev-server:bundle-analyzer cypress open
# This will generate a webpack-bundle-analyzer reportFeatures:
When bundle analyzer is enabled, the system provides:
When user dependencies cannot be found:
// onConfigNotFound callback will be triggered
onConfigNotFound?: (devServer: 'webpack', cwd: string, lookedIn: string[]) => voidError Scenarios:
Supported Versions:
Compatibility Matrix:
| Webpack | Dev Server | HTML Plugin | Support |
|---------|------------|-------------|---------|
| 4.x | 4.x | 4.x | ✅ |
| 5.x | 5.x | 5.x | ✅ |
| 4.x | 5.x | 4.x | ❌ |
| 5.x | 4.x | 5.x | ❌ |For advanced scenarios, you can influence module sourcing:
import { devServer } from '@cypress/webpack-dev-server';
export default defineConfig({
component: {
devServer(devServerConfig) {
return devServer({
...devServerConfig,
webpackConfig: async () => {
// Custom webpack loading logic
const webpack = require('webpack');
const path = require('path');
return {
resolve: {
modules: [
path.resolve(__dirname, 'custom_modules'),
'node_modules'
]
}
};
}
});
}
}
});Best Practices: