@cypress/webpack-dev-server 1.8.4 → 2.2.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/README.md +54 -75
- package/dist/CypressCTWebpackPlugin.d.ts +15 -0
- package/dist/CypressCTWebpackPlugin.js +105 -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 +106 -0
- package/dist/helpers/angularHandler.d.ts +80 -0
- package/dist/helpers/angularHandler.js +131 -0
- package/dist/helpers/createReactAppHandler.d.ts +8 -0
- package/dist/helpers/createReactAppHandler.js +300 -0
- package/dist/helpers/nextHandler.d.ts +3 -0
- package/dist/helpers/nextHandler.js +236 -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 +211 -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 +8 -28
- package/dist/makeDefaultWebpackConfig.d.ts +1 -0
- package/dist/makeDefaultWebpackConfig.js +45 -0
- package/dist/makeWebpackConfig.d.ts +7 -16
- package/dist/makeWebpackConfig.js +127 -94
- package/dist/measureWebpackPerformance.d.ts +1 -2
- package/dist/measureWebpackPerformance.js +11 -8
- package/package.json +34 -26
- package/CHANGELOG.md +0 -188
- 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,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.allCssTests = 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
|
+
watchEntryPoint(webpackConfig);
|
|
16
|
+
allowGlobalStylesImports(webpackConfig);
|
|
17
|
+
changeNextCachePath(webpackConfig);
|
|
18
|
+
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult: sourceNextWebpackDeps(devServerConfig) };
|
|
19
|
+
}
|
|
20
|
+
exports.nextHandler = nextHandler;
|
|
21
|
+
/**
|
|
22
|
+
* Acquire the modules needed to load the Next webpack config. We are using Next's APIs to grab the webpackConfig
|
|
23
|
+
* but since this is in the binary, we have to `require.resolve` them from the projectRoot
|
|
24
|
+
* `loadConfig` acquires the next.config.js
|
|
25
|
+
* `getNextJsBaseWebpackConfig` acquires the webpackConfig dependent on the next.config.js
|
|
26
|
+
*/
|
|
27
|
+
function getNextJsPackages(devServerConfig) {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
const resolvePaths = { paths: [devServerConfig.cypressConfig.projectRoot] };
|
|
30
|
+
const packages = {};
|
|
31
|
+
try {
|
|
32
|
+
const loadConfigPath = require.resolve('next/dist/server/config', resolvePaths);
|
|
33
|
+
packages.loadConfig = require(loadConfigPath).default;
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw new Error(`Failed to load "next/dist/server/config" with error: ${(_a = e.message) !== null && _a !== void 0 ? _a : e}`);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const getNextJsBaseWebpackConfigPath = require.resolve('next/dist/build/webpack-config', resolvePaths);
|
|
40
|
+
packages.getNextJsBaseWebpackConfig = require(getNextJsBaseWebpackConfigPath).default;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new Error(`Failed to load "next/dist/build/webpack-config" with error: ${(_b = e.message) !== null && _b !== void 0 ? _b : e}`);
|
|
44
|
+
}
|
|
45
|
+
return packages;
|
|
46
|
+
}
|
|
47
|
+
async function loadWebpackConfig(devServerConfig) {
|
|
48
|
+
const { loadConfig, getNextJsBaseWebpackConfig } = getNextJsPackages(devServerConfig);
|
|
49
|
+
const nextConfig = await loadConfig('development', devServerConfig.cypressConfig.projectRoot);
|
|
50
|
+
const runWebpackSpan = getRunWebpackSpan(devServerConfig);
|
|
51
|
+
const webpackConfig = await getNextJsBaseWebpackConfig(devServerConfig.cypressConfig.projectRoot, Object.assign(Object.assign({ buildId: `@cypress/react-${Math.random().toString()}`, config: nextConfig, dev: true, pagesDir: await findPagesDir(devServerConfig.cypressConfig.projectRoot), entrypoints: {}, rewrites: { fallback: [], afterFiles: [], beforeFiles: [] } }, runWebpackSpan), {
|
|
52
|
+
// Client webpack config for Next.js <= 12.1.5
|
|
53
|
+
isServer: false,
|
|
54
|
+
// Client webpack config for Next.js > 12.1.5
|
|
55
|
+
compilerType: 'client' }));
|
|
56
|
+
return webpackConfig;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check if Next is using the SWC compiler. Compilation will fail if user has `nodeVersion: "bundled"` set
|
|
60
|
+
* due to SWC certificate issues.
|
|
61
|
+
*/
|
|
62
|
+
function checkSWC(webpackConfig, cypressConfig) {
|
|
63
|
+
var _a, _b;
|
|
64
|
+
const hasSWCLoader = (_b = (_a = webpackConfig.module) === null || _a === void 0 ? void 0 : _a.rules) === null || _b === void 0 ? void 0 : _b.some((rule) => {
|
|
65
|
+
var _a;
|
|
66
|
+
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'; }));
|
|
67
|
+
});
|
|
68
|
+
// "resolvedNodePath" is only set when using the user's Node.js, which is required to compile Next.js with SWC optimizations
|
|
69
|
+
// 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
|
|
70
|
+
if (hasSWCLoader && cypressConfig.nodeVersion === 'bundled') {
|
|
71
|
+
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.`);
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const exists = async (file) => {
|
|
76
|
+
try {
|
|
77
|
+
await fs.promises.access(file, fs.constants.F_OK);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch (_) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Next allows the `pages` directory to be located at either
|
|
86
|
+
* `${projectRoot}/pages` or `${projectRoot}/src/pages`.
|
|
87
|
+
* If neither is found, return projectRoot
|
|
88
|
+
*/
|
|
89
|
+
async function findPagesDir(projectRoot) {
|
|
90
|
+
// prioritize ./pages over ./src/pages
|
|
91
|
+
let pagesDir = path.join(projectRoot, 'pages');
|
|
92
|
+
if (await exists(pagesDir)) {
|
|
93
|
+
return pagesDir;
|
|
94
|
+
}
|
|
95
|
+
pagesDir = path.join(projectRoot, 'src', 'pages');
|
|
96
|
+
if (await exists(pagesDir)) {
|
|
97
|
+
return pagesDir;
|
|
98
|
+
}
|
|
99
|
+
return projectRoot;
|
|
100
|
+
}
|
|
101
|
+
// Starting with v11.1.1, a trace is required.
|
|
102
|
+
// 'next/dist/telemetry/trace/trace' only exists since v10.0.9
|
|
103
|
+
// and our peerDeps support back to v8 so try-catch this import
|
|
104
|
+
// Starting from 12.0 trace is now located in 'next/dist/trace/trace'
|
|
105
|
+
function getRunWebpackSpan(devServerConfig) {
|
|
106
|
+
let trace;
|
|
107
|
+
try {
|
|
108
|
+
try {
|
|
109
|
+
const traceImportPath = require.resolve('next/dist/telemetry/trace/trace', { paths: [devServerConfig.cypressConfig.projectRoot] });
|
|
110
|
+
trace = require(traceImportPath).trace;
|
|
111
|
+
return { runWebpackSpan: trace('cypress') };
|
|
112
|
+
}
|
|
113
|
+
catch (_) {
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
const traceImportPath = require.resolve('next/dist/trace/trace', { paths: [devServerConfig.cypressConfig.projectRoot] });
|
|
116
|
+
trace = require(traceImportPath).trace;
|
|
117
|
+
return { runWebpackSpan: trace('cypress') };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (_) {
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const originalModuleLoad = module_1.default._load;
|
|
125
|
+
function sourceNextWebpackDeps(devServerConfig) {
|
|
126
|
+
const framework = (0, sourceRelativeWebpackModules_1.sourceFramework)(devServerConfig);
|
|
127
|
+
const webpack = sourceNextWebpack(devServerConfig, framework);
|
|
128
|
+
const webpackDevServer = (0, sourceRelativeWebpackModules_1.sourceWebpackDevServer)(devServerConfig, framework);
|
|
129
|
+
const htmlWebpackPlugin = (0, sourceRelativeWebpackModules_1.sourceHtmlWebpackPlugin)(devServerConfig, framework, webpack);
|
|
130
|
+
return {
|
|
131
|
+
framework,
|
|
132
|
+
webpack,
|
|
133
|
+
webpackDevServer,
|
|
134
|
+
htmlWebpackPlugin,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function sourceNextWebpack(devServerConfig, framework) {
|
|
138
|
+
const searchRoot = framework.importPath;
|
|
139
|
+
debug('NextWebpack: Attempting to load NextWebpack from %s', searchRoot);
|
|
140
|
+
let webpackJsonPath;
|
|
141
|
+
const webpack = {};
|
|
142
|
+
try {
|
|
143
|
+
webpackJsonPath = require.resolve('next/dist/compiled/webpack/package.json', {
|
|
144
|
+
paths: [searchRoot],
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
debug('NextWebpack: Failed to load NextWebpack - %s', e);
|
|
149
|
+
throw e;
|
|
150
|
+
}
|
|
151
|
+
// Next 11 allows the choice of webpack@4 or webpack@5, depending on the "webpack5" property in their next.config.js
|
|
152
|
+
// The webpackModule.init" for Next 11 returns a webpack@4 or webpack@4 compiler instance based on this boolean
|
|
153
|
+
let webpack5 = true;
|
|
154
|
+
const importPath = path.join(path.dirname(webpackJsonPath), 'webpack.js');
|
|
155
|
+
const webpackModule = require(importPath);
|
|
156
|
+
try {
|
|
157
|
+
const nextConfig = require(path.resolve(devServerConfig.cypressConfig.projectRoot, 'next.config.js'));
|
|
158
|
+
debug('NextWebpack: next.config.js found - %o', nextConfig);
|
|
159
|
+
if (nextConfig.webpack5 === false) {
|
|
160
|
+
webpack5 = false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
// No next.config.js, assume webpack 5
|
|
165
|
+
}
|
|
166
|
+
debug('NextWebpack: webpack5 - %s', webpack5);
|
|
167
|
+
webpackModule.init(webpack5);
|
|
168
|
+
const packageJson = require(webpackJsonPath);
|
|
169
|
+
webpack.importPath = importPath;
|
|
170
|
+
// The package.json of "next/dist/compiled/webpack/package.json" has no version so we supply the version for later use
|
|
171
|
+
webpack.packageJson = Object.assign(Object.assign({}, packageJson), { version: webpack5 ? '5' : '4' });
|
|
172
|
+
webpack.module = webpackModule.webpack;
|
|
173
|
+
webpack.majorVersion = (0, sourceRelativeWebpackModules_1.getMajorVersion)(webpack.packageJson, [4, 5]);
|
|
174
|
+
debug('NextWebpack: Successfully loaded NextWebpack - %o', webpack);
|
|
175
|
+
module_1.default._load = function (request, parent, isMain) {
|
|
176
|
+
// Next with webpack@4 doesn't ship certain dependencies that HtmlWebpackPlugin requires, so we patch the resolution through to our bundled version
|
|
177
|
+
if ((request === 'webpack' || request.startsWith('webpack/')) && webpack.majorVersion === 4) {
|
|
178
|
+
const resolvePath = require.resolve(request, {
|
|
179
|
+
paths: [(0, sourceRelativeWebpackModules_1.cypressWebpackPath)(devServerConfig)],
|
|
180
|
+
});
|
|
181
|
+
debug('NextWebpack: Module._load for webpack@4 - %s', resolvePath);
|
|
182
|
+
return originalModuleLoad(resolvePath, parent, isMain);
|
|
183
|
+
}
|
|
184
|
+
if (request === 'webpack' || request.startsWith('webpack/')) {
|
|
185
|
+
const resolvePath = require.resolve(request, {
|
|
186
|
+
paths: [framework.importPath],
|
|
187
|
+
});
|
|
188
|
+
debug('NextWebpack: Module._load - %s', resolvePath);
|
|
189
|
+
return originalModuleLoad(resolvePath, parent, isMain);
|
|
190
|
+
}
|
|
191
|
+
return originalModuleLoad(request, parent, isMain);
|
|
192
|
+
};
|
|
193
|
+
return webpack;
|
|
194
|
+
}
|
|
195
|
+
// Next webpack compiler ignored watching any node_modules changes, but we need to watch
|
|
196
|
+
// for changes to 'dist/browser.js' in order to detect new specs that have been added
|
|
197
|
+
function watchEntryPoint(webpackConfig) {
|
|
198
|
+
if (webpackConfig.watchOptions && Array.isArray(webpackConfig.watchOptions.ignored)) {
|
|
199
|
+
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)**'] });
|
|
200
|
+
debug('found options next.js watchOptions.ignored %O', webpackConfig.watchOptions.ignored);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// We are matching the Next.js regex rules exactly. If we were writing our own loader, we could
|
|
204
|
+
// condense these regex rules into a single rule but we need the regex.source to be identical to what
|
|
205
|
+
// we get from Next.js webpack config
|
|
206
|
+
// see: https://github.com/vercel/next.js/blob/20486c159d8538a337da6b07b0b4490a3a0d6b91/packages/next/build/webpack/config/blocks/css/index.ts#L18
|
|
207
|
+
const globalCssRe = [/(?<!\.module)\.css$/, /(?<!\.module)\.(scss|sass)$/];
|
|
208
|
+
const globalCssModulesRe = [/\.module\.css$/, /\.module\.(scss|sass)$/];
|
|
209
|
+
exports.allCssTests = [...globalCssRe, ...globalCssModulesRe];
|
|
210
|
+
// Next does not allow global styles to be loaded outside of the main <App /> component.
|
|
211
|
+
// We want users to be able to import the global styles into their component support file so we
|
|
212
|
+
// delete the "issuer" from the rules that process css/scss files.
|
|
213
|
+
// see: https://github.com/cypress-io/cypress/issues/22525
|
|
214
|
+
// Motivated by: https://github.com/bem/next-global-css
|
|
215
|
+
function allowGlobalStylesImports(webpackConfig) {
|
|
216
|
+
var _a;
|
|
217
|
+
const rules = ((_a = webpackConfig.module) === null || _a === void 0 ? void 0 : _a.rules) || [];
|
|
218
|
+
for (const rule of rules) {
|
|
219
|
+
if (typeof rule !== 'string' && rule.oneOf) {
|
|
220
|
+
for (const oneOf of rule.oneOf) {
|
|
221
|
+
if (oneOf.test && exports.allCssTests.some((re) => { var _a; return re.source === ((_a = oneOf.test) === null || _a === void 0 ? void 0 : _a.source); })) {
|
|
222
|
+
delete oneOf.issuer;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Our modifications of the Next webpack config can corrupt the cache used for local development.
|
|
229
|
+
// We separate the cache used for CT from the normal cache (".next/cache/webpack" -> ".next/cache/cypress-webpack") so they don't interfere with each other
|
|
230
|
+
function changeNextCachePath(webpackConfig) {
|
|
231
|
+
if (typeof webpackConfig.cache === 'object' && ('cacheDirectory' in webpackConfig.cache) && webpackConfig.cache.cacheDirectory) {
|
|
232
|
+
const { cacheDirectory } = webpackConfig.cache;
|
|
233
|
+
webpackConfig.cache.cacheDirectory = cacheDirectory.replace(/webpack$/, 'cypress-webpack');
|
|
234
|
+
debug('Changing Next cache path from %s to %s', cacheDirectory, webpackConfig.cache.cacheDirectory);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.restoreLoadHook = exports.getMajorVersion = exports.sourceDefaultWebpackDependencies = exports.sourceHtmlWebpackPlugin = exports.sourceWebpackDevServer = exports.sourceWebpack = exports.sourceFramework = exports.cypressWebpackPath = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const module_1 = (0, tslib_1.__importDefault)(require("module"));
|
|
6
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
7
|
+
const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('cypress:webpack-dev-server:sourceRelativeWebpackModules');
|
|
9
|
+
const originalModuleLoad = module_1.default._load;
|
|
10
|
+
const originalModuleResolveFilename = module_1.default._resolveFilename;
|
|
11
|
+
// We ship webpack@4 as part of '@cypress/webpack-batteries-included-preprocessor'. The path to this module
|
|
12
|
+
// serves as our fallback.
|
|
13
|
+
const cypressWebpackPath = (config) => {
|
|
14
|
+
return require.resolve('@cypress/webpack-batteries-included-preprocessor', {
|
|
15
|
+
paths: [config.cypressConfig.cypressBinaryRoot],
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
exports.cypressWebpackPath = cypressWebpackPath;
|
|
19
|
+
const frameworkWebpackMapper = {
|
|
20
|
+
'create-react-app': 'react-scripts',
|
|
21
|
+
'vue-cli': '@vue/cli-service',
|
|
22
|
+
'nuxt': '@nuxt/webpack',
|
|
23
|
+
react: undefined,
|
|
24
|
+
vue: undefined,
|
|
25
|
+
next: 'next',
|
|
26
|
+
'angular': '@angular-devkit/build-angular',
|
|
27
|
+
};
|
|
28
|
+
// Source the users framework from the provided projectRoot. The framework, if available, will serve
|
|
29
|
+
// as the resolve base for webpack dependency resolution.
|
|
30
|
+
function sourceFramework(config) {
|
|
31
|
+
debug('Framework: Attempting to source framework for %s', config.cypressConfig.projectRoot);
|
|
32
|
+
if (!config.framework) {
|
|
33
|
+
debug('Framework: No framework provided');
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const sourceOfWebpack = frameworkWebpackMapper[config.framework];
|
|
37
|
+
if (!sourceOfWebpack) {
|
|
38
|
+
debug('Not a higher-order framework so webpack dependencies should be resolvable from projectRoot');
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const framework = {};
|
|
42
|
+
try {
|
|
43
|
+
const frameworkJsonPath = require.resolve(`${sourceOfWebpack}/package.json`, {
|
|
44
|
+
paths: [config.cypressConfig.projectRoot],
|
|
45
|
+
});
|
|
46
|
+
const frameworkPathRoot = path_1.default.dirname(frameworkJsonPath);
|
|
47
|
+
// Want to make sure we're sourcing this from the user's code. Otherwise we can
|
|
48
|
+
// warn and tell them they don't have their dependencies installed
|
|
49
|
+
framework.importPath = frameworkPathRoot;
|
|
50
|
+
framework.packageJson = require(frameworkJsonPath);
|
|
51
|
+
debug('Framework: Successfully sourced framework - %o', framework);
|
|
52
|
+
return framework;
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
debug('Framework: Failed to source framework - %s', e);
|
|
56
|
+
// TODO
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.sourceFramework = sourceFramework;
|
|
61
|
+
// Source the webpack module from the provided framework or projectRoot. We override the module resolution
|
|
62
|
+
// so that other packages that import webpack resolve to the version we found.
|
|
63
|
+
// If none is found, we fallback to the bundled version in '@cypress/webpack-batteries-included-preprocessor'.
|
|
64
|
+
function sourceWebpack(config, framework) {
|
|
65
|
+
var _a;
|
|
66
|
+
const searchRoot = (_a = framework === null || framework === void 0 ? void 0 : framework.importPath) !== null && _a !== void 0 ? _a : config.cypressConfig.projectRoot;
|
|
67
|
+
debug('Webpack: Attempting to source webpack from %s', searchRoot);
|
|
68
|
+
const webpack = {};
|
|
69
|
+
let webpackJsonPath;
|
|
70
|
+
try {
|
|
71
|
+
webpackJsonPath = require.resolve('webpack/package.json', {
|
|
72
|
+
paths: [searchRoot],
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
77
|
+
debug('Webpack: Failed to source webpack - %s', e);
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
debug('Webpack: Falling back to bundled version');
|
|
81
|
+
webpackJsonPath = require.resolve('webpack/package.json', {
|
|
82
|
+
paths: [(0, exports.cypressWebpackPath)(config)],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
webpack.importPath = path_1.default.dirname(webpackJsonPath);
|
|
86
|
+
webpack.packageJson = require(webpackJsonPath);
|
|
87
|
+
webpack.module = require(webpack.importPath);
|
|
88
|
+
webpack.majorVersion = getMajorVersion(webpack.packageJson, [4, 5]);
|
|
89
|
+
debug('Webpack: Successfully sourced webpack - %o', webpack);
|
|
90
|
+
module_1.default._load = function (request, parent, isMain) {
|
|
91
|
+
if (request === 'webpack' || request.startsWith('webpack/')) {
|
|
92
|
+
const resolvePath = require.resolve(request, {
|
|
93
|
+
paths: [webpack.importPath],
|
|
94
|
+
});
|
|
95
|
+
debug('Webpack: Module._load resolvePath - %s', resolvePath);
|
|
96
|
+
return originalModuleLoad(resolvePath, parent, isMain);
|
|
97
|
+
}
|
|
98
|
+
return originalModuleLoad(request, parent, isMain);
|
|
99
|
+
};
|
|
100
|
+
module_1.default._resolveFilename = function (request, parent, isMain, options) {
|
|
101
|
+
if (request === 'webpack' || request.startsWith('webpack/') && !(options === null || options === void 0 ? void 0 : options.paths)) {
|
|
102
|
+
const resolveFilename = originalModuleResolveFilename(request, parent, isMain, {
|
|
103
|
+
paths: [webpack.importPath],
|
|
104
|
+
});
|
|
105
|
+
debug('Webpack: Module._resolveFilename resolveFilename - %s', resolveFilename);
|
|
106
|
+
return resolveFilename;
|
|
107
|
+
}
|
|
108
|
+
return originalModuleResolveFilename(request, parent, isMain, options);
|
|
109
|
+
};
|
|
110
|
+
return webpack;
|
|
111
|
+
}
|
|
112
|
+
exports.sourceWebpack = sourceWebpack;
|
|
113
|
+
// Source the webpack-dev-server module from the provided framework or projectRoot.
|
|
114
|
+
// If none is found, we fallback to the version bundled with this package.
|
|
115
|
+
function sourceWebpackDevServer(config, framework) {
|
|
116
|
+
var _a;
|
|
117
|
+
const searchRoot = (_a = framework === null || framework === void 0 ? void 0 : framework.importPath) !== null && _a !== void 0 ? _a : config.cypressConfig.projectRoot;
|
|
118
|
+
debug('WebpackDevServer: Attempting to source webpack-dev-server from %s', searchRoot);
|
|
119
|
+
const webpackDevServer = {};
|
|
120
|
+
let webpackDevServerJsonPath;
|
|
121
|
+
try {
|
|
122
|
+
webpackDevServerJsonPath = require.resolve('webpack-dev-server/package.json', {
|
|
123
|
+
paths: [searchRoot],
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
128
|
+
debug('WebpackDevServer: Failed to source webpack-dev-server - %s', e);
|
|
129
|
+
throw e;
|
|
130
|
+
}
|
|
131
|
+
debug('WebpackDevServer: Falling back to bundled version');
|
|
132
|
+
webpackDevServerJsonPath = require.resolve('webpack-dev-server/package.json', {
|
|
133
|
+
paths: [__dirname],
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
webpackDevServer.importPath = path_1.default.dirname(webpackDevServerJsonPath);
|
|
137
|
+
webpackDevServer.packageJson = require(webpackDevServerJsonPath);
|
|
138
|
+
webpackDevServer.module = require(webpackDevServer.importPath);
|
|
139
|
+
webpackDevServer.majorVersion = getMajorVersion(webpackDevServer.packageJson, [3, 4]);
|
|
140
|
+
debug('WebpackDevServer: Successfully sourced webpack-dev-server - %o', webpackDevServer);
|
|
141
|
+
return webpackDevServer;
|
|
142
|
+
}
|
|
143
|
+
exports.sourceWebpackDevServer = sourceWebpackDevServer;
|
|
144
|
+
// Source the html-webpack-plugin module from the provided framework or projectRoot.
|
|
145
|
+
// If none is found, we fallback to the version bundled with this package dependent on the major version of webpack.
|
|
146
|
+
// We ship both v4 and v5 of 'html-webpack-plugin' by aliasing the package with the major version (check package.json).
|
|
147
|
+
function sourceHtmlWebpackPlugin(config, framework, webpack) {
|
|
148
|
+
var _a;
|
|
149
|
+
const searchRoot = (_a = framework === null || framework === void 0 ? void 0 : framework.importPath) !== null && _a !== void 0 ? _a : config.cypressConfig.projectRoot;
|
|
150
|
+
debug('HtmlWebpackPlugin: Attempting to source html-webpack-plugin from %s', searchRoot);
|
|
151
|
+
const htmlWebpackPlugin = {};
|
|
152
|
+
let htmlWebpackPluginJsonPath;
|
|
153
|
+
try {
|
|
154
|
+
htmlWebpackPluginJsonPath = require.resolve('html-webpack-plugin/package.json', {
|
|
155
|
+
paths: [searchRoot],
|
|
156
|
+
});
|
|
157
|
+
htmlWebpackPlugin.packageJson = require(htmlWebpackPluginJsonPath);
|
|
158
|
+
// Check that they're not using v3 of html-webpack-plugin. Since we should be the only consumer of it,
|
|
159
|
+
// we shouldn't be concerned with using our own copy if they've shipped w/ an earlier version
|
|
160
|
+
htmlWebpackPlugin.majorVersion = getMajorVersion(htmlWebpackPlugin.packageJson, [4, 5]);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
const err = e;
|
|
164
|
+
if (err.code !== 'MODULE_NOT_FOUND' && !err.message.includes('Unexpected major version')) {
|
|
165
|
+
debug('HtmlWebpackPlugin: Failed to source html-webpack-plugin - %s', e);
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
const htmlWebpack = `html-webpack-plugin-${webpack.majorVersion}`;
|
|
169
|
+
debug('HtmlWebpackPlugin: Falling back to bundled version %s', htmlWebpack);
|
|
170
|
+
htmlWebpackPluginJsonPath = require.resolve(`${htmlWebpack}/package.json`, {
|
|
171
|
+
paths: [
|
|
172
|
+
__dirname,
|
|
173
|
+
],
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
htmlWebpackPlugin.importPath = path_1.default.dirname(htmlWebpackPluginJsonPath),
|
|
177
|
+
htmlWebpackPlugin.packageJson = require(htmlWebpackPluginJsonPath),
|
|
178
|
+
htmlWebpackPlugin.module = require(htmlWebpackPlugin.importPath),
|
|
179
|
+
htmlWebpackPlugin.majorVersion = getMajorVersion(htmlWebpackPlugin.packageJson, [4, 5]);
|
|
180
|
+
debug('HtmlWebpackPlugin: Successfully sourced html-webpack-plugin - %o', htmlWebpackPlugin);
|
|
181
|
+
return htmlWebpackPlugin;
|
|
182
|
+
}
|
|
183
|
+
exports.sourceHtmlWebpackPlugin = sourceHtmlWebpackPlugin;
|
|
184
|
+
// Most frameworks follow a similar path for sourcing webpack dependencies so this is a utility to handle all the sourcing.
|
|
185
|
+
function sourceDefaultWebpackDependencies(config) {
|
|
186
|
+
const framework = sourceFramework(config);
|
|
187
|
+
const webpack = sourceWebpack(config, framework);
|
|
188
|
+
const webpackDevServer = sourceWebpackDevServer(config, framework);
|
|
189
|
+
const htmlWebpackPlugin = sourceHtmlWebpackPlugin(config, framework, webpack);
|
|
190
|
+
return {
|
|
191
|
+
framework,
|
|
192
|
+
webpack,
|
|
193
|
+
webpackDevServer,
|
|
194
|
+
htmlWebpackPlugin,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
exports.sourceDefaultWebpackDependencies = sourceDefaultWebpackDependencies;
|
|
198
|
+
function getMajorVersion(json, acceptedVersions) {
|
|
199
|
+
const major = Number(json.version.split('.')[0]);
|
|
200
|
+
if (!acceptedVersions.includes(major)) {
|
|
201
|
+
throw new Error(`Unexpected major version of ${json.name}. ` +
|
|
202
|
+
`Cypress webpack-dev-server works with ${json.name} versions ${acceptedVersions.join(', ')} - saw ${json.version}`);
|
|
203
|
+
}
|
|
204
|
+
return Number(major);
|
|
205
|
+
}
|
|
206
|
+
exports.getMajorVersion = getMajorVersion;
|
|
207
|
+
function restoreLoadHook() {
|
|
208
|
+
module_1.default._load = originalModuleLoad;
|
|
209
|
+
module_1.default._resolveFilename = originalModuleResolveFilename;
|
|
210
|
+
}
|
|
211
|
+
exports.restoreLoadHook = restoreLoadHook;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.vueCliHandler = 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:vueCliHandler');
|
|
8
|
+
function vueCliHandler(devServerConfig) {
|
|
9
|
+
const sourceWebpackModulesResult = (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig);
|
|
10
|
+
try {
|
|
11
|
+
const config = require.resolve('@vue/cli-service/webpack.config', {
|
|
12
|
+
paths: [devServerConfig.cypressConfig.projectRoot],
|
|
13
|
+
});
|
|
14
|
+
const webpackConfig = require(config);
|
|
15
|
+
debug('webpack config %o', webpackConfig);
|
|
16
|
+
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult };
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
console.error(e); // eslint-disable-line no-console
|
|
20
|
+
throw Error(`Error loading @vue/cli-service/webpack.config.js. Looked in ${require.resolve.paths(devServerConfig.cypressConfig.projectRoot)}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.vueCliHandler = vueCliHandler;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export interface ResolvedDevServerConfig {
|
|
5
|
-
port: number;
|
|
6
|
-
close: (done?: DoneCallback) => void;
|
|
7
|
-
}
|
|
8
|
-
export { StartDevServer };
|
|
9
|
-
export declare function startDevServer(startDevServerArgs: StartDevServer, exitProcess?: (code?: number | undefined) => never): Promise<ResolvedDevServerConfig>;
|
|
10
|
-
export interface CypressWebpackDevServerConfig {
|
|
11
|
-
webpackConfig?: WebpackConfigurationWithDevServer;
|
|
12
|
-
template?: string;
|
|
13
|
-
}
|
|
14
|
-
export declare function devServer(cypressDevServerConfig: Cypress.DevServerConfig, devServerConfig?: CypressWebpackDevServerConfig): Promise<ResolvedDevServerConfig>;
|
|
15
|
-
export declare function defineDevServerConfig(devServerConfig: CypressWebpackDevServerConfig): CypressWebpackDevServerConfig;
|
|
1
|
+
import { devServer } from './devServer';
|
|
2
|
+
export { devServer };
|
|
3
|
+
export default devServer;
|
package/dist/index.js
CHANGED
|
@@ -1,65 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const debug = (0, debug_1.debug)('cypress:webpack-dev-server:webpack');
|
|
17
|
-
function startDevServer(startDevServerArgs, exitProcess = process.exit) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
const webpackDevServer = yield (0, startServer_1.start)(startDevServerArgs, exitProcess);
|
|
20
|
-
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
if (webpackDevServerFacts_1.webpackDevServerFacts.isV3()) {
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
const server = webpackDevServer.listen(0, '127.0.0.1', () => {
|
|
24
|
-
// FIXME: handle address returning a string
|
|
25
|
-
const port = server.address().port;
|
|
26
|
-
debug('Component testing webpack server started on port', port);
|
|
27
|
-
resolve({
|
|
28
|
-
port,
|
|
29
|
-
close: (done) => {
|
|
30
|
-
server.close();
|
|
31
|
-
done === null || done === void 0 ? void 0 : done();
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (webpackDevServerFacts_1.webpackDevServerFacts.isV4()) {
|
|
38
|
-
yield webpackDevServer.start();
|
|
39
|
-
resolve({
|
|
40
|
-
// @ts-expect-error @types do not yet support v4
|
|
41
|
-
port: webpackDevServer.options.port,
|
|
42
|
-
close: (done) => {
|
|
43
|
-
webpackDevServer.stop();
|
|
44
|
-
done === null || done === void 0 ? void 0 : done();
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
reject(webpackDevServerFacts_1.webpackDevServerFacts.unsupported());
|
|
50
|
-
}));
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
exports.startDevServer = startDevServer;
|
|
54
|
-
function devServer(cypressDevServerConfig, devServerConfig) {
|
|
55
|
-
return startDevServer({
|
|
56
|
-
options: cypressDevServerConfig,
|
|
57
|
-
webpackConfig: devServerConfig === null || devServerConfig === void 0 ? void 0 : devServerConfig.webpackConfig,
|
|
58
|
-
template: devServerConfig === null || devServerConfig === void 0 ? void 0 : devServerConfig.template,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
exports.devServer = devServer;
|
|
62
|
-
function defineDevServerConfig(devServerConfig) {
|
|
63
|
-
return devServerConfig;
|
|
64
|
-
}
|
|
65
|
-
exports.defineDevServerConfig = defineDevServerConfig;
|
|
3
|
+
exports.devServer = void 0;
|
|
4
|
+
const devServer_1 = require("./devServer");
|
|
5
|
+
Object.defineProperty(exports, "devServer", { enumerable: true, get: function () { return devServer_1.devServer; } });
|
|
6
|
+
exports.default = devServer_1.devServer;
|
package/dist/loader.d.ts
CHANGED