Skip to content

chore: add minimal built-ins back in to @cypress/webpack-batteries-included-preprocessor #31815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/15.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mainBuildFilters: &mainBuildFilters
- /^release\/\d+\.\d+\.\d+$/
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- 'update-v8-snapshot-cache-on-develop'
- 'feat/wbip_full_resolve_ts_config'
- 'feat/add_built_ins_minimal'

# usually we don't build Mac app - it takes a long time
# but sometimes we want to really confirm we are doing the right thing
Expand All @@ -49,7 +49,7 @@ macWorkflowFilters: &darwin-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'feat/wbip_full_resolve_ts_config', << pipeline.git.branch >> ]
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand All @@ -60,7 +60,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'feat/wbip_full_resolve_ts_config', << pipeline.git.branch >> ]
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand All @@ -83,7 +83,7 @@ windowsWorkflowFilters: &windows-workflow-filters
- equal: [ develop, << pipeline.git.branch >> ]
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
- equal: [ 'feat/wbip_full_resolve_ts_config', << pipeline.git.branch >> ]
- equal: [ 'feat/add_built_ins_minimal', << pipeline.git.branch >> ]
- matches:
pattern: /^release\/\d+\.\d+\.\d+$/
value: << pipeline.git.branch >>
Expand Down Expand Up @@ -157,7 +157,7 @@ commands:
name: Set environment variable to determine whether or not to persist artifacts
command: |
echo "Setting SHOULD_PERSIST_ARTIFACTS variable"
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/wbip_full_resolve_ts_config" ]]; then
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/add_built_ins_minimal" ]]; then
export SHOULD_PERSIST_ARTIFACTS=true
fi' >> "$BASH_ENV"
# You must run `setup_should_persist_artifacts` command and be using bash before running this command
Expand Down
2 changes: 1 addition & 1 deletion cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _Released 07/01/2025 (PENDING)_
- The Cypress configuration wizard for Component Testing supports TypeScript 5.0 or greater. Addresses [#31187](https://github.com/cypress-io/cypress/issues/31187).
- `@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).
- `@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).
- 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).
- 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).

**Features:**

Expand Down
4 changes: 2 additions & 2 deletions npm/webpack-batteries-included-preprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = (on) => {
}
```

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.
As of version `4.x.x`, `@cypress/webpack-batteries-included-preprocessor` only includes the `buffer`, `path`, `process`, `os`, and the `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.

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

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

return options
}
Expand Down
29 changes: 24 additions & 5 deletions npm/webpack-batteries-included-preprocessor/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const Debug = require('debug')
const getTsConfig = require('get-tsconfig')
const webpack = require('webpack')
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

Expand Down Expand Up @@ -135,6 +136,24 @@ const getDefaultWebpackOptions = () => {
}],
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
// As of Webpack 5, a new option called resolve.fullySpecified, was added.
// This option means that a full path, in particular to .mjs / .js files
// in ESM packages must have the full path of an import specified.
// Otherwise, compilation fails as this option defaults to true.
// This means we need to adjust our global injections to always
// resolve to include the full file extension if a file resolution is provided.
// @see https://github.com/cypress-io/cypress/issues/27599
// @see https://webpack.js.org/configuration/module/#resolvefullyspecified

