Skip to content

Commit 2050501

Browse files
fix: webpack integration tests for app w webpack-dev-server-fresh (#21093)
Co-authored-by: Zachary Williams <zachjw34@gmail.com>
1 parent 991312a commit 2050501

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

npm/webpack-dev-server-fresh/src/helpers/sourceRelativeWebpackModules.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import Module from 'module'
22
import path from 'path'
33
import type { WebpackDevServerConfig } from '../devServer'
4+
import debugFn from 'debug'
5+
6+
const debug = debugFn('cypress:webpack-dev-server-fresh:sourceRelativeWebpackModules')
47

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

6972
// First, we source the framework, ensuring it's sourced from the user's project and not the
7073
// Cypress binary. This is the path we use to relative-resolve the
74+
// This is generally used for Create React App and Vue CLI and other packages
75+
// that ship webpack as a dependency. e.g. your-project/node_modules/react-scripts/node_modules/webpack
76+
// So what we do, is we grab the framework's path, and try and find webpack relative to that framework.
7177
if (config.framework) {
7278
try {
7379
const frameworkJsonPath = require.resolve(`${config.framework}/package.json`, {
7480
paths: [searchRoot],
7581
})
82+
83+
debug('Framework JSON path is %s', frameworkJsonPath)
7684
const frameworkPathRoot = path.dirname(frameworkJsonPath)
7785

86+
debug('Framework JSON path root is %s', frameworkPathRoot)
87+
7888
// Want to make sure we're sourcing this from the user's code. Otherwise we can
7989
// warn and tell them they don't have their dependencies installed
8090
if (!frameworkPathRoot.includes(config.cypressConfig.cypressBinaryRoot)) {
@@ -86,14 +96,18 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {
8696
searchRoot = frameworkPathRoot
8797
}
8898
} catch (e) {
99+
debug('Error %o', e)
89100
// TODO
90101
}
91102
}
92103

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

109+
debug('search root is %s', searchRoot)
110+
97111
try {
98112
webpackJsonPath = require.resolve('webpack/package.json', {
99113
paths: [searchRoot],
@@ -110,19 +124,25 @@ export function sourceRelativeWebpackModules (config: WebpackDevServerConfig) {
110124
}),
111125
],
112126
})
127+
128+
debug('using webpack-batteries-included %s', webpackJsonPath)
113129
}
114130

115131
result.webpack.importPath = path.dirname(webpackJsonPath)
116132
result.webpack.packageJson = require(webpackJsonPath)
117133
result.webpack.module = require(result.webpack.importPath)
118-
result.webpack.majorVersion = getMajorVersion(result.webpack.packageJson, [4, 5]);
134+
result.webpack.majorVersion = getMajorVersion(result.webpack.packageJson, [4, 5])
135+
136+
const webpackImportPath = result.webpack.importPath
119137

120-
(Module as ModuleClass)._load = function (request, parent, isMain) {
138+
;(Module as ModuleClass)._load = function (request, parent, isMain) {
121139
if (request === 'webpack' || request.startsWith('webpack/')) {
122140
const resolvePath = require.resolve(request, {
123-
paths: [searchRoot],
141+
paths: [webpackImportPath],
124142
})
125143

144+
debug('Resolve path %s', resolvePath)
145+
126146
return originalModuleLoad(resolvePath, parent, isMain)
127147
}
128148

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

0 commit comments

Comments
 (0)