Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webpack integration tests for app w webpack-dev-server-fresh #21093

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Module from 'module'
import path from 'path'
import type { WebpackDevServerConfig } from '../devServer'
import debugFn from 'debug'

const debug = debugFn('cypress:webpack-dev-server-fresh:sourceRelativeWebpackModules')

type ModuleClass = typeof Module & {
_load(id: string, parent: Module, isMain: boolean): any
Expand Down Expand Up @@ -68,13 +71,20 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {

// First, we source the framework, ensuring it's sourced from the user's project and not the
// Cypress binary. This is the path we use to relative-resolve the
// This is generally used for Create React App and Vue CLI and other packages
// that ship webpack as a dependency. e.g. your-project/node_modules/react-scripts/node_modules/webpack
// So what we do, is we grab the framework's path, and try and find webpack relative to that framework.
if (config.framework) {
try {
const frameworkJsonPath = require.resolve(`${config.framework}/package.json`, {
paths: [searchRoot],
})

debug('Framework JSON path is %s', frameworkJsonPath)
const frameworkPathRoot = path.dirname(frameworkJsonPath)

debug('Framework JSON path root is %s', frameworkPathRoot)

// Want to make sure we're sourcing this from the user's code. Otherwise we can
// warn and tell them they don't have their dependencies installed
if (!frameworkPathRoot.includes(config.cypressConfig.cypressBinaryRoot)) {
Expand All @@ -86,14 +96,18 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {
searchRoot = frameworkPathRoot
}
} catch (e) {
debug('Error %o', e)
// TODO
}
}

// Webpack:

// At this point, we know where we're looking for webpack!
// We've made accommodations for certain frameworks that bundle it in (e.g. react-scripts)
let webpackJsonPath: string

debug('search root is %s', searchRoot)

try {
webpackJsonPath = require.resolve('webpack/package.json', {
paths: [searchRoot],
Expand All @@ -110,19 +124,25 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {
}),
],
})

debug('using webpack-batteries-included %s', webpackJsonPath)
}

result.webpack.importPath = path.dirname(webpackJsonPath)
result.webpack.packageJson = require(webpackJsonPath)
result.webpack.module = require(result.webpack.importPath)
result.webpack.majorVersion = getMajorVersion(result.webpack.packageJson, [4, 5]);
result.webpack.majorVersion = getMajorVersion(result.webpack.packageJson, [4, 5])

const webpackImportPath = result.webpack.importPath

(Module as ModuleClass)._load = function (request, parent, isMain) {
;(Module as ModuleClass)._load = function (request, parent, isMain) {
if (request === 'webpack' || request.startsWith('webpack/')) {
const resolvePath = require.resolve(request, {
paths: [searchRoot],
paths: [webpackImportPath],
})

debug('Resolve path %s', resolvePath)

return originalModuleLoad(resolvePath, parent, isMain)
}

Expand All @@ -132,7 +152,7 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {
(Module as ModuleClass)._resolveFilename = function (request, parent, isMain, options) {
if (request === 'webpack' || request.startsWith('webpack/') && !options?.paths) {
return originalModuleResolveFilename(request, parent, isMain, {
paths: [searchRoot],
paths: [webpackImportPath],
})
}

Expand Down