// Due to Pnp compatibility issues, we want to make sure that we resolve to the 'process' library installed with the binary,
// which should resolve on leaf app/packages/server/node_modules/@cypress/webpack-batteries-included-preprocessor and up the tree.
// In other words, we want to resolve 'process' that is installed with cypress (or the package itself, i.e. @cypress/webpack-batteries-included-preprocessor)
// and not in the user's node_modules directory as it may not exist.
// @see https://github.com/cypress-io/cypress/issues/27947.
process: require.resolve('process/browser.js'),
}),
// If the user is trying to debug their bundle, we'll add the BundleAnalyzerPlugin
// to see the size of the support file (first bundle when running `cypress open`)
// and spec files (subsequent bundles when running `cypress open`)
Expand All @@ -144,7 +163,7 @@ const getDefaultWebpackOptions = () => {
extensions: ['.js', '.json', '.jsx', '.mjs', '.coffee'],
fallback: {
assert: false,
buffer: false,
buffer: require.resolve('buffer/'),
child_process: false,
cluster: false,
console: false,
Expand All @@ -161,15 +180,15 @@ const getDefaultWebpackOptions = () => {
inspector: false,
module: false,
net: false,
os: false,
path: false,
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
perf_hooks: false,
punycode: false,
process: false,
process: require.resolve('process/browser.js'),
querystring: false,
readline: false,
repl: false,
stream: false,
stream: require.resolve('stream-browserify'),
string_decoder: false,
sys: false,
timers: false,
Expand Down
5 changes: 5 additions & 0 deletions npm/webpack-batteries-included-preprocessor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
"@babel/runtime": "^7.25.0",
"babel-loader": "^10.0.0",
"babel-plugin-add-module-exports": "^1.0.2",
"buffer": "^6.0.3",
"coffee-loader": "^4.0.0",
"coffeescript": "2.6.0",
"debug": "^4.3.4",
"get-tsconfig": "^4.10.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"ts-loader": "9.5.2",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"webpack": "^5.88.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('webpack-batteries-included-preprocessor', () => {
preprocessor = require('../../index')
const result = preprocessor.getFullWebpackOptions('file/path', 'typescript/path')

expect(result.plugins).to.have.length(1)
expect(result.plugins[0].constructor.name).to.equal('BundleAnalyzerPlugin')
expect(result.plugins).to.have.length(2)
expect(result.plugins[1].constructor.name).to.equal('BundleAnalyzerPlugin')
Debug.disable()
})
})
Expand Down
12 changes: 0 additions & 12 deletions packages/launchpad/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { defineConfig } from 'cypress'
import { snapshotCypressDirectory } from './cypress/tasks/snapshotsScaffold'
import { uninstallDependenciesInScaffoldedProject } from './cypress/tasks/uninstallDependenciesInScaffoldedProject'
import wbip from '@cypress/webpack-batteries-included-preprocessor'

function getWebpackOptions () {
const options = wbip.getFullWebpackOptions()

// our tests need the path built-in for testing, so we need to shim it here into the webpack config
options.resolve.fallback.path = require.resolve('path-browserify')

return options
}

export default defineConfig({
projectId: 'ypt4pf',
Expand Down Expand Up @@ -47,8 +37,6 @@ export default defineConfig({
uninstallDependenciesInScaffoldedProject,
})

on('file:preprocessor', wbip({ webpackOptions: getWebpackOptions(), typescript: require.resolve('typescript') }))

return await e2ePluginSetup(on, config)
},
},
Expand Down
1 change: 0 additions & 1 deletion packages/launchpad/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"human-interval": "1.0.0",
"javascript-time-ago": "2.3.8",
"markdown-it": "13.0.1",
"path-browserify": "1.0.1",
"rollup-plugin-polyfill-node": "^0.7.0",
"sinon": "13.0.2",
"type-fest": "^2.3.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
it('does not support any node builtins', () => {
it('only supports a handful of built-ins that are popular amongst cypress projects', () => {
expect(require('assert')).to.be.eql({})
expect(require('buffer')).to.be.eql({})
expect(require('buffer')).to.be.an('object').and.have.property('Buffer')
expect(require('child_process')).to.be.eql({})
expect(require('cluster')).to.be.eql({})
expect(require('console')).to.be.eql({})
Expand All @@ -17,14 +17,14 @@ it('does not support any node builtins', () => {
expect(require('inspector')).to.be.eql({})
expect(require('module')).to.be.eql({})
expect(require('net')).to.be.eql({})
expect(require('os')).to.be.eql({})
expect(require('path')).to.be.eql({})
expect(require('os')).to.be.an('object').and.have.property('platform')
expect(require('path')).to.be.an('object').and.have.property('join')
expect(require('perf_hooks')).to.eql({})
expect(require('punycode')).to.be.eql({})
expect(require('querystring')).to.be.eql({})
expect(require('readline')).to.be.eql({})
expect(require('repl')).to.be.eql({})
expect(require('stream')).to.be.eql({})
expect(require('stream')).to.be.a('function').and.have.property('Readable')
expect(require('string_decoder')).to.be.eql({})
expect(require('sys')).to.be.eql({})
expect(require('timers')).to.be.eql({})
Expand Down
12 changes: 0 additions & 12 deletions system-tests/projects/screenshots/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
const path = require('path')
const Jimp = require('jimp')
const _ = require('lodash')
const wbip = require('@cypress/webpack-batteries-included-preprocessor')
const { useFixedBrowserLaunchSize } = require('@tooling/system-tests/lib/pluginUtils')

function getWebpackOptions () {
const options = wbip.getFullWebpackOptions()

// our tests need the path built-in for testing, so we need to shim it here into the webpack config
options.resolve.fallback.path = require.resolve('path-browserify')

return options
}

module.exports = {
'e2e': {
setupNodeEvents (on, config) {
Expand All @@ -22,8 +12,6 @@ module.exports = {
return options
})

on('file:preprocessor', wbip({ webpackOptions: getWebpackOptions() }))

on('task', {
'ensure:pixel:color' ({ name, colors, devicePixelRatio }) {
const imagePath = path.join(__dirname, 'cypress', 'screenshots', `${name}.png`)
Expand Down
7 changes: 1 addition & 6 deletions system-tests/projects/screenshots/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"name": "screenshots",
"version": "0.0.0",
"dependencies": {
"@cypress/webpack-batteries-included-preprocessor": "file:../../../npm/webpack-batteries-included-preprocessor",
"@cypress/webpack-preprocessor": "file:../../../npm/webpack-preprocessor",
"path-browserify": "1.0.1"
}
"version": "0.0.0"
}
Loading