Skip to content

Commit 13464eb

Browse files
committed
remove certain built-ins from the preprocessor and see what breaks
Remove ALL built ins and see what falls over
1 parent b7de30a commit 13464eb

File tree

21 files changed

+2062
-269
lines changed

21 files changed

+2062
-269
lines changed

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

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path')
2-
const webpack = require('webpack')
32
const Debug = require('debug')
43
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
54

@@ -117,65 +116,45 @@ const getDefaultWebpackOptions = () => {
117116
loader: require.resolve('coffee-loader'),
118117
}],
119118
},
120-
plugins: [
121-
new webpack.ProvidePlugin({
122-
Buffer: ['buffer', 'Buffer'],
123-
// As of Webpack 5, a new option called resolve.fullySpecified, was added.
124-
// This option means that a full path, in particular to .mjs / .js files
125-
// in ESM packages must have the full path of an import specified.
126-
// Otherwise, compilation fails as this option defaults to true.
127-
// This means we need to adjust our global injections to always
128-
// resolve to include the full file extension if a file resolution is provided.
129-
// @see https://github.com/cypress-io/cypress/issues/27599
130-
// @see https://webpack.js.org/configuration/module/#resolvefullyspecified
131-
132-
// Due to Pnp compatibility issues, we want to make sure that we resolve to the 'process' library installed with the binary,
133-
// which should resolve on leaf app/packages/server/node_modules/@cypress/webpack-batteries-included-preprocessor and up the tree.
134-
// In other words, we want to resolve 'process' that is installed with cypress (or the package itself, i.e. @cypress/webpack-batteries-included-preprocessor)
135-
// and not in the user's node_modules directory as it may not exist.
136-
// @see https://github.com/cypress-io/cypress/issues/27947.
137-
process: require.resolve('process/browser.js'),
138-
}),
139-
],
140119
resolve: {
141120
extensions: ['.js', '.json', '.jsx', '.mjs', '.coffee'],
142121
fallback: {
143-
assert: require.resolve('assert/'),
144-
buffer: require.resolve('buffer/'),
122+
assert: false,
123+
buffer: false,
145124
child_process: false,
146125
cluster: false,
147126
console: false,
148-
constants: require.resolve('constants-browserify'),
149-
crypto: require.resolve('crypto-browserify'),
127+
constants: false,
128+
crypto: false,
150129
dgram: false,
151130
dns: false,
152-
domain: require.resolve('domain-browser'),
153-
events: require.resolve('events/'),
131+
domain: false,
132+
events: false,
154133
fs: false,
155-
http: require.resolve('stream-http'),
156-
https: require.resolve('https-browserify'),
134+
http: false,
135+
https: false,
157136
http2: false,
158137
inspector: false,
159138
module: false,
160139
net: false,
161-
os: require.resolve('os-browserify/browser'),
162-
path: require.resolve('path-browserify'),
140+
os: false,
141+
path: false,
163142
perf_hooks: false,
164-
punycode: require.resolve('punycode/'),
165-
process: require.resolve('process/browser.js'),
166-
querystring: require.resolve('querystring-es3'),
143+
punycode: false,
144+
process: false,
145+
querystring: false,
167146
readline: false,
168147
repl: false,
169-
stream: require.resolve('stream-browserify'),
170-
string_decoder: require.resolve('string_decoder/'),
171-
sys: require.resolve('util/'),
172-
timers: require.resolve('timers-browserify'),
148+
stream: false,
149+
string_decoder: false,
150+
sys: false,
151+
timers: false,
173152
tls: false,
174-
tty: require.resolve('tty-browserify'),
175-
url: require.resolve('url/'),
176-
util: require.resolve('util/'),
177-
vm: require.resolve('vm-browserify'),
178-
zlib: require.resolve('browserify-zlib'),
153+
tty: false,
154+
url: false,
155+
util: false,
156+
vm: false,
157+
zlib: false,
179158
},
180159
plugins: [],
181160
},
@@ -187,7 +166,18 @@ const typescriptExtensionRegex = /\.tsx?$/
187166
const preprocessor = (options = {}) => {
188167
return (file) => {
189168
if (!options.typescript && typescriptExtensionRegex.test(file.filePath)) {
190-
return Promise.reject(new Error(`You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support.\n\nThe file: ${file.filePath}`))
169+
// Attempt to discover the user's typescript path assuming they aren't explicitly opting out of TypeScript
170+
if (options.typescript !== false) {
171+
try {
172+
const typescriptPath = require.resolve('typescript', { paths: [file.filePath] })
173+
174+
options.typescript = typescriptPath
175+
} catch (err) {
176+
return Promise.reject(new Error(`You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support.\n\nThe file: ${file.filePath}`))
177+
}
178+
} else {
179+
return Promise.reject(new Error(`You are attempting to run a TypeScript file, but do not have TypeScript transpilation enabled. Please either pass in your typescript path or set typescript to true.\n\nThe file: ${file.filePath}`))
180+
}
191181
}
192182

193183
options.webpackOptions = options.webpackOptions || getDefaultWebpackOptions()

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,14 @@
1515
"@babel/preset-env": "^7.25.3",
1616
"@babel/preset-react": "^7.24.7",
1717
"@babel/runtime": "^7.25.0",
18-
"assert": "^2.0.0",
1918
"babel-loader": "^10.0.0",
2019
"babel-plugin-add-module-exports": "^1.0.2",
21-
"browserify-zlib": "^0.2.0",
22-
"buffer": "^6.0.3",
2320
"coffee-loader": "^4.0.0",
2421
"coffeescript": "2.6.0",
25-
"constants-browserify": "^1.0.0",
26-
"crypto-browserify": "^3.12.0",
2722
"debug": "^4.3.4",
28-
"domain-browser": "^4.22.0",
29-
"events": "^3.3.0",
3023
"get-tsconfig": "^4.10.0",
31-
"https-browserify": "^1.0.0",
32-
"os-browserify": "^0.3.0",
33-
"path-browserify": "^1.0.1",
34-
"process": "^0.11.10",
35-
"punycode": "^2.3.0",
36-
"querystring-es3": "^0.2.1",
37-
"stream-browserify": "^3.0.0",
38-
"stream-http": "^3.2.0",
39-
"string_decoder": "1.3.0",
40-
"timers-browserify": "^2.0.12",
4124
"ts-loader": "9.5.2",
4225
"tsconfig-paths-webpack-plugin": "^3.5.2",
43-
"tty-browserify": "^0.0.1",
44-
"url": "^0.11.1",
45-
"util": "^0.12.5",
46-
"vm-browserify": "^1.1.2",
4726
"webpack": "^5.88.2"
4827
},
4928
"devDependencies": {

npm/webpack-batteries-included-preprocessor/test/e2e/features.spec.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ describe('webpack-batteries-included-preprocessor features', () => {
6060
await runAndEval('node_shim_spec.js')
6161
})
6262

63-
it('shims node builtins', async () => {
64-
await runAndEval('node_builtins_spec.js')
65-
})
63+
// Node built-ins are tested in the /cypress/system-tests/test/plugins_spec.js system test. Built-ins needs to be tested there as the preprocessor execution
64+
// code runs in the browser as opposed to an eval() in the node environment (like these unit tests).
65+
// For instance, require('events') is not available in the browser but is available in the eval() node context
66+
// because the 'events' dependency is installed by another dependency and webpack is smart enough to find and source it in the node_modules tree.
6667

6768
it('outputs inline source map', async () => {
6869
const outputPath = await run('es_features_spec.js')
@@ -105,20 +106,24 @@ describe('webpack-batteries-included-preprocessor features', () => {
105106
await runAndEval('tsx_spec.tsx', { ...options, ...preprocessor.defaultOptions })
106107
})
107108

108-
it('errors when processing .ts file and typescript option is not set', () => {
109-
return run('ts_spec.ts')
109+
it('errors when processing .ts file and typescript option is set explicitly to false', () => {
110+
return run('ts_spec.ts', {
111+
typescript: false,
112+
})
110113
.then(shouldntResolve)
111114
.catch((err) => {
112-
expect(err.message).to.include(`You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support`)
115+
expect(err.message).to.include(`You are attempting to run a TypeScript file, but do not have TypeScript transpilation enabled. Please either pass in your typescript path or set typescript to true.`)
113116
expect(err.message).to.include('ts_spec.ts')
114117
})
115118
})
116119

117-
it('errors when processing .tsx file and typescript option is not set', () => {
118-
return run('tsx_spec.tsx')
120+
it('errors when processing .tsx file and typescript option is set explicitly to false', () => {
121+
return run('tsx_spec.tsx', {
122+
typescript: false,
123+
})
119124
.then(shouldntResolve)
120125
.catch((err) => {
121-
expect(err.message).to.include(`You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support`)
126+
expect(err.message).to.include(`You are attempting to run a TypeScript file, but do not have TypeScript transpilation enabled. Please either pass in your typescript path or set typescript to true.`)
122127
expect(err.message).to.include('tsx_spec.tsx')
123128
})
124129
})

npm/webpack-batteries-included-preprocessor/test/fixtures/node_builtins_spec.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/launchpad/cypress.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
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+
}
414

515
export default defineConfig({
616
projectId: 'ypt4pf',
@@ -37,6 +47,8 @@ export default defineConfig({
3747
uninstallDependenciesInScaffoldedProject,
3848
})
3949

50+
on('file:preprocessor', wbip({ webpackOptions: getWebpackOptions() }))
51+
4052
return await e2ePluginSetup(on, config)
4153
},
4254
},

packages/launchpad/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
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",
6970
"rollup-plugin-polyfill-node": "^0.7.0",
7071
"sinon": "13.0.2",
7172
"type-fest": "^2.3.4",

packages/server/lib/plugins/child/run_plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class RunPlugins {
264264
...tsPath && { typescript: tsPath },
265265
}
266266

267-
debug('creating webpack preprocessor with options %o', options)
267+
debug('creating webpack batteries included preprocessor with options %o', options)
268268

269269
const webpackPreprocessor = require('@cypress/webpack-batteries-included-preprocessor')
270270

packages/web-config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@svgr/webpack": "8.0.1",
1717
"@types/webpack": "^5.28.1",
1818
"arraybuffer-loader": "1.0.8",
19+
"assert": "2.0.0",
1920
"autoprefixer": "10.4.20",
2021
"babel-loader": "10.0.0",
2122
"browser-resolve": "2.0.0",

packages/web-config/webpack.config.base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export const getCommonConfig = () => {
121121
extensions: ['.ts', '.js', '.jsx', '.tsx', '.scss', '.json'],
122122
// see https://gist.github.com/ef4/d2cf5672a93cf241fd47c020b9b3066a for polyfill migration details
123123
fallback: {
124+
assert: require.resolve('assert/'),
124125
buffer: require.resolve('buffer/'),
125126
child_process: false,
126127
fs: false,

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)