Skip to content

Commit c93b652

Browse files
authored
fix: remove webpack entrypoints from for ct webpack projects (#23299)
1 parent f1122fc commit c93b652

File tree

6 files changed

+63
-34
lines changed

6 files changed

+63
-34
lines changed

npm/webpack-dev-server/src/helpers/createReactAppHandler.ts

-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Configurat
7777
webpackConfig = webpackConfig('development')
7878
}
7979

80-
delete webpackConfig.entry
81-
8280
return webpackConfig
8381
} catch (err) {
8482
throw new Error(`Failed to require webpack config at ${webpackConfigPath} with error: ${err}`)

npm/webpack-dev-server/src/helpers/nextHandler.ts

-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Prom
7272
},
7373
)
7474

75-
delete webpackConfig.entry
76-
7775
return webpackConfig
7876
}
7977

npm/webpack-dev-server/src/helpers/nuxtHandler.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export async function nuxtHandler (devServerConfig: WebpackDevServerConfig): Pro
1919
// Nuxt has asset size warnings configured by default which will cause webpack overlays to appear
2020
// in the browser which we don't want.
2121
delete webpackConfig.performance
22-
delete webpackConfig.entry
2322

2423
debug('webpack config %o', webpackConfig)
2524

npm/webpack-dev-server/src/helpers/vueCliHandler.ts

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export function vueCliHandler (devServerConfig: WebpackDevServerConfig): PresetH
1717

1818
debug('webpack config %o', webpackConfig)
1919

20-
delete webpackConfig.entry
21-
2220
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult }
2321
} catch (e) {
2422
console.error(e) // eslint-disable-line no-console

npm/webpack-dev-server/src/makeWebpackConfig.ts

+12-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { debug as debugFn } from 'debug'
22
import * as path from 'path'
33
import { merge } from 'webpack-merge'
44
import { importModule } from 'local-pkg'
5-
import type { Configuration } from 'webpack'
5+
import type { Configuration, EntryObject } from 'webpack'
66
import { makeDefaultWebpackConfig } from './makeDefaultWebpackConfig'
77
import { CypressCTWebpackPlugin } from './CypressCTWebpackPlugin'
88
import type { CreateFinalWebpackConfig } from './createWebpackDevServer'
@@ -41,7 +41,7 @@ if (process.platform === 'linux') {
4141
const OsSeparatorRE = RegExp(`\\${path.sep}`, 'g')
4242
const posixSeparator = '/'
4343

44-
const CYPRESS_WEBPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js')
44+
export const CYPRESS_WEBPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js')
4545

4646
/**
4747
* Removes and/or modifies certain plugins known to conflict
@@ -72,29 +72,6 @@ function modifyWebpackConfigForCypress (webpackConfig: Partial<Configuration>) {
7272
return webpackConfig
7373
}
7474

75-
async function addEntryPoint (webpackConfig: Configuration) {
76-
let entry = webpackConfig.entry
77-
78-
if (typeof entry === 'function') {
79-
entry = await entry()
80-
}
81-
82-
if (typeof entry === 'string') {
83-
entry = [entry, CYPRESS_WEBPACK_ENTRYPOINT]
84-
} else if (Array.isArray(entry)) {
85-
entry.push(CYPRESS_WEBPACK_ENTRYPOINT)
86-
} else if (typeof entry === 'object') {
87-
entry = {
88-
...entry,
89-
['cypress-entry']: CYPRESS_WEBPACK_ENTRYPOINT,
90-
}
91-
} else {
92-
entry = CYPRESS_WEBPACK_ENTRYPOINT
93-
}
94-
95-
webpackConfig.entry = entry
96-
}
97-
9875
/**
9976
* Creates a webpack 4/5 compatible webpack "configuration"
10077
* to pass to the sourced webpack function
@@ -113,6 +90,7 @@ export async function makeWebpackConfig (
11390
},
11491
specs: files,
11592
devServerEvents,
93+
framework,
11694
} = config.devServerConfig
11795

11896
let configFile: string | undefined = undefined
@@ -184,7 +162,15 @@ export async function makeWebpackConfig (
184162
dynamicWebpackConfig,
185163
)
186164

187-
await addEntryPoint(mergedConfig)
165+
// Angular loads global styles and polyfills via script injection in the index.html
166+
if (framework === 'angular') {
167+
mergedConfig.entry = {
168+
...(mergedConfig.entry as EntryObject) || {},
169+
'cypress-entry': CYPRESS_WEBPACK_ENTRYPOINT,
170+
}
171+
} else {
172+
mergedConfig.entry = CYPRESS_WEBPACK_ENTRYPOINT
173+
}
188174

189175
debug('Merged webpack config %o', mergedConfig)
190176

npm/webpack-dev-server/test/makeWebpackConfig.spec.ts

+51-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EventEmitter from 'events'
33
import snapshot from 'snap-shot-it'
44
import { WebpackDevServerConfig } from '../src/devServer'
55
import { sourceDefaultWebpackDependencies } from '../src/helpers/sourceRelativeWebpackModules'
6-
import { makeWebpackConfig } from '../src/makeWebpackConfig'
6+
import { CYPRESS_WEBPACK_ENTRYPOINT, makeWebpackConfig } from '../src/makeWebpackConfig'
77
import { createModuleMatrixResult } from './test-helpers/createModuleMatrixResult'
88

99
describe('makeWebpackConfig', () => {
@@ -86,4 +86,54 @@ describe('makeWebpackConfig', () => {
8686
expect(actual.output.publicPath).to.eq('/test-public-path/')
8787
snapshot(actual)
8888
})
89+
90+
it('removes entrypoint from merged webpackConfig', async () => {
91+
const devServerConfig: WebpackDevServerConfig = {
92+
specs: [],
93+
cypressConfig: {
94+
projectRoot: '.',
95+
devServerPublicPathRoute: '/test-public-path',
96+
} as Cypress.PluginConfigOptions,
97+
webpackConfig: {
98+
entry: { main: 'src/index.js' },
99+
},
100+
devServerEvents: new EventEmitter(),
101+
}
102+
const actual = await makeWebpackConfig({
103+
devServerConfig,
104+
sourceWebpackModulesResult: createModuleMatrixResult({
105+
webpack: 4,
106+
webpackDevServer: 4,
107+
}),
108+
})
109+
110+
expect(actual.entry).eq(CYPRESS_WEBPACK_ENTRYPOINT)
111+
})
112+
113+
it('preserves entrypoint from merged webpackConfig if framework = angular', async () => {
114+
const devServerConfig: WebpackDevServerConfig = {
115+
specs: [],
116+
cypressConfig: {
117+
projectRoot: '.',
118+
devServerPublicPathRoute: '/test-public-path',
119+
} as Cypress.PluginConfigOptions,
120+
webpackConfig: {
121+
entry: { main: 'src/index.js' },
122+
},
123+
devServerEvents: new EventEmitter(),
124+
framework: 'angular',
125+
}
126+
const actual = await makeWebpackConfig({
127+
devServerConfig,
128+
sourceWebpackModulesResult: createModuleMatrixResult({
129+
webpack: 4,
130+
webpackDevServer: 4,
131+
}),
132+
})
133+
134+
expect(actual.entry).deep.eq({
135+
main: 'src/index.js',
136+
'cypress-entry': CYPRESS_WEBPACK_ENTRYPOINT,
137+
})
138+
})
89139
})

0 commit comments

Comments
 (0)