@cypress/webpack-dev-server 2.5.0 → 3.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/dist/devServer.js CHANGED
@@ -80,7 +80,7 @@ async function getPreset(devServerConfig) {
80
80
  case undefined:
81
81
  return { sourceWebpackModulesResult: (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig) };
82
82
  default:
83
- throw new Error(`Unexpected framework ${devServerConfig.framework}, please visit https://docs.cypress.io/guides/component-testing/component-framework-configuration to see a list of supported frameworks`);
83
+ throw new Error(`Unexpected framework ${devServerConfig.framework}, please visit https://on.cypress.io/component-framework-configuration to see a list of supported frameworks`);
84
84
  }
85
85
  }
86
86
  /**
@@ -0,0 +1,2 @@
1
+ export declare const dynamicImport: <T>(module: string) => Promise<T>;
2
+ export declare const dynamicAbsoluteImport: (filePath: string) => Promise<any>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dynamicAbsoluteImport = exports.dynamicImport = void 0;
4
+ const url_1 = require("url");
5
+ // tsc will compile `import(...)` calls to require unless a different tsconfig.module value
6
+ // is used (e.g. module=node16). To change this, we would also have to change the ts-node behavior when requiring the
7
+ // Cypress config file. This hack for keeping dynamic imports from being converted works across all
8
+ // of our supported node versions
9
+ const _dynamicImport = new Function('specifier', 'return import(specifier)');
10
+ const dynamicImport = (module) => _dynamicImport(module);
11
+ exports.dynamicImport = dynamicImport;
12
+ const dynamicAbsoluteImport = (filePath) => {
13
+ return (0, exports.dynamicImport)((0, url_1.pathToFileURL)(filePath).href);
14
+ };
15
+ exports.dynamicAbsoluteImport = dynamicAbsoluteImport;
@@ -5,9 +5,8 @@ const tslib_1 = require("tslib");
5
5
  const fs = tslib_1.__importStar(require("fs-extra"));
6
6
  const os_1 = require("os");
7
7
  const path = tslib_1.__importStar(require("path"));
8
- const url_1 = require("url");
8
+ const dynamic_import_1 = require("../dynamic-import");
9
9
  const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
10
- const dynamicImport = new Function('specifier', 'return import(specifier)');
11
10
  async function getProjectConfig(projectRoot) {
12
11
  var _a;
13
12
  const angularJson = await getAngularJson(projectRoot);
@@ -74,15 +73,15 @@ async function getTempDir() {
74
73
  }
75
74
  exports.getTempDir = getTempDir;
76
75
  async function getAngularCliModules(projectRoot) {
77
- const [{ generateBrowserWebpackConfigFromContext }, { getCommonConfig }, { getStylesConfig },] = await Promise.all([
76
+ const angularCLiModules = [
78
77
  '@angular-devkit/build-angular/src/utils/webpack-browser-config.js',
79
78
  '@angular-devkit/build-angular/src/webpack/configs/common.js',
80
79
  '@angular-devkit/build-angular/src/webpack/configs/styles.js',
81
- ].map((dep) => {
80
+ ];
81
+ const [{ generateBrowserWebpackConfigFromContext }, { getCommonConfig }, { getStylesConfig },] = await Promise.all(angularCLiModules.map((dep) => {
82
82
  try {
83
83
  const depPath = require.resolve(dep, { paths: [projectRoot] });
84
- const url = (0, url_1.pathToFileURL)(depPath).href;
85
- return dynamicImport(url);
84
+ return (0, dynamic_import_1.dynamicAbsoluteImport)(depPath);
86
85
  }
87
86
  catch (e) {
88
87
  throw new Error(`Could not resolve "${dep}". Do you have "@angular-devkit/build-angular" installed?`);
@@ -96,7 +95,7 @@ async function getAngularCliModules(projectRoot) {
96
95
  }
97
96
  exports.getAngularCliModules = getAngularCliModules;
98
97
  async function getAngularJson(projectRoot) {
99
- const { findUp } = await dynamicImport('find-up');
98
+ const { findUp } = await (0, dynamic_import_1.dynamicImport)('find-up');
100
99
  const angularJsonPath = await findUp('angular.json', { cwd: projectRoot });
101
100
  if (!angularJsonPath) {
102
101
  throw new Error(`Could not find angular.json. Looked in ${projectRoot} and up.`);
@@ -1 +1,3 @@
1
- export {};
1
+ import type { Configuration } from 'webpack';
2
+ import type { CreateFinalWebpackConfig } from './createWebpackDevServer';
3
+ export declare function makeCypressWebpackConfig(config: CreateFinalWebpackConfig): Configuration;
@@ -1,43 +1,55 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeDefaultWebpackConfig = void 0;
3
+ exports.makeCypressWebpackConfig = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const path_1 = tslib_1.__importDefault(require("path"));
6
6
  const debug_1 = tslib_1.__importDefault(require("debug"));
7
+ const CypressCTWebpackPlugin_1 = require("./CypressCTWebpackPlugin");
7
8
  const debug = (0, debug_1.default)('cypress:webpack-dev-server:makeDefaultWebpackConfig');
8
9
  const OUTPUT_PATH = path_1.default.join(__dirname, 'dist');
9
- /**
10
- * @returns {import('webpack').Configuration}
11
- * @internal
12
- */
13
- function makeDefaultWebpackConfig(config) {
14
- const { module: _HtmlWebpackPlugin, packageJson: { version }, importPath, } = config.sourceWebpackModulesResult.htmlWebpackPlugin;
15
- const indexHtmlFile = config.devServerConfig.cypressConfig.indexHtmlFile;
16
- const isRunMode = config.devServerConfig.cypressConfig.isTextTerminal;
17
- const HtmlWebpackPlugin = _HtmlWebpackPlugin;
18
- debug(`Using HtmlWebpackPlugin version ${version} from ${importPath}`);
19
- const optimization = {};
20
- if (config.sourceWebpackModulesResult.webpack.majorVersion === 5) {
10
+ const OsSeparatorRE = RegExp(`\\${path_1.default.sep}`, 'g');
11
+ const posixSeparator = '/';
12
+ function makeCypressWebpackConfig(config) {
13
+ const { devServerConfig: { cypressConfig: { projectRoot, devServerPublicPathRoute, supportFile, indexHtmlFile, isTextTerminal: isRunMode, }, specs: files, devServerEvents, framework, }, sourceWebpackModulesResult: { webpack: { module: webpack, majorVersion: webpackMajorVersion, }, htmlWebpackPlugin: { module: HtmlWebpackPlugin, majorVersion: htmlWebpackPluginVersion, importPath: htmlWebpackPluginImportPath, }, webpackDevServer: { majorVersion: webpackDevServerMajorVersion, }, }, } = config;
14
+ debug(`Using HtmlWebpackPlugin version ${htmlWebpackPluginVersion} from ${htmlWebpackPluginImportPath}`);
15
+ const optimization = {
16
+ // To prevent files from being tree shaken by webpack, we set optimization.sideEffects: false ensuring that
17
+ // webpack does not recognize the sideEffects flag in the package.json and thus files are not unintentionally
18
+ // dropped during testing in production mode.
19
+ sideEffects: false,
20
+ splitChunks: {
21
+ chunks: 'all',
22
+ },
23
+ };
24
+ if (webpackMajorVersion === 5) {
21
25
  optimization.emitOnErrors = true;
22
26
  }
23
27
  else {
24
28
  optimization.noEmitOnErrors = false;
25
29
  }
26
- // To prevent files from being tree shaken by webpack, we set optimization.sideEffects: false ensuring that
27
- // webpack does not recognize the sideEffects flag in the package.json and thus files are not unintentionally
28
- // dropped during testing in production mode.
29
- optimization.sideEffects = false;
30
+ const publicPath = (path_1.default.sep === posixSeparator)
31
+ ? path_1.default.join(devServerPublicPathRoute, posixSeparator)
32
+ // The second line here replaces backslashes on windows with posix compatible slash
33
+ // See https://github.com/cypress-io/cypress/issues/16097
34
+ : path_1.default.join(devServerPublicPathRoute, posixSeparator)
35
+ .replace(OsSeparatorRE, posixSeparator);
30
36
  const finalConfig = {
31
37
  mode: 'development',
32
- optimization: Object.assign(Object.assign({}, optimization), { splitChunks: {
33
- chunks: 'all',
34
- } }),
38
+ optimization,
35
39
  output: {
36
40
  filename: '[name].js',
37
41
  path: OUTPUT_PATH,
42
+ publicPath,
38
43
  },
39
44
  plugins: [
40
- new HtmlWebpackPlugin(Object.assign({ template: indexHtmlFile }, (config.devServerConfig.framework === 'angular' ? { scriptLoading: 'module', base: '/__cypress/src/' } : {}))),
45
+ new HtmlWebpackPlugin(Object.assign({ template: indexHtmlFile }, (framework === 'angular' ? { scriptLoading: 'module', base: '/__cypress/src/' } : {}))),
46
+ new CypressCTWebpackPlugin_1.CypressCTWebpackPlugin({
47
+ files,
48
+ projectRoot,
49
+ devServerEvents,
50
+ supportFile,
51
+ webpack,
52
+ }),
41
53
  ],
42
54
  devtool: 'inline-source-map',
43
55
  };
@@ -47,7 +59,7 @@ function makeDefaultWebpackConfig(config) {
47
59
  ignored: '**/*',
48
60
  };
49
61
  }
50
- if (config.sourceWebpackModulesResult.webpackDevServer.majorVersion === 4) {
62
+ if (webpackDevServerMajorVersion === 4) {
51
63
  return Object.assign(Object.assign({}, finalConfig), { devServer: {
52
64
  client: {
53
65
  overlay: false,
@@ -59,4 +71,4 @@ function makeDefaultWebpackConfig(config) {
59
71
  overlay: false,
60
72
  } });
61
73
  }
62
- exports.makeDefaultWebpackConfig = makeDefaultWebpackConfig;
74
+ exports.makeCypressWebpackConfig = makeCypressWebpackConfig;
@@ -1,7 +1,8 @@
1
+ import type { Configuration } from 'webpack';
1
2
  import type { CreateFinalWebpackConfig } from './createWebpackDevServer';
2
3
  export declare const CYPRESS_WEBPACK_ENTRYPOINT: string;
3
4
  /**
4
5
  * Creates a webpack 4/5 compatible webpack "configuration"
5
6
  * to pass to the sourced webpack function
6
7
  */
7
- export declare function makeWebpackConfig(config: CreateFinalWebpackConfig): Promise<any>;
8
+ export declare function makeWebpackConfig(config: CreateFinalWebpackConfig): Promise<Configuration>;
@@ -5,10 +5,9 @@ const tslib_1 = require("tslib");
5
5
  const debug_1 = require("debug");
6
6
  const path = tslib_1.__importStar(require("path"));
7
7
  const webpack_merge_1 = require("webpack-merge");
8
- const local_pkg_1 = require("local-pkg");
9
8
  const makeDefaultWebpackConfig_1 = require("./makeDefaultWebpackConfig");
10
- const CypressCTWebpackPlugin_1 = require("./CypressCTWebpackPlugin");
11
9
  const constants_1 = require("./constants");
10
+ const dynamic_import_1 = require("./dynamic-import");
12
11
  const debug = (0, debug_1.debug)('cypress:webpack-dev-server:makeWebpackConfig');
13
12
  const removeList = [
14
13
  // We provide a webpack-html-plugin config pinned to a specific version (4.x)
@@ -33,8 +32,6 @@ const removeList = [
33
32
  if (process.platform === 'linux') {
34
33
  removeList.push('CaseSensitivePathsPlugin');
35
34
  }
36
- const OsSeparatorRE = RegExp(`\\${path.sep}`, 'g');
37
- const posixSeparator = '/';
38
35
  exports.CYPRESS_WEBPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js');
39
36
  /**
40
37
  * Removes and/or modifies certain plugins known to conflict
@@ -43,14 +40,7 @@ exports.CYPRESS_WEBPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js');
43
40
  function modifyWebpackConfigForCypress(webpackConfig) {
44
41
  var _a;
45
42
  if (webpackConfig === null || webpackConfig === void 0 ? void 0 : webpackConfig.plugins) {
46
- webpackConfig.plugins = webpackConfig.plugins.filter((plugin) => {
47
- if (removeList.includes(plugin.constructor.name)) {
48
- /* eslint-disable no-console */
49
- console.warn(`[@cypress/webpack-dev-server]: removing ${plugin.constructor.name} from configuration.`);
50
- return false;
51
- }
52
- return true;
53
- });
43
+ webpackConfig.plugins = webpackConfig.plugins.filter((plugin) => !removeList.includes(plugin.constructor.name));
54
44
  }
55
45
  if (typeof ((_a = webpackConfig === null || webpackConfig === void 0 ? void 0 : webpackConfig.module) === null || _a === void 0 ? void 0 : _a.unsafeCache) === 'function') {
56
46
  const originalCachePredicate = webpackConfig.module.unsafeCache;
@@ -60,24 +50,25 @@ function modifyWebpackConfigForCypress(webpackConfig) {
60
50
  }
61
51
  return webpackConfig;
62
52
  }
53
+ async function getWebpackConfigFromProjectRoot(projectRoot) {
54
+ const { findUp } = await (0, dynamic_import_1.dynamicImport)('find-up');
55
+ return await findUp(constants_1.configFiles, { cwd: projectRoot });
56
+ }
63
57
  /**
64
58
  * Creates a webpack 4/5 compatible webpack "configuration"
65
59
  * to pass to the sourced webpack function
66
60
  */
67
61
  async function makeWebpackConfig(config) {
68
62
  var _a, _b, _c;
69
- const { module: webpack } = config.sourceWebpackModulesResult.webpack;
70
63
  let userWebpackConfig = config.devServerConfig.webpackConfig;
71
64
  const frameworkWebpackConfig = config.frameworkConfig;
72
- const { cypressConfig: { projectRoot, devServerPublicPathRoute, supportFile, }, specs: files, devServerEvents, framework, } = config.devServerConfig;
73
- let configFile = undefined;
65
+ const { cypressConfig: { projectRoot, supportFile, }, specs: files, framework, } = config.devServerConfig;
74
66
  if (!userWebpackConfig && !frameworkWebpackConfig) {
75
67
  debug('Not user or framework webpack config received. Trying to automatically source it');
76
- const { default: findUp } = await (0, local_pkg_1.importModule)('find-up');
77
- configFile = await findUp(constants_1.configFiles, { cwd: projectRoot });
68
+ const configFile = await getWebpackConfigFromProjectRoot(projectRoot);
78
69
  if (configFile) {
79
70
  debug('found webpack config %s', configFile);
80
- const sourcedConfig = await (0, local_pkg_1.importModule)(configFile);
71
+ const sourcedConfig = await (0, dynamic_import_1.dynamicAbsoluteImport)(configFile);
81
72
  debug('config contains %o', sourcedConfig);
82
73
  if (sourcedConfig && typeof sourcedConfig === 'object') {
83
74
  userWebpackConfig = (_a = sourcedConfig.default) !== null && _a !== void 0 ? _a : sourcedConfig;
@@ -104,27 +95,7 @@ async function makeWebpackConfig(config) {
104
95
  debug(`New webpack entries %o`, files);
105
96
  debug(`Project root`, projectRoot);
106
97
  debug(`Support file`, supportFile);
107
- const publicPath = (path.sep === posixSeparator)
108
- ? path.join(devServerPublicPathRoute, posixSeparator)
109
- // The second line here replaces backslashes on windows with posix compatible slash
110
- // See https://github.com/cypress-io/cypress/issues/16097
111
- : path.join(devServerPublicPathRoute, posixSeparator)
112
- .replace(OsSeparatorRE, posixSeparator);
113
- const dynamicWebpackConfig = {
114
- output: {
115
- publicPath,
116
- },
117
- plugins: [
118
- new CypressCTWebpackPlugin_1.CypressCTWebpackPlugin({
119
- files,
120
- projectRoot,
121
- devServerEvents,
122
- supportFile,
123
- webpack,
124
- }),
125
- ],
126
- };
127
- const mergedConfig = (0, webpack_merge_1.merge)(userAndFrameworkWebpackConfig, (0, makeDefaultWebpackConfig_1.makeDefaultWebpackConfig)(config), dynamicWebpackConfig);
98
+ const mergedConfig = (0, webpack_merge_1.merge)(userAndFrameworkWebpackConfig, (0, makeDefaultWebpackConfig_1.makeCypressWebpackConfig)(config));
128
99
  // Some frameworks (like Next.js) change this value which changes the path we would need to use to fetch our spec.
129
100
  // (eg, http://localhost:xxxx/<dev-server-public-path>/static/chunks/spec-<x>.js). Deleting this key to normalize
130
101
  // the spec URL to `*/spec-<x>.js` which we need to know up-front so we can fetch the sourcemaps.
@@ -137,11 +108,6 @@ async function makeWebpackConfig(config) {
137
108
  mergedConfig.entry = exports.CYPRESS_WEBPACK_ENTRYPOINT;
138
109
  }
139
110
  debug('Merged webpack config %o', mergedConfig);
140
- if (process.env.WEBPACK_PERF_MEASURE) {
141
- // only for debugging
142
- const { measureWebpackPerformance } = require('./measureWebpackPerformance');
143
- return measureWebpackPerformance(mergedConfig);
144
- }
145
111
  return mergedConfig;
146
112
  }
147
113
  exports.makeWebpackConfig = makeWebpackConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress/webpack-dev-server",
3
- "version": "2.5.0",
3
+ "version": "3.0.0",
4
4
  "description": "Launches Webpack Dev Server for Component Testing",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -21,7 +21,6 @@
21
21
  "fs-extra": "9.1.0",
22
22
  "html-webpack-plugin-4": "npm:html-webpack-plugin@^4",
23
23
  "html-webpack-plugin-5": "npm:html-webpack-plugin@^5",
24
- "local-pkg": "0.4.1",
25
24
  "speed-measure-webpack-plugin": "1.4.2",
26
25
  "tslib": "^2.3.1",
27
26
  "webpack-dev-server": "^4.7.4",
@@ -1 +0,0 @@
1
- export {};
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.measureWebpackPerformance = void 0;
4
- const tslib_1 = require("tslib");
5
- /* eslint-disable no-restricted-syntax */
6
- /* eslint-disable no-console */
7
- const path_1 = tslib_1.__importDefault(require("path"));
8
- const fs_1 = tslib_1.__importDefault(require("fs"));
9
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
10
- const speed_measure_webpack_plugin_1 = tslib_1.__importDefault(require("speed-measure-webpack-plugin"));
11
- /**
12
- * @internal
13
- */
14
- function measureWebpackPerformance(webpackConfig) {
15
- if (!process.env.WEBPACK_PERF_MEASURE) {
16
- throw new Error('Performance monitoring is possible only with WEBPACK_PERF_MEASURE env variable set');
17
- }
18
- const compareWithPrevious = process.env.WEBPACK_PERF_MEASURE_COMPARE;
19
- function percentageDiff(a, b) {
20
- return ((a - b) / a) * 100;
21
- }
22
- const compareOutput = (output) => {
23
- const statsPath = path_1.default.resolve(__dirname, '..', '__perf-stats', `${compareWithPrevious}.json`);
24
- if (!fs_1.default.existsSync(statsPath) || process.env.WEBPACK_PERF_MEASURE_UPDATE) {
25
- return fs_1.default.writeFileSync(statsPath, output, { encoding: 'utf-8' });
26
- }
27
- const newStats = JSON.parse(output);
28
- const oldStats = require(statsPath);
29
- const totalPercentageDiff = percentageDiff(oldStats.misc.compileTime, newStats.misc.compileTime);
30
- const printResult = (result) => {
31
- const delimiter = new Array(process.stdout.columns).fill('═').join('');
32
- console.log(delimiter);
33
- console.log(`${chalk_1.default.bold('WEBPACK_PERF_MEASURE')}`);
34
- console.log(`Before: ${chalk_1.default.bold(oldStats.misc.compileTime / 1000)}s`);
35
- console.log(`After: ${chalk_1.default.bold(newStats.misc.compileTime / 1000)}s`);
36
- console.log(result);
37
- console.log(delimiter);
38
- };
39
- if (Math.abs(totalPercentageDiff) < 5) {
40
- printResult('No sufficient build time difference');
41
- }
42
- else if (totalPercentageDiff > 0) {
43
- printResult(`New build is faster: ${chalk_1.default.green.bold(`+${Math.round(totalPercentageDiff)}%`)}`);
44
- }
45
- else {
46
- printResult(`New build is slower: ${chalk_1.default.red.bold(`${Math.round(totalPercentageDiff)}%`)}`);
47
- }
48
- };
49
- const smp = compareWithPrevious ?
50
- new speed_measure_webpack_plugin_1.default({
51
- outputFormat: 'json',
52
- outputTarget: compareOutput,
53
- })
54
- : new speed_measure_webpack_plugin_1.default();
55
- // @ts-expect-error
56
- return smp.wrap(webpackConfig);
57
- }
58
- exports.measureWebpackPerformance = measureWebpackPerformance;