Skip to content

Commit 49f64e7

Browse files
chore: add minimal built-ins back in to @cypress/webpack-batteries-included-preprocessor (#31815)
* chore: re-add the buffer, path, process, os, and stream built-ins to wbip * Update npm/webpack-batteries-included-preprocessor/README.md Co-authored-by: Jennifer Shehane <[email protected]> * chore: add process to built in test --------- Co-authored-by: Jennifer Shehane <[email protected]>
1 parent b75850e commit 49f64e7

File tree

13 files changed

+48
-1893
lines changed

13 files changed

+48
-1893
lines changed

.circleci/workflows.yml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mainBuildFilters: &mainBuildFilters
3838
- /^release\/\d+\.\d+\.\d+$/
3939
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
4040
- 'update-v8-snapshot-cache-on-develop'
41-
- 'update-chrome-stable-from-136.0.7103.113-beta-from-137.0.7151.40'
41+
- 'feat/add_built_ins_minimal'
4242

4343
# usually we don't build Mac app - it takes a long time
4444
# but sometimes we want to really confirm we are doing the right thing
@@ -49,11 +49,7 @@ macWorkflowFilters: &darwin-workflow-filters
4949
- equal: [ develop, << pipeline.git.branch >> ]
5050
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
5151
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
52-
- equal:
53-
[
54-
'update-chrome-stable-from-136.0.7103.113-beta-from-137.0.7151.40',
55-
<< pipeline.git.branch >>
56-
]
52+
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
5753
- matches:
5854
pattern: /^release\/\d+\.\d+\.\d+$/
5955
value: << pipeline.git.branch >>
@@ -64,11 +60,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters
6460
- equal: [ develop, << pipeline.git.branch >> ]
6561
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
6662
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
67-
- equal:
68-
[
69-
'update-chrome-stable-from-136.0.7103.113-beta-from-137.0.7151.40',
70-
<< pipeline.git.branch >>
71-
]
63+
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
7264
- matches:
7365
pattern: /^release\/\d+\.\d+\.\d+$/
7466
value: << pipeline.git.branch >>
@@ -91,11 +83,7 @@ windowsWorkflowFilters: &windows-workflow-filters
9183
- equal: [ develop, << pipeline.git.branch >> ]
9284
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
9385
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
94-
- equal:
95-
[
96-
'update-chrome-stable-from-136.0.7103.113-beta-from-137.0.7151.40',
97-
<< pipeline.git.branch >>
98-
]
86+
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
9987
- matches:
10088
pattern: /^release\/\d+\.\d+\.\d+$/
10189
value: << pipeline.git.branch >>
@@ -169,7 +157,7 @@ commands:
169157
name: Set environment variable to determine whether or not to persist artifacts
170158
command: |
171159
echo "Setting SHOULD_PERSIST_ARTIFACTS variable"
172-
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "update-chrome-stable-from-136.0.7103.113-beta-from-137.0.7151.40" ]]; then
160+
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/add_built_ins_minimal" ]]; then
173161
export SHOULD_PERSIST_ARTIFACTS=true
174162
fi' >> "$BASH_ENV"
175163
# You must run `setup_should_persist_artifacts` command and be using bash before running this command

cli/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _Released 07/01/2025 (PENDING)_
1313
- The Cypress configuration wizard for Component Testing supports TypeScript 5.0 or greater. Addresses [#31187](https://github.com/cypress-io/cypress/issues/31187).
1414
- `@cypress/webpack-dev-server` and `@cypress/webpack-preprocessor` no longer support `webpack` version 4. Addresses [#31344](https://github.com/cypress-io/cypress/issues/31344). If you still need to use `webpack` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150).
1515
- `@cypress/webpack-dev-server` no longer supports `webpack-dev-server` version 4. Addresses [#31605](https://github.com/cypress-io/cypress/issues/31605). If you still need to use `webpack-dev-server` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150).
16-
- The `@cypress/webpack-batteries-included-preprocessor` no longer includes browser built-ins that were automatically provided by Webpack 4. To add built-ins manually, refer to the Webpack [resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) documentation and the [`@cypress/webpack-batteries-included-preprocessor` README](../npm/webpack-batteries-included-preprocessor/README.md). Addresses [#31039](https://github.com/cypress-io/cypress/issues/31039).
16+
- In order to better align with best practices, `@cypress/webpack-batteries-included-preprocessor` no longer includes certain browser built-ins that were automatically provided by Webpack 4. The removed built-ins are `assert`, `constants`, `crypto`, `domain`, `events`, `http`, `https`, `punycode`, `querystring`, `string_decoder`, `sys`, `timers`, `tty`, `url`, `util`, `vm`, and `zlib`. However, we know that certain built-ins are popular, given that many users have files that are shared between their Cypress tests and node context. Because of this, `@cypress/webpack-batteries-included-preprocessor` will ship with built-in support for `buffer`, `path`, `process`, `os`, and `stream`. If there is a built-in that isn't supported be default and you need to add support, please refer to the Webpack [resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) documentation and the [`@cypress/webpack-batteries-included-preprocessor` README](../npm/webpack-batteries-included-preprocessor/README.md). Addresses [#31039](https://github.com/cypress-io/cypress/issues/31039).
1717
- The application under test's `pagehide` event in Chromium browsers will no longer trigger Cypress's `window:unload` event. Addressed in [#31853](https://github.com/cypress-io/cypress/pull/31853).
1818

1919
**Features:**

npm/webpack-batteries-included-preprocessor/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = (on) => {
4444
}
4545
```
4646

47-
As of version `4.x.x`, `@cypress/webpack-batteries-included-preprocessor` no longer includes Webpack built-ins by default. If your project requires them, you can retrieve the preprocessor's default Webpack options and decorate them as needed.
47+
As of version `4.x.x`, `@cypress/webpack-batteries-included-preprocessor` only includes the `buffer`, `path`, `process`, `os`, and `stream` built-ins. If your project requires built-ins not provided, you can retrieve the preprocessor's default Webpack options and decorate them as needed.
4848

4949
```javascript
5050
const webpackPreprocessor = require('@cypress/webpack-batteries-included-preprocessor')
@@ -53,7 +53,7 @@ function getWebpackOptions () {
5353
const options = webpackPreprocessor.getFullWebpackOptions()
5454

5555
// add built-ins as needed
56-
options.resolve.fallback.path = require.resolve('path-browserify')
56+
options.resolve.fallback.zlib = require.resolve('browserify-zlib')
5757

5858
return options
5959
}

npm/webpack-batteries-included-preprocessor/index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const Debug = require('debug')
33
const getTsConfig = require('get-tsconfig')
4+
const webpack = require('webpack')
45
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
56
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
67

@@ -135,6 +136,24 @@ const getDefaultWebpackOptions = () => {
135136
}],
136137
},
137138
plugins: [
139+
new webpack.ProvidePlugin({
140+
Buffer: ['buffer', 'Buffer'],
141+
// As of Webpack 5, a new option called resolve.fullySpecified, was added.
142+
// This option means that a full path, in particular to .mjs / .js files
143+
// in ESM packages must have the full path of an import specified.
144+
// Otherwise, compilation fails as this option defaults to true.
145+
// This means we need to adjust our global injections to always
146+
// resolve to include the full file extension if a file resolution is provided.
147+
// @see https://github.com/cypress-io/cypress/issues/27599
148+
// @see https://webpack.js.org/configuration/module/#resolvefullyspecified
149+
150+
// Due to Pnp compatibility issues, we want to make sure that we resolve to the 'process' library installed with the binary,
151+
// which should resolve on leaf app/packages/server/node_modules/@cypress/webpack-batteries-included-preprocessor and up the tree.
152+
// In other words, we want to resolve 'process' that is installed with cypress (or the package itself, i.e. @cypress/webpack-batteries-included-preprocessor)
153+
// and not in the user's node_modules directory as it may not exist.
154+
// @see https://github.com/cypress-io/cypress/issues/27947.
155+
process: require.resolve('process/browser.js'),
156+
}),
138157
// If the user is trying to debug their bundle, we'll add the BundleAnalyzerPlugin
139158
// to see the size of the support file (first bundle when running `cypress open`)
140159
// and spec files (subsequent bundles when running `cypress open`)
@@ -144,7 +163,7 @@ const getDefaultWebpackOptions = () => {
144163
extensions: ['.js', '.json', '.jsx', '.mjs', '.coffee'],
145164
fallback: {
146165
assert: false,
147-
buffer: false,
166+
buffer: require.resolve('buffer/'),
148167
child_process: false,
149168
cluster: false,
150169
console: false,
@@ -161,15 +180,15 @@ const getDefaultWebpackOptions = () => {
161180
inspector: false,
162181
module: false,
163182
net: false,
164-
os: false,
165-
path: false,
183+
os: require.resolve('os-browserify/browser'),
184+
path: require.resolve('path-browserify'),
166185
perf_hooks: false,
167186
punycode: false,
168-
process: false,
187+
process: require.resolve('process/browser.js'),
169188
querystring: false,
170189
readline: false,
171190
repl: false,
172-
stream: false,
191+
stream: require.resolve('stream-browserify'),
173192
string_decoder: false,
174193
sys: false,
175194
timers: false,

npm/webpack-batteries-included-preprocessor/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
"@babel/runtime": "^7.25.0",
1818
"babel-loader": "^10.0.0",
1919
"babel-plugin-add-module-exports": "^1.0.2",
20+
"buffer": "^6.0.3",
2021
"coffee-loader": "^4.0.0",
2122
"coffeescript": "2.6.0",
2223
"debug": "^4.3.4",
2324
"get-tsconfig": "^4.10.0",
25+
"os-browserify": "^0.3.0",
26+
"path-browserify": "^1.0.1",
27+
"process": "^0.11.10",
28+
"stream-browserify": "^3.0.0",
2429
"ts-loader": "9.5.2",
2530
"tsconfig-paths-webpack-plugin": "^3.5.2",
2631
"webpack": "^5.88.2",

npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ describe('webpack-batteries-included-preprocessor', () => {
4040
preprocessor = require('../../index')
4141
const result = preprocessor.getFullWebpackOptions('file/path', 'typescript/path')
4242

43-
expect(result.plugins).to.have.length(1)
44-
expect(result.plugins[0].constructor.name).to.equal('BundleAnalyzerPlugin')
43+
expect(result.plugins).to.have.length(2)
44+
expect(result.plugins[1].constructor.name).to.equal('BundleAnalyzerPlugin')
4545
Debug.disable()
4646
})
4747
})

packages/launchpad/cypress.config.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import { defineConfig } from 'cypress'
22
import { snapshotCypressDirectory } from './cypress/tasks/snapshotsScaffold'
33
import { uninstallDependenciesInScaffoldedProject } from './cypress/tasks/uninstallDependenciesInScaffoldedProject'
4-
import wbip from '@cypress/webpack-batteries-included-preprocessor'
5-
6-
function getWebpackOptions () {
7-
const options = wbip.getFullWebpackOptions()
8-
9-
// our tests need the path built-in for testing, so we need to shim it here into the webpack config
10-
options.resolve.fallback.path = require.resolve('path-browserify')
11-
12-
return options
13-
}
144

155
export default defineConfig({
166
projectId: 'ypt4pf',
@@ -47,8 +37,6 @@ export default defineConfig({
4737
uninstallDependenciesInScaffoldedProject,
4838
})
4939

50-
on('file:preprocessor', wbip({ webpackOptions: getWebpackOptions(), typescript: require.resolve('typescript') }))
51-
5240
return await e2ePluginSetup(on, config)
5341
},
5442
},

packages/launchpad/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"human-interval": "1.0.0",
6767
"javascript-time-ago": "2.3.8",
6868
"markdown-it": "13.0.1",
69-
"path-browserify": "1.0.1",
7069
"rollup-plugin-polyfill-node": "^0.7.0",
7170
"sinon": "13.0.2",
7271
"type-fest": "^2.3.4",

system-tests/projects/node-builtins/cypress/e2e/node_builtins.cy.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
it('does not support any node builtins', () => {
1+
it('only supports a handful of built-ins that are popular amongst cypress projects', () => {
22
expect(require('assert')).to.be.eql({})
3-
expect(require('buffer')).to.be.eql({})
3+
expect(require('buffer')).to.be.an('object').and.have.property('Buffer')
44
expect(require('child_process')).to.be.eql({})
55
expect(require('cluster')).to.be.eql({})
66
expect(require('console')).to.be.eql({})
@@ -17,14 +17,15 @@ it('does not support any node builtins', () => {
1717
expect(require('inspector')).to.be.eql({})
1818
expect(require('module')).to.be.eql({})
1919
expect(require('net')).to.be.eql({})
20-
expect(require('os')).to.be.eql({})
21-
expect(require('path')).to.be.eql({})
20+
expect(require('os')).to.be.an('object').and.have.property('platform')
21+
expect(require('path')).to.be.an('object').and.have.property('join')
2222
expect(require('perf_hooks')).to.eql({})
23+
expect(require('process')).to.be.an('object').and.have.property('env')
2324
expect(require('punycode')).to.be.eql({})
2425
expect(require('querystring')).to.be.eql({})
2526
expect(require('readline')).to.be.eql({})
2627
expect(require('repl')).to.be.eql({})
27-
expect(require('stream')).to.be.eql({})
28+
expect(require('stream')).to.be.a('function').and.have.property('Readable')
2829
expect(require('string_decoder')).to.be.eql({})
2930
expect(require('sys')).to.be.eql({})
3031
expect(require('timers')).to.be.eql({})

system-tests/projects/screenshots/cypress.config.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
const path = require('path')
22
const Jimp = require('jimp')
33
const _ = require('lodash')
4-
const wbip = require('@cypress/webpack-batteries-included-preprocessor')
54
const { useFixedBrowserLaunchSize } = require('@tooling/system-tests/lib/pluginUtils')
65

7-
function getWebpackOptions () {
8-
const options = wbip.getFullWebpackOptions()
9-
10-
// our tests need the path built-in for testing, so we need to shim it here into the webpack config
11-
options.resolve.fallback.path = require.resolve('path-browserify')
12-
13-
return options
14-
}
15-
166
module.exports = {
177
'e2e': {
188
setupNodeEvents (on, config) {
@@ -22,8 +12,6 @@ module.exports = {
2212
return options
2313
})
2414

25-
on('file:preprocessor', wbip({ webpackOptions: getWebpackOptions() }))
26-
2715
on('task', {
2816
'ensure:pixel:color' ({ name, colors, devicePixelRatio }) {
2917
const imagePath = path.join(__dirname, 'cypress', 'screenshots', `${name}.png`)
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
{
22
"name": "screenshots",
3-
"version": "0.0.0",
4-
"dependencies": {
5-
"@cypress/webpack-batteries-included-preprocessor": "file:../../../npm/webpack-batteries-included-preprocessor",
6-
"@cypress/webpack-preprocessor": "file:../../../npm/webpack-preprocessor",
7-
"path-browserify": "1.0.1"
8-
}
3+
"version": "0.0.0"
94
}

0 commit comments

Comments
 (0)