@cypress/webpack-dev-server 1.8.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +47 -81
- package/dist/CypressCTWebpackPlugin.d.ts +15 -0
- package/dist/CypressCTWebpackPlugin.js +103 -0
- package/dist/aut-runner.js +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +9 -0
- package/dist/createWebpackDevServer.d.ts +26 -0
- package/dist/createWebpackDevServer.js +49 -0
- package/dist/devServer.d.ts +35 -0
- package/dist/devServer.js +103 -0
- package/dist/helpers/createReactAppHandler.d.ts +8 -0
- package/dist/helpers/createReactAppHandler.js +300 -0
- package/dist/helpers/nextHandler.d.ts +2 -0
- package/dist/helpers/nextHandler.js +197 -0
- package/dist/helpers/nuxtHandler.d.ts +2 -0
- package/dist/helpers/nuxtHandler.js +27 -0
- package/dist/helpers/sourceRelativeWebpackModules.d.ts +46 -0
- package/dist/helpers/sourceRelativeWebpackModules.js +197 -0
- package/dist/helpers/vueCliHandler.d.ts +2 -0
- package/dist/helpers/vueCliHandler.js +23 -0
- package/dist/index.d.ts +3 -15
- package/dist/index.js +4 -63
- package/dist/loader.d.ts +1 -3
- package/dist/loader.js +7 -27
- package/dist/makeDefaultWebpackConfig.d.ts +1 -0
- package/dist/makeDefaultWebpackConfig.js +48 -0
- package/dist/makeWebpackConfig.d.ts +6 -16
- package/dist/makeWebpackConfig.js +120 -93
- package/dist/measureWebpackPerformance.d.ts +1 -2
- package/dist/measureWebpackPerformance.js +10 -8
- package/package.json +34 -25
- package/dist/plugin.d.ts +0 -45
- package/dist/plugin.js +0 -84
- package/dist/startServer.d.ts +0 -13
- package/dist/startServer.js +0 -75
- package/dist/webpack.config.d.ts +0 -2
- package/dist/webpack.config.js +0 -24
- package/dist/webpackDevServerFacts.d.ts +0 -6
- package/dist/webpackDevServerFacts.js +0 -19
- package/index-template.html +0 -12
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reactScriptsFiveModifications = exports.cypressGlobals = exports.createReactAppHandler = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
|
|
6
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
7
|
+
const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
|
|
8
|
+
const debug = (0, debug_1.default)('cypress:webpack-dev-server:create-react-app');
|
|
9
|
+
/**
|
|
10
|
+
* Sourcing the config for create-react-app
|
|
11
|
+
*/
|
|
12
|
+
function createReactAppHandler(devServerConfig) {
|
|
13
|
+
const sourceWebpackModulesResult = (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig);
|
|
14
|
+
// this is required because
|
|
15
|
+
// 1) we use our own HMR and we don't need react-refresh transpiling overhead
|
|
16
|
+
// 2) it doesn't work with process.env=test @see https://github.com/cypress-io/cypress-realworld-app/pull/832
|
|
17
|
+
process.env.FAST_REFRESH = 'false';
|
|
18
|
+
const webpackConfig = loadWebpackConfig(devServerConfig);
|
|
19
|
+
addCypressToWebpackEslintRulesInPlace(webpackConfig);
|
|
20
|
+
getTranspileFolders(devServerConfig.cypressConfig).forEach((folder) => {
|
|
21
|
+
allowModuleSourceInPlace(folder, webpackConfig);
|
|
22
|
+
addFolderToBabelLoaderTranspileInPlace(folder, webpackConfig);
|
|
23
|
+
});
|
|
24
|
+
if (sourceWebpackModulesResult.webpack.majorVersion === 5) {
|
|
25
|
+
debug('Modifying configuration for react-scripts@5');
|
|
26
|
+
reactScriptsFiveModifications(webpackConfig);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
frameworkConfig: webpackConfig,
|
|
30
|
+
sourceWebpackModulesResult,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.createReactAppHandler = createReactAppHandler;
|
|
34
|
+
function loadWebpackConfig(devServerConfig) {
|
|
35
|
+
let webpackConfigPath;
|
|
36
|
+
const envName = 'test';
|
|
37
|
+
process.env.NODE_ENV = envName;
|
|
38
|
+
process.env.BABEL_ENV = envName;
|
|
39
|
+
try {
|
|
40
|
+
// Search for react-scripts webpack config
|
|
41
|
+
webpackConfigPath = require.resolve('react-scripts/config/webpack.config.js', {
|
|
42
|
+
paths: [devServerConfig.cypressConfig.projectRoot],
|
|
43
|
+
});
|
|
44
|
+
debug('Found react-scripts webpack config at %s', webpackConfigPath);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
// Might be an ejected cra app, search for common webpack configs
|
|
48
|
+
const ejectedWebpackConfigPath = path_1.default.join(devServerConfig.cypressConfig.projectRoot, 'config', 'webpack.config.js');
|
|
49
|
+
try {
|
|
50
|
+
webpackConfigPath = require.resolve(ejectedWebpackConfigPath);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
throw new Error(`Failed to find webpack at ${ejectedWebpackConfigPath}. We looked in this location as we could not find the "react-scripts" dependency and are assuming an ejected create-react-app.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
let webpackConfig = require(webpackConfigPath);
|
|
58
|
+
if (webpackConfig.default) {
|
|
59
|
+
// we probably loaded TS file
|
|
60
|
+
debug('loaded webpack options has .default - taking that as the config');
|
|
61
|
+
webpackConfig = webpackConfig.default;
|
|
62
|
+
}
|
|
63
|
+
if (typeof webpackConfig === 'function') {
|
|
64
|
+
debug('calling webpack function with environment "%s"', envName);
|
|
65
|
+
webpackConfig = webpackConfig('development');
|
|
66
|
+
}
|
|
67
|
+
return webpackConfig;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
throw new Error(`Failed to require webpack config at ${webpackConfigPath} with error: ${err}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.cypressGlobals = ['cy', 'Cypress', 'before', 'after', 'context'];
|
|
74
|
+
function addCypressToWebpackEslintRulesInPlace(webpackConfig) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const eslintPlugin = (_a = webpackConfig.plugins) === null || _a === void 0 ? void 0 : _a.find((plugin) => plugin.constructor.name === 'ESLintWebpackPlugin');
|
|
77
|
+
if (eslintPlugin) {
|
|
78
|
+
const cypressGlobalsRules = exports.cypressGlobals
|
|
79
|
+
.reduce((acc, global) => (Object.assign(Object.assign({}, acc), { [global]: 'writable' })), {});
|
|
80
|
+
eslintPlugin.options.baseConfig = Object.assign(Object.assign({}, eslintPlugin.options.baseConfig), { globals: Object.assign(Object.assign({}, (_b = eslintPlugin.options.baseConfig) === null || _b === void 0 ? void 0 : _b.globals), cypressGlobalsRules) });
|
|
81
|
+
debug('Found ESLintWebpackPlugin, modified eslint config %o', eslintPlugin.options.baseConfig);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function getTranspileFolders(cypressConfig) {
|
|
85
|
+
var _a;
|
|
86
|
+
const rawFolders = (_a = cypressConfig.addTranspiledFolders) !== null && _a !== void 0 ? _a : [];
|
|
87
|
+
const folders = rawFolders.map((folder) => path_1.default.resolve(cypressConfig.projectRoot, folder));
|
|
88
|
+
// ensure path is absolute
|
|
89
|
+
const ensureAbs = (folder) => {
|
|
90
|
+
if (!path_1.default.isAbsolute(folder)) {
|
|
91
|
+
return path_1.default.resolve(folder);
|
|
92
|
+
}
|
|
93
|
+
return folder;
|
|
94
|
+
};
|
|
95
|
+
if (cypressConfig.fixturesFolder) {
|
|
96
|
+
folders.push(ensureAbs(cypressConfig.fixturesFolder));
|
|
97
|
+
}
|
|
98
|
+
if (cypressConfig.supportFolder) {
|
|
99
|
+
folders.push(ensureAbs(cypressConfig.supportFolder));
|
|
100
|
+
}
|
|
101
|
+
// attempt to add directories based on spec pattern
|
|
102
|
+
let componentDirs = cypressConfig.specPattern || '';
|
|
103
|
+
// can be string or array
|
|
104
|
+
if (typeof componentDirs === 'string') {
|
|
105
|
+
componentDirs = [componentDirs];
|
|
106
|
+
}
|
|
107
|
+
const dirsFromSpecPattern = componentDirs.reduce((acc, curr) => {
|
|
108
|
+
// glob
|
|
109
|
+
if (curr.includes('*')) {
|
|
110
|
+
const parts = curr.slice(0, curr.indexOf('*') - 1);
|
|
111
|
+
const joined = parts.split(path_1.default.sep);
|
|
112
|
+
const dir = path_1.default.join(...joined);
|
|
113
|
+
return acc.concat(path_1.default.resolve(cypressConfig.projectRoot, dir));
|
|
114
|
+
}
|
|
115
|
+
return acc;
|
|
116
|
+
}, []);
|
|
117
|
+
return folders.concat(...dirsFromSpecPattern, path_1.default.resolve('cypress'));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Finds the ModuleScopePlugin plugin and adds given folder
|
|
121
|
+
* to that list. This allows react-scripts to import folders
|
|
122
|
+
* outside of the default "/src" folder.
|
|
123
|
+
* WARNING modifies the input webpack options argument.
|
|
124
|
+
* @see https://github.com/bahmutov/cypress-react-unit-test/issues/289
|
|
125
|
+
* @param {string} folderName Folder to add, should be absolute
|
|
126
|
+
*/
|
|
127
|
+
function allowModuleSourceInPlace(folderName, webpackConfig) {
|
|
128
|
+
if (!folderName) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (!webpackConfig.resolve) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (!Array.isArray(webpackConfig.resolve.plugins)) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const moduleSourcePlugin = webpackConfig.resolve.plugins.find((plugin) => {
|
|
138
|
+
return typeof plugin !== 'string' && Array.isArray(plugin.appSrcs);
|
|
139
|
+
});
|
|
140
|
+
if (!moduleSourcePlugin) {
|
|
141
|
+
debug('cannot find module source plugin');
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
debug('found module source plugin %o', moduleSourcePlugin);
|
|
145
|
+
if (!moduleSourcePlugin.appSrcs.includes(folderName)) {
|
|
146
|
+
moduleSourcePlugin.appSrcs.push(folderName);
|
|
147
|
+
debug('added folder %s to allowed sources', folderName);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Returns true if the provided loader path includes "babel-loader".
|
|
152
|
+
* Uses current OS path separator to split the loader path correctly.
|
|
153
|
+
*/
|
|
154
|
+
const isBabelLoader = (loaderPath) => {
|
|
155
|
+
if (!loaderPath) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const loaderPathParts = loaderPath.split(path_1.default.sep);
|
|
159
|
+
return loaderPathParts.some((pathPart) => pathPart === 'babel-loader');
|
|
160
|
+
};
|
|
161
|
+
const findBabelRule = (webpackConfig) => {
|
|
162
|
+
if (!webpackConfig) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!webpackConfig.module) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
169
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const oneOfRule = webpackConfig.module.rules.find((rule) => {
|
|
173
|
+
return typeof rule !== 'string' && Array.isArray(rule.oneOf);
|
|
174
|
+
});
|
|
175
|
+
if (!oneOfRule || !oneOfRule.oneOf) {
|
|
176
|
+
debug('could not find oneOfRule');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
debug('looking through oneOf rules');
|
|
180
|
+
debug('oneOfRule.oneOf %o', oneOfRule.oneOf);
|
|
181
|
+
oneOfRule.oneOf.forEach((rule) => debug('rule %o', rule));
|
|
182
|
+
const babelRule = oneOfRule.oneOf.find((rule) => rule.loader && isBabelLoader(rule.loader));
|
|
183
|
+
return babelRule;
|
|
184
|
+
};
|
|
185
|
+
// see https://github.com/bahmutov/find-webpack/issues/7
|
|
186
|
+
const findBabelLoaderRule = (webpackConfig) => {
|
|
187
|
+
debug('looking for babel-loader rule');
|
|
188
|
+
if (!webpackConfig) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!webpackConfig.module) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
195
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
debug('webpack module rules');
|
|
199
|
+
webpackConfig.module.rules.forEach((rule) => {
|
|
200
|
+
debug('rule %o', rule);
|
|
201
|
+
});
|
|
202
|
+
const babelRule = webpackConfig.module.rules.find((rule) => typeof rule !== 'string' && rule.loader === 'babel-loader');
|
|
203
|
+
if (!babelRule || !babelRule.test) {
|
|
204
|
+
debug('could not find babel rule');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
debug('found Babel rule that applies to %s', babelRule.test.toString());
|
|
208
|
+
return babelRule;
|
|
209
|
+
};
|
|
210
|
+
const findBabelLoaderUseRule = (webpackConfig) => {
|
|
211
|
+
var _a;
|
|
212
|
+
debug('looking for babel-loader rule with use property');
|
|
213
|
+
if (!webpackConfig) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!webpackConfig.module) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
220
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
debug('webpack module rules');
|
|
224
|
+
webpackConfig.module.rules.forEach((rule) => {
|
|
225
|
+
debug('rule %o', rule);
|
|
226
|
+
});
|
|
227
|
+
const isBabelLoader = (rule) => { var _a; return ((_a = rule === null || rule === void 0 ? void 0 : rule.use) === null || _a === void 0 ? void 0 : _a.loader) === 'babel-loader'; };
|
|
228
|
+
const isNextBabelLoader = (rule) => { var _a; return ((_a = rule === null || rule === void 0 ? void 0 : rule.use) === null || _a === void 0 ? void 0 : _a.loader) === 'next-babel-loader'; };
|
|
229
|
+
const babelRule = webpackConfig.module.rules.find((rule) => isBabelLoader(rule) || isNextBabelLoader(rule));
|
|
230
|
+
if (!babelRule) {
|
|
231
|
+
debug('could not find babel rule');
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
debug('found Babel use rule that applies to %s', (_a = babelRule.test) === null || _a === void 0 ? void 0 : _a.toString());
|
|
235
|
+
return babelRule.use;
|
|
236
|
+
};
|
|
237
|
+
const findBabelRuleWrap = (webpackConfig) => {
|
|
238
|
+
let babelRule = findBabelRule(webpackConfig);
|
|
239
|
+
if (!babelRule) {
|
|
240
|
+
debug('could not find Babel rule using oneOf');
|
|
241
|
+
babelRule = findBabelLoaderRule(webpackConfig);
|
|
242
|
+
}
|
|
243
|
+
if (!babelRule) {
|
|
244
|
+
debug('could not find Babel rule directly');
|
|
245
|
+
babelRule = findBabelLoaderUseRule(webpackConfig);
|
|
246
|
+
}
|
|
247
|
+
if (!babelRule) {
|
|
248
|
+
debug('could not find Babel rule');
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
return babelRule;
|
|
252
|
+
};
|
|
253
|
+
function addFolderToBabelLoaderTranspileInPlace(folder, webpackConfig) {
|
|
254
|
+
if (!folder) {
|
|
255
|
+
debug('no extra folder to transpile using Babel');
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
debug('trying to transpile additional folder %s using Babel', folder);
|
|
259
|
+
const babelRule = findBabelRuleWrap(webpackConfig);
|
|
260
|
+
if (!babelRule) {
|
|
261
|
+
debug('could not find Babel rule');
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
debug('babel rule %o', babelRule);
|
|
265
|
+
if (!babelRule.include) {
|
|
266
|
+
debug('could not find Babel include condition');
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (typeof babelRule.include === 'string') {
|
|
270
|
+
babelRule.include = [babelRule.include];
|
|
271
|
+
}
|
|
272
|
+
if (babelRule.include.includes(folder)) {
|
|
273
|
+
// do not double include the same folder
|
|
274
|
+
debug('babel includes rule for folder %s', folder);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
babelRule.include.push(folder);
|
|
278
|
+
debug('added folder %s to babel rules', folder);
|
|
279
|
+
}
|
|
280
|
+
function reactScriptsFiveModifications(webpackConfig) {
|
|
281
|
+
var _a;
|
|
282
|
+
// React-Scripts sets the webpack target to ["browserslist"] which tells
|
|
283
|
+
// webpack to target the browsers found within the browserslist config
|
|
284
|
+
// depending on the environment (process.env.NODE_ENV). Since we set
|
|
285
|
+
// process.env.NODE_ENV = "test", webpack is unable to find any browsers and errors.
|
|
286
|
+
// We set BROWSERSLIST_ENV = "development" to override the default NODE_ENV search of browsers.
|
|
287
|
+
if (!process.env.BROWSERSLIST_ENV) {
|
|
288
|
+
process.env.BROWSERSLIST_ENV = 'development';
|
|
289
|
+
}
|
|
290
|
+
// We use the "development" configuration of the react-scripts webpack config.
|
|
291
|
+
// There is a conflict when settings process.env.NODE_ENV = "test" since DefinePlugin
|
|
292
|
+
// uses the "development" configuration and expects process.env.NODE_ENV = "development".
|
|
293
|
+
const definePlugin = (_a = webpackConfig.plugins) === null || _a === void 0 ? void 0 : _a.find((plugin) => plugin.constructor.name === 'DefinePlugin');
|
|
294
|
+
if (definePlugin) {
|
|
295
|
+
const processEnv = definePlugin.definitions['process.env'];
|
|
296
|
+
processEnv.NODE_ENV = JSON.stringify('development');
|
|
297
|
+
debug('Found "DefinePlugin", modified "process.env" definition %o', processEnv);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
exports.reactScriptsFiveModifications = reactScriptsFiveModifications;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nextHandler = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
|
|
6
|
+
const module_1 = (0, tslib_1.__importDefault)(require("module"));
|
|
7
|
+
const fs = (0, tslib_1.__importStar)(require("fs"));
|
|
8
|
+
const path = (0, tslib_1.__importStar)(require("path"));
|
|
9
|
+
const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
|
|
10
|
+
const debug = (0, debug_1.default)('cypress:webpack-dev-server-fresh:nextHandler');
|
|
11
|
+
async function nextHandler(devServerConfig) {
|
|
12
|
+
const webpackConfig = await loadWebpackConfig(devServerConfig);
|
|
13
|
+
debug('resolved next.js webpack config %o', webpackConfig);
|
|
14
|
+
checkSWC(webpackConfig, devServerConfig.cypressConfig);
|
|
15
|
+
// Next webpack compiler ignored watching any node_modules changes, but we need to watch
|
|
16
|
+
// for changes to 'dist/browser.js' in order to detect new specs that have been added
|
|
17
|
+
if (webpackConfig.watchOptions && Array.isArray(webpackConfig.watchOptions.ignored)) {
|
|
18
|
+
webpackConfig.watchOptions = Object.assign(Object.assign({}, webpackConfig.watchOptions), { ignored: [...webpackConfig.watchOptions.ignored.filter((pattern) => !/node_modules/.test(pattern)), '**/node_modules/!(@cypress/webpack-dev-server/dist/browser.js)**'] });
|
|
19
|
+
debug('found options next.js watchOptions.ignored %O', webpackConfig.watchOptions.ignored);
|
|
20
|
+
}
|
|
21
|
+
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult: sourceNextWebpackDeps(devServerConfig) };
|
|
22
|
+
}
|
|
23
|
+
exports.nextHandler = nextHandler;
|
|
24
|
+
/**
|
|
25
|
+
* Acquire the modules needed to load the Next webpack config. We are using Next's APIs to grab the webpackConfig
|
|
26
|
+
* but since this is in the binary, we have to `require.resolve` them from the projectRoot
|
|
27
|
+
* `loadConfig` acquires the next.config.js
|
|
28
|
+
* `getNextJsBaseWebpackConfig` acquires the webpackConfig dependent on the next.config.js
|
|
29
|
+
*/
|
|
30
|
+
function getNextJsPackages(devServerConfig) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
const resolvePaths = { paths: [devServerConfig.cypressConfig.projectRoot] };
|
|
33
|
+
const packages = {};
|
|
34
|
+
try {
|
|
35
|
+
const loadConfigPath = require.resolve('next/dist/server/config', resolvePaths);
|
|
36
|
+
packages.loadConfig = require(loadConfigPath).default;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
throw new Error(`Failed to load "next/dist/server/config" with error: ${(_a = e.message) !== null && _a !== void 0 ? _a : e}`);
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const getNextJsBaseWebpackConfigPath = require.resolve('next/dist/build/webpack-config', resolvePaths);
|
|
43
|
+
packages.getNextJsBaseWebpackConfig = require(getNextJsBaseWebpackConfigPath).default;
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
throw new Error(`Failed to load "next/dist/build/webpack-config" with error: ${(_b = e.message) !== null && _b !== void 0 ? _b : e}`);
|
|
47
|
+
}
|
|
48
|
+
return packages;
|
|
49
|
+
}
|
|
50
|
+
async function loadWebpackConfig(devServerConfig) {
|
|
51
|
+
const { loadConfig, getNextJsBaseWebpackConfig } = getNextJsPackages(devServerConfig);
|
|
52
|
+
const nextConfig = await loadConfig('development', devServerConfig.cypressConfig.projectRoot);
|
|
53
|
+
const runWebpackSpan = getRunWebpackSpan(devServerConfig);
|
|
54
|
+
const webpackConfig = await getNextJsBaseWebpackConfig(devServerConfig.cypressConfig.projectRoot, Object.assign(Object.assign({ buildId: `@cypress/react-${Math.random().toString()}`, config: nextConfig, dev: true, pagesDir: findPagesDir(devServerConfig.cypressConfig.projectRoot), entrypoints: {}, rewrites: { fallback: [], afterFiles: [], beforeFiles: [] } }, runWebpackSpan), {
|
|
55
|
+
// Client webpack config for Next.js <= 12.1.5
|
|
56
|
+
isServer: false,
|
|
57
|
+
// Client webpack config for Next.js > 12.1.5
|
|
58
|
+
compilerType: 'client' }));
|
|
59
|
+
return webpackConfig;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Check if Next is using the SWC compiler. Compilation will fail if user has `nodeVersion: "bundled"` set
|
|
63
|
+
* due to SWC certificate issues.
|
|
64
|
+
*/
|
|
65
|
+
function checkSWC(webpackConfig, cypressConfig) {
|
|
66
|
+
var _a, _b;
|
|
67
|
+
const hasSWCLoader = (_b = (_a = webpackConfig.module) === null || _a === void 0 ? void 0 : _a.rules) === null || _b === void 0 ? void 0 : _b.some((rule) => {
|
|
68
|
+
var _a;
|
|
69
|
+
return typeof rule !== 'string' && ((_a = rule.oneOf) === null || _a === void 0 ? void 0 : _a.some((oneOf) => { var _a; return ((_a = oneOf.use) === null || _a === void 0 ? void 0 : _a.loader) === 'next-swc-loader'; }));
|
|
70
|
+
});
|
|
71
|
+
// "resolvedNodePath" is only set when using the user's Node.js, which is required to compile Next.js with SWC optimizations
|
|
72
|
+
// If it is not set, they have either explicitly set "nodeVersion" to "bundled" or are are using Cypress < 9.0.0 where it was set to "bundled" by default
|
|
73
|
+
if (hasSWCLoader && cypressConfig.nodeVersion === 'bundled') {
|
|
74
|
+
throw new Error(`Cypress cannot compile your Next.js application when "nodeVersion" is set to "bundled". Please remove this option from your Cypress configuration file.`);
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const existsSync = (file) => {
|
|
79
|
+
try {
|
|
80
|
+
fs.accessSync(file, fs.constants.F_OK);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
catch (_) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Next allows the `pages` directory to be located at either
|
|
89
|
+
* `${projectRoot}/pages` or `${projectRoot}/src/pages`.
|
|
90
|
+
* If neither is found, return projectRoot
|
|
91
|
+
*/
|
|
92
|
+
function findPagesDir(projectRoot) {
|
|
93
|
+
// prioritize ./pages over ./src/pages
|
|
94
|
+
let pagesDir = path.join(projectRoot, 'pages');
|
|
95
|
+
if (existsSync(pagesDir)) {
|
|
96
|
+
return pagesDir;
|
|
97
|
+
}
|
|
98
|
+
pagesDir = path.join(projectRoot, 'src', 'pages');
|
|
99
|
+
if (existsSync(pagesDir)) {
|
|
100
|
+
return pagesDir;
|
|
101
|
+
}
|
|
102
|
+
return projectRoot;
|
|
103
|
+
}
|
|
104
|
+
// Starting with v11.1.1, a trace is required.
|
|
105
|
+
// 'next/dist/telemetry/trace/trace' only exists since v10.0.9
|
|
106
|
+
// and our peerDeps support back to v8 so try-catch this import
|
|
107
|
+
// Starting from 12.0 trace is now located in 'next/dist/trace/trace'
|
|
108
|
+
function getRunWebpackSpan(devServerConfig) {
|
|
109
|
+
let trace;
|
|
110
|
+
try {
|
|
111
|
+
try {
|
|
112
|
+
const traceImportPath = require.resolve('next/dist/telemetry/trace/trace', { paths: [devServerConfig.cypressConfig.projectRoot] });
|
|
113
|
+
trace = require(traceImportPath).trace;
|
|
114
|
+
return { runWebpackSpan: trace('cypress') };
|
|
115
|
+
}
|
|
116
|
+
catch (_) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
const traceImportPath = require.resolve('next/dist/trace/trace', { paths: [devServerConfig.cypressConfig.projectRoot] });
|
|
119
|
+
trace = require(traceImportPath).trace;
|
|
120
|
+
return { runWebpackSpan: trace('cypress') };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (_) {
|
|
124
|
+
return {};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const originalModuleLoad = module_1.default._load;
|
|
128
|
+
function sourceNextWebpackDeps(devServerConfig) {
|
|
129
|
+
const framework = (0, sourceRelativeWebpackModules_1.sourceFramework)(devServerConfig);
|
|
130
|
+
const webpack = sourceNextWebpack(devServerConfig, framework);
|
|
131
|
+
const webpackDevServer = (0, sourceRelativeWebpackModules_1.sourceWebpackDevServer)(devServerConfig, framework);
|
|
132
|
+
const htmlWebpackPlugin = (0, sourceRelativeWebpackModules_1.sourceHtmlWebpackPlugin)(devServerConfig, framework, webpack);
|
|
133
|
+
return {
|
|
134
|
+
framework,
|
|
135
|
+
webpack,
|
|
136
|
+
webpackDevServer,
|
|
137
|
+
htmlWebpackPlugin,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function sourceNextWebpack(devServerConfig, framework) {
|
|
141
|
+
const searchRoot = framework.importPath;
|
|
142
|
+
debug('NextWebpack: Attempting to load NextWebpack from %s', searchRoot);
|
|
143
|
+
let webpackJsonPath;
|
|
144
|
+
const webpack = {};
|
|
145
|
+
try {
|
|
146
|
+
webpackJsonPath = require.resolve('next/dist/compiled/webpack/package.json', {
|
|
147
|
+
paths: [searchRoot],
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
debug('NextWebpack: Failed to load NextWebpack - %s', e);
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
// Next 11 allows the choice of webpack@4 or webpack@5, depending on the "webpack5" property in their next.config.js
|
|
155
|
+
// The webpackModule.init" for Next 11 returns a webpack@4 or webpack@4 compiler instance based on this boolean
|
|
156
|
+
let webpack5 = true;
|
|
157
|
+
const importPath = path.join(path.dirname(webpackJsonPath), 'webpack.js');
|
|
158
|
+
const webpackModule = require(importPath);
|
|
159
|
+
try {
|
|
160
|
+
const nextConfig = require(path.resolve(devServerConfig.cypressConfig.projectRoot, 'next.config.js'));
|
|
161
|
+
debug('NextWebpack: next.config.js found - %o', nextConfig);
|
|
162
|
+
if (nextConfig.webpack5 === false) {
|
|
163
|
+
webpack5 = false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
// No next.config.js, assume webpack 5
|
|
168
|
+
}
|
|
169
|
+
debug('NextWebpack: webpack5 - %s', webpack5);
|
|
170
|
+
webpackModule.init(webpack5);
|
|
171
|
+
const packageJson = require(webpackJsonPath);
|
|
172
|
+
webpack.importPath = importPath;
|
|
173
|
+
// The package.json of "next/dist/compiled/webpack/package.json" has no version so we supply the version for later use
|
|
174
|
+
webpack.packageJson = Object.assign(Object.assign({}, packageJson), { version: webpack5 ? '5' : '4' });
|
|
175
|
+
webpack.module = webpackModule.webpack;
|
|
176
|
+
webpack.majorVersion = (0, sourceRelativeWebpackModules_1.getMajorVersion)(webpack.packageJson, [4, 5]);
|
|
177
|
+
debug('NextWebpack: Successfully loaded NextWebpack - %o', webpack);
|
|
178
|
+
module_1.default._load = function (request, parent, isMain) {
|
|
179
|
+
// Next with webpack@4 doesn't ship certain dependencies that HtmlWebpackPlugin requires, so we patch the resolution through to our bundled version
|
|
180
|
+
if ((request === 'webpack' || request.startsWith('webpack/')) && webpack.majorVersion === 4) {
|
|
181
|
+
const resolvePath = require.resolve(request, {
|
|
182
|
+
paths: [(0, sourceRelativeWebpackModules_1.cypressWebpackPath)(devServerConfig)],
|
|
183
|
+
});
|
|
184
|
+
debug('NextWebpack: Module._load for webpack@4 - %s', resolvePath);
|
|
185
|
+
return originalModuleLoad(resolvePath, parent, isMain);
|
|
186
|
+
}
|
|
187
|
+
if (request === 'webpack' || request.startsWith('webpack/')) {
|
|
188
|
+
const resolvePath = require.resolve(request, {
|
|
189
|
+
paths: [framework.importPath],
|
|
190
|
+
});
|
|
191
|
+
debug('NextWebpack: Module._load - %s', resolvePath);
|
|
192
|
+
return originalModuleLoad(resolvePath, parent, isMain);
|
|
193
|
+
}
|
|
194
|
+
return originalModuleLoad(request, parent, isMain);
|
|
195
|
+
};
|
|
196
|
+
return webpack;
|
|
197
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nuxtHandler = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
|
|
6
|
+
const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
|
|
7
|
+
const debug = (0, debug_1.default)('cypress:webpack-dev-server:nuxtHandler');
|
|
8
|
+
async function nuxtHandler(devServerConfig) {
|
|
9
|
+
const sourceWebpackModulesResult = (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig);
|
|
10
|
+
try {
|
|
11
|
+
const nuxt = require.resolve('nuxt', {
|
|
12
|
+
paths: [devServerConfig.cypressConfig.projectRoot],
|
|
13
|
+
});
|
|
14
|
+
const { getWebpackConfig } = require(nuxt);
|
|
15
|
+
const webpackConfig = await getWebpackConfig();
|
|
16
|
+
// Nuxt has asset size warnings configured by default which will cause webpack overlays to appear
|
|
17
|
+
// in the browser which we don't want.
|
|
18
|
+
delete webpackConfig.performance;
|
|
19
|
+
debug('webpack config %o', webpackConfig);
|
|
20
|
+
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult };
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
console.error(e); // eslint-disable-line no-console
|
|
24
|
+
throw Error(`Error loading nuxt. Looked in ${require.resolve.paths(devServerConfig.cypressConfig.projectRoot)}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.nuxtHandler = nuxtHandler;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import Module from 'module';
|
|
3
|
+
import type { WebpackDevServerConfig } from '../devServer';
|
|
4
|
+
export declare type ModuleClass = typeof Module & {
|
|
5
|
+
_load(id: string, parent: Module, isMain: boolean): any;
|
|
6
|
+
_resolveFilename(request: string, parent: Module, isMain: boolean, options?: {
|
|
7
|
+
paths: string[];
|
|
8
|
+
}): string;
|
|
9
|
+
_cache: Record<string, Module>;
|
|
10
|
+
};
|
|
11
|
+
export interface PackageJson {
|
|
12
|
+
name: string;
|
|
13
|
+
version: string;
|
|
14
|
+
}
|
|
15
|
+
export interface SourcedDependency {
|
|
16
|
+
importPath: string;
|
|
17
|
+
packageJson: PackageJson;
|
|
18
|
+
}
|
|
19
|
+
export interface SourcedWebpack extends SourcedDependency {
|
|
20
|
+
module: Function;
|
|
21
|
+
majorVersion: 4 | 5;
|
|
22
|
+
}
|
|
23
|
+
export interface SourcedWebpackDevServer extends SourcedDependency {
|
|
24
|
+
module: {
|
|
25
|
+
new (...args: unknown[]): unknown;
|
|
26
|
+
};
|
|
27
|
+
majorVersion: 3 | 4;
|
|
28
|
+
}
|
|
29
|
+
export interface SourcedHtmlWebpackPlugin extends SourcedDependency {
|
|
30
|
+
module: unknown;
|
|
31
|
+
majorVersion: 4 | 5;
|
|
32
|
+
}
|
|
33
|
+
export interface SourceRelativeWebpackResult {
|
|
34
|
+
framework: SourcedDependency | null;
|
|
35
|
+
webpack: SourcedWebpack;
|
|
36
|
+
webpackDevServer: SourcedWebpackDevServer;
|
|
37
|
+
htmlWebpackPlugin: SourcedHtmlWebpackPlugin;
|
|
38
|
+
}
|
|
39
|
+
export declare const cypressWebpackPath: (config: WebpackDevServerConfig) => string;
|
|
40
|
+
export declare function sourceFramework(config: WebpackDevServerConfig): SourcedDependency | null;
|
|
41
|
+
export declare function sourceWebpack(config: WebpackDevServerConfig, framework: SourcedDependency | null): SourcedWebpack;
|
|
42
|
+
export declare function sourceWebpackDevServer(config: WebpackDevServerConfig, framework?: SourcedDependency | null): SourcedWebpackDevServer;
|
|
43
|
+
export declare function sourceHtmlWebpackPlugin(config: WebpackDevServerConfig, framework: SourcedDependency | null, webpack: SourcedWebpack): SourcedHtmlWebpackPlugin;
|
|
44
|
+
export declare function sourceDefaultWebpackDependencies(config: WebpackDevServerConfig): SourceRelativeWebpackResult;
|
|
45
|
+
export declare function getMajorVersion<T extends number>(json: PackageJson, acceptedVersions: T[]): T;
|
|
46
|
+
export declare function restoreLoadHook(): void;
|