From 726120da183cb6d89c83181761d21f09844a9fc2 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Fri, 9 Apr 2021 12:55:09 +1000 Subject: [PATCH 1/3] fix: ensure root el mounting exists, remove userland html webpack plugin (#15870) * fix: ensure root el exists * fix: remove userland webpack plugins * chore: bump html webpack plugin version * fix: remove preload plugin * chore: remove dep * fix: revert code * chore: revert * chore: revert * fix: null check for user webpack --- npm/webpack-dev-server/package.json | 2 +- .../src/makeWebpackConfig.ts | 20 +++++++++++++++++++ yarn.lock | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/npm/webpack-dev-server/package.json b/npm/webpack-dev-server/package.json index b688bd251b95..c72c9e9809a1 100644 --- a/npm/webpack-dev-server/package.json +++ b/npm/webpack-dev-server/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "debug": "4.3.2", + "html-webpack-plugin": "^4.0.0", "lazy-compile-webpack-plugin": "0.1.11", "semver": "^7.3.4", "webpack-merge": "^5.4.0" @@ -25,7 +26,6 @@ "webpack-dev-server": "^3.11.0" }, "peerDependencies": { - "html-webpack-plugin": "^4.0.0", "webpack": ">=4", "webpack-dev-server": "^3.0.0" }, diff --git a/npm/webpack-dev-server/src/makeWebpackConfig.ts b/npm/webpack-dev-server/src/makeWebpackConfig.ts index 1e83d6ade646..03a3c3ec5b8d 100644 --- a/npm/webpack-dev-server/src/makeWebpackConfig.ts +++ b/npm/webpack-dev-server/src/makeWebpackConfig.ts @@ -8,6 +8,7 @@ import CypressCTOptionsPlugin, { CypressCTOptionsPluginOptions } from './plugin' const debug = debugFn('cypress:webpack-dev-server:makeWebpackConfig') const WEBPACK_MAJOR_VERSION = Number(webpack.version.split('.')[0]) +const removeList = ['HtmlWebpackPlugin', 'PreloadPlugin'] export interface UserWebpackDevServerOptions { /** @@ -68,6 +69,25 @@ export async function makeWebpackConfig (userWebpackConfig: webpack.Configuratio ], } + // certain plugins conflict with HtmlWebpackPlugin and cause + // problems for some setups. + // most of these are application optimizations that are not relevant in a + // testing environment. + // remove those plugins to ensure a smooth configuration experience. + // https://github.com/cypress-io/cypress/issues/15865 + if (userWebpackConfig?.plugins) { + userWebpackConfig.plugins = userWebpackConfig.plugins.filter((plugin) => { + if (removeList.includes(plugin.constructor.name)) { + /* eslint-disable no-console */ + console.warn(`[@cypress/webpack-dev-server]: removing ${plugin.constructor.name} from configuration.`) + + return false + } + + return true + }) + } + const mergedConfig = merge( userWebpackConfig, defaultWebpackConfig, diff --git a/yarn.lock b/yarn.lock index cc0f36cab972..641645190a90 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19133,7 +19133,7 @@ html-webpack-plugin@4.0.0-beta.5: tapable "^1.1.0" util.promisify "1.0.0" -html-webpack-plugin@^4.5.0: +html-webpack-plugin@^4.0.0, html-webpack-plugin@^4.5.0: version "4.5.2" resolved "https://registry.yarnpkg.com/html-webpack-plugin/-/html-webpack-plugin-4.5.2.tgz#76fc83fa1a0f12dd5f7da0404a54e2699666bc12" integrity sha512-q5oYdzjKUIPQVjOosjgvCHQOv9Ett9CYYHlgvJeXG0qQvdSojnBq4vAdQBwn1+yGveAwHCoe/rMR86ozX3+c2A== From 8e894a0fdb899be8dd8993319c9297ea73c10321 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Fri, 9 Apr 2021 22:23:01 +1000 Subject: [PATCH 2/3] fix(webpack-dev-server): remove output.publicPath from webpack-dev-server (#15839) * fix: remove publicPath in react-scripts if it is present * fix: remove publicPath in react-scripts if it is present * fix: remove publicPath in webpack-dev-server regardless * fix: add root path * chore: revert * fix: remove user base path * remove unused function * test: add unit test for make webpack config * chore: make purpose for fake url more clear * do not include local filesystem path in snapshot --- npm/react/examples/react-scripts/package.json | 1 + .../makeWebpackConfig.spec.ts.js | 17 ++++++++++ npm/webpack-dev-server/package.json | 1 + .../src/makeWebpackConfig.ts | 6 +--- .../test/unit/makeWebpackConfig.spec.ts | 31 +++++++++++++++++++ 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 npm/webpack-dev-server/__snapshots__/makeWebpackConfig.spec.ts.js create mode 100644 npm/webpack-dev-server/test/unit/makeWebpackConfig.spec.ts diff --git a/npm/react/examples/react-scripts/package.json b/npm/react/examples/react-scripts/package.json index eea91abca730..9b63bb6beab5 100644 --- a/npm/react/examples/react-scripts/package.json +++ b/npm/react/examples/react-scripts/package.json @@ -17,6 +17,7 @@ "react-dom": "17.0.1", "react-scripts": "4.0.2" }, + "homepage": "/fake-to-avoid-regression-https://github.com/cypress-io/cypress/issues/15811", "browserslist": { "production": [ ">0.2%", diff --git a/npm/webpack-dev-server/__snapshots__/makeWebpackConfig.spec.ts.js b/npm/webpack-dev-server/__snapshots__/makeWebpackConfig.spec.ts.js new file mode 100644 index 000000000000..78137930678d --- /dev/null +++ b/npm/webpack-dev-server/__snapshots__/makeWebpackConfig.spec.ts.js @@ -0,0 +1,17 @@ +exports['makeWebpackConfig ignores userland webpack `output.publicPath` 1'] = { + "output": { + "publicPath": "/test-public-path/", + "filename": "[name].js" + }, + "mode": "development", + "optimization": { + "splitChunks": { + "chunks": "all" + } + }, + "plugins": [ + "HtmlWebpackPlugin", + "CypressCTOptionsPlugin", + "LazyCompilePlugin" + ] +} diff --git a/npm/webpack-dev-server/package.json b/npm/webpack-dev-server/package.json index c72c9e9809a1..7945c004d6ff 100644 --- a/npm/webpack-dev-server/package.json +++ b/npm/webpack-dev-server/package.json @@ -20,6 +20,7 @@ "@types/webpack-dev-server": "^3.11.1", "chai": "^4.2.0", "mocha": "^8.1.3", + "snap-shot-it": "7.9.3", "speed-measure-webpack-plugin": "1.4.2", "typescript": "^4.2.3", "webpack": "^4.44.2", diff --git a/npm/webpack-dev-server/src/makeWebpackConfig.ts b/npm/webpack-dev-server/src/makeWebpackConfig.ts index 03a3c3ec5b8d..1be0d39949a3 100644 --- a/npm/webpack-dev-server/src/makeWebpackConfig.ts +++ b/npm/webpack-dev-server/src/makeWebpackConfig.ts @@ -23,10 +23,6 @@ interface MakeWebpackConfigOptions extends CypressCTOptionsPluginOptions, UserWe isOpenMode: boolean } -const mergePublicPath = (baseValue = '/', userValue = '/') => { - return path.join(baseValue, userValue, '/') -} - function getLazyCompilationWebpackConfig (options: MakeWebpackConfigOptions): webpack.Configuration { if (options.disableLazyCompilation || !options.isOpenMode) { return {} @@ -53,7 +49,7 @@ export async function makeWebpackConfig (userWebpackConfig: webpack.Configuratio const entry = path.resolve(__dirname, './browser.js') - const publicPath = mergePublicPath(devServerPublicPathRoute, userWebpackConfig?.output?.publicPath) + const publicPath = path.join(devServerPublicPathRoute, '/') const dynamicWebpackConfig = { output: { diff --git a/npm/webpack-dev-server/test/unit/makeWebpackConfig.spec.ts b/npm/webpack-dev-server/test/unit/makeWebpackConfig.spec.ts new file mode 100644 index 000000000000..074daf07605e --- /dev/null +++ b/npm/webpack-dev-server/test/unit/makeWebpackConfig.spec.ts @@ -0,0 +1,31 @@ +import { expect } from 'chai' +import snapshot from 'snap-shot-it' +import { makeWebpackConfig } from '../../src/makeWebpackConfig' + +describe('makeWebpackConfig', () => { + it('ignores userland webpack `output.publicPath`', async () => { + const actual = await makeWebpackConfig({ + output: { + publicPath: '/this-will-be-ignored', + }, + }, { + devServerPublicPathRoute: '/test-public-path', + isOpenMode: true, + supportFile: '/support.js', + projectRoot: '.', + files: [], + }) + + // plugins contain circular deps which cannot be serialized in a snapshot. + // instead just compare the name and order of the plugins. + // @ts-expect-error + actual.plugins = actual.plugins.map((p) => p.constructor.name) + + // these will include paths from the user's local file system, so we should not include them the snapshot + delete actual.output.path + delete actual.entry + + expect(actual.output.publicPath).to.eq('/test-public-path/') + snapshot(actual) + }) +}) From a5bb2e0764c222fd482fbd0c17443d946a0ca15b Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Fri, 9 Apr 2021 18:45:44 +0200 Subject: [PATCH 3/3] fix(component testing): default config values (#15902) Co-authored-by: Jessica Sachs --- packages/server-ct/src/project-ct.ts | 19 ++++--------------- packages/server/lib/config.js | 2 ++ 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/server-ct/src/project-ct.ts b/packages/server-ct/src/project-ct.ts index fdbe12b0adc0..3c10c331b387 100644 --- a/packages/server-ct/src/project-ct.ts +++ b/packages/server-ct/src/project-ct.ts @@ -2,12 +2,11 @@ import Debug from 'debug' import config from '@packages/server/lib/config' import plugins from '@packages/server/lib/plugins' import devServer from '@packages/server/lib/plugins/dev-server' -import { ProjectBase } from '@packages/server/lib/project-base' +import { Cfg, ProjectBase } from '@packages/server/lib/project-base' import settings from '@packages/server/lib/util/settings' import specsUtil from '@packages/server/lib/util/specs' import { ServerCt } from './server-ct' import { SpecsStore } from './specs-store' -import { options } from '@packages/server/lib/config_options' export * from '@packages/server/lib/project-base' @@ -24,22 +23,12 @@ export class ProjectCt extends ProjectBase { * 2. otherwise, use 500/500 by default. */ addComponentTestingUniqueDefaults (cfg: Record) { - const defaultViewport = options.reduce>((acc, curr) => { - if (curr.name === 'viewportHeight') { - return { ...acc, viewportHeight: curr.defaultValue as number } - } - - if (curr.name === 'viewportWidth') { - return { ...acc, viewportWidth: curr.defaultValue as number } - } - - return acc - }, {}) + const rawJson = cfg.rawJson as Cfg return { ...cfg, - viewportHeight: cfg.viewportHeight !== defaultViewport.viewportHeight ? cfg.viewportHeight : 500, - viewportWidth: cfg.viewportWidth !== defaultViewport.viewportWidth ? cfg.viewportWidth : 500, + viewportHeight: rawJson.viewportHeight ?? 500, + viewportWidth: rawJson.viewportWidth ?? 500, } } diff --git a/packages/server/lib/config.js b/packages/server/lib/config.js index e996da02d0b4..c15fa6ef67ef 100644 --- a/packages/server/lib/config.js +++ b/packages/server/lib/config.js @@ -241,6 +241,8 @@ module.exports = { mergeDefaults (config = {}, options = {}) { const resolved = {} + config.rawJson = _.cloneDeep(config) + _.extend(config, _.pick(options, 'configFile', 'morgan', 'isTextTerminal', 'socketId', 'report', 'browsers')) debug('merged config with options, got %o', config)