-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathfindNextWebpackConfig.js
54 lines (46 loc) · 1.69 KB
/
findNextWebpackConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @ts-check
/// <reference types="next" />
const debug = require('debug')('@cypress/react')
const getNextJsBaseWebpackConfig = require('next/dist/build/webpack-config').default
const { findPagesDir } = require('../../dist/next/findPagesDir')
async function getNextWebpackConfig (config) {
let loadConfig
try {
loadConfig = require('next/dist/next-server/server/config').default
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
// Starting from 11.0.2-canary.23, the server config file
// is not in the next-server folder anymore.
// @ts-ignore
loadConfig = require('next/dist/server/config').default
} else {
throw e
}
}
const nextConfig = await loadConfig('development', config.projectRoot)
const nextWebpackConfig = await getNextJsBaseWebpackConfig(
config.projectRoot,
{
buildId: `@cypress/react-${Math.random().toString()}`,
config: nextConfig,
dev: true,
isServer: false,
pagesDir: findPagesDir(config.projectRoot),
entrypoints: {},
rewrites: { fallback: [], afterFiles: [], beforeFiles: [] },
},
)
debug('resolved next.js webpack config %o', nextWebpackConfig)
return nextWebpackConfig
}
let webpackConfigCache = null
/** Resolving next.js webpack and all config with plugin takes long, so cache the webpack configuration */
module.exports = async function findNextWebpackConfig (config) {
// ⛔️ ⛔️ Comment this `if` for debugging
if (webpackConfigCache !== null) {
return webpackConfigCache
}
webpackConfigCache = await getNextWebpackConfig(config)
debug('created and cached webpack preprocessor based on next.config.js', webpackConfigCache)
return webpackConfigCache
}