Skip to content

Commit db0d256

Browse files
committed
misc: convert @cypress/webpack-batteries-included-preprocessor source to TypeScript
make filePath and typescript args optional
1 parent a5a0dae commit db0d256

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

guides/esm-migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ When migrating some of these projects away from the `ts-node` entry [see `@packa
3434
- [x] npm/vite-dev-server ✅ **COMPLETED**
3535
- [x] vite-plugin-cypress-esm ✅ **COMPLETED**
3636
- [x] npm/vue ✅ **COMPLETED**
37-
- [ ] npm/webpack-batteries-included-preprocessor
37+
- [x] npm/webpack-batteries-included-preprocessor**COMPLETED**
3838
- [x] npm/webpack-dev-server ✅ **COMPLETED**
3939
- [ ] npm/webpack-preprocessor **PARTIAL**
4040

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index.js
2+
index.d.ts

npm/webpack-batteries-included-preprocessor/eslint.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import globals from 'globals'
44
export default [
55
...baseConfig,
66
{
7-
ignores: ['test/fixtures/**/*', 'test/_test-output/**'],
7+
ignores: ['test/fixtures/**/*', 'test/_test-output/**', 'index.js', 'index.d.ts'],
88
},
99
{
1010
files: ['**/*.js', '**/*.ts'],

npm/webpack-batteries-included-preprocessor/index.js renamed to npm/webpack-batteries-included-preprocessor/index.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
const path = require('path')
2-
const Debug = require('debug')
3-
const getTsConfig = require('get-tsconfig')
4-
const webpack = require('webpack')
5-
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
6-
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
1+
import path from 'path'
2+
import Debug from 'debug'
3+
import type { EventEmitter } from 'events'
4+
import getTsConfig from 'get-tsconfig'
5+
import webpack from 'webpack'
6+
import webpackPreprocessor from '@cypress/webpack-preprocessor'
7+
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
78

89
const debug = Debug('cypress:webpack-batteries-included-preprocessor')
910
const WBADebugNamespace = 'cypress-verbose:webpack-batteries-included-preprocessor:bundle-analyzer'
1011

12+
// NOTE: these types are duplicated from @cypress/webpack-preprocessor as we are unable to currently export them from the main entry.
13+
interface FileEvent extends EventEmitter {
14+
filePath: string
15+
outputPath: string
16+
shouldWatch: boolean
17+
}
18+
1119
class TsConfigNotFoundError extends Error {
1220
constructor () {
1321
super('No tsconfig.json found. ts-loader needs a tsconfig.json file to work. Please add one to your project in either the root or the cypress directory.')
@@ -24,17 +32,20 @@ class TypeScriptNotFoundError extends Error {
2432

2533
const typescriptExtensionRegex = /\.m?tsx?$/
2634

27-
const hasTsLoader = (rules) => {
35+
const hasTsLoader = (rules: any[]) => {
2836
return rules.some((rule) => {
2937
if (!rule.use || !Array.isArray(rule.use)) return false
3038

31-
return rule.use.some((use) => {
39+
return rule.use.some((use: any) => {
3240
return use.loader && use.loader.match(/(^|[^a-zA-Z])ts-loader([^a-zA-Z]|$)/)
3341
})
3442
})
3543
}
3644

37-
const addTypeScriptConfig = (file, options) => {
45+
const addTypeScriptConfig = (file: { filePath: string }, options: {
46+
typescript?: string | boolean
47+
webpackOptions?: any
48+
}) => {
3849
// returns null if tsconfig cannot be found in the path/parent hierarchy
3950
const configFile = getTsConfig.getTsconfig(file.filePath)
4051

@@ -46,14 +57,15 @@ const addTypeScriptConfig = (file, options) => {
4657

4758
debug(`found user tsconfig.json at ${configFile?.path} with compilerOptions: ${JSON.stringify(configFile?.config?.compilerOptions)}`)
4859

49-
let typeScriptPath = null
60+
let typeScriptPath: string | boolean | undefined | null = null
5061

5162
try {
5263
if (options.typescript === true) {
53-
const configFileDirectory = path.dirname(configFile?.path)
64+
const configFileDirectory = path.dirname(configFile?.path ?? '')
5465

5566
// attempt to resolve typescript from the user's tsconfig.json file / project directory
5667
typeScriptPath = require.resolve('typescript', { paths: [configFileDirectory] })
68+
options.typescript = typeScriptPath
5769
} else {
5870
typeScriptPath = options.typescript
5971
}
@@ -65,6 +77,7 @@ const addTypeScriptConfig = (file, options) => {
6577
throw new TypeScriptNotFoundError()
6678
}
6779
// shortcut if we know we've already added typescript support
80+
// @ts-expect-error - not typed intentionally
6881
if (options.__typescriptSupportAdded) return options
6982

7083
const webpackOptions = options.webpackOptions
@@ -117,6 +130,7 @@ const addTypeScriptConfig = (file, options) => {
117130
silent: true,
118131
})]
119132

133+
// @ts-expect-error - not typed intentionally
120134
options.__typescriptSupportAdded = true
121135

122136
return options
@@ -148,7 +162,7 @@ const getDefaultWebpackOptions = () => {
148162
'babel-plugin-add-module-exports',
149163
'@babel/plugin-transform-class-properties',
150164
'@babel/plugin-transform-object-rest-spread',
151-
].map(require.resolve),
165+
].map((plugin) => require.resolve(plugin)),
152166
[require.resolve('@babel/plugin-transform-runtime'), {
153167
absoluteRuntime: path.dirname(require.resolve('@babel/runtime/package')),
154168
}],
@@ -239,8 +253,11 @@ const getDefaultWebpackOptions = () => {
239253
}
240254
}
241255

242-
const preprocessor = (options = {}) => {
243-
return (file) => {
256+
const preprocessor = (options: {
257+
typescript?: string | boolean
258+
webpackOptions?: any
259+
} = {}) => {
260+
return (file: FileEvent) => {
244261
if (!options.typescript && typescriptExtensionRegex.test(file.filePath)) {
245262
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}`))
246263
}
@@ -251,6 +268,7 @@ const preprocessor = (options = {}) => {
251268
options = addTypeScriptConfig(file, options)
252269
}
253270

271+
// @ts-expect-error - typescript is casted back to a string | undefined inside addTypeScriptConfig
254272
return webpackPreprocessor(options)(file)
255273
}
256274
}
@@ -260,17 +278,18 @@ preprocessor.defaultOptions = {
260278
watchOptions: {},
261279
}
262280

263-
preprocessor.getFullWebpackOptions = (filePath, typescript) => {
281+
preprocessor.getFullWebpackOptions = (filePath?: string, typescript?: string | boolean) => {
264282
const webpackOptions = getDefaultWebpackOptions()
265283

266-
if (typescript) {
284+
if (typescript && filePath) {
267285
return addTypeScriptConfig({ filePath }, { typescript, webpackOptions }).webpackOptions
268286
}
269287

270288
return webpackOptions
271289
}
272290

273291
// for testing purposes, but do not add this to the typescript interface
292+
// @ts-expect-error - not typed intentionally
274293
preprocessor.__reset = webpackPreprocessor.__reset
275294

276-
module.exports = preprocessor
295+
export = preprocessor

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
"version": "0.0.0-development",
44
"description": "Cypress preprocessor for bundling JavaScript via webpack with dependencies included and support for various ES features, TypeScript, and CoffeeScript",
55
"private": false,
6+
"main": "index.js",
7+
"types": "index.d.ts",
68
"scripts": {
9+
"build": "tsc || echo 'built, with errors'",
10+
"check-ts": "tsc --noEmit",
711
"lint": "eslint",
8-
"test": "mocha test/**/*.spec.* --timeout 4000"
12+
"test": "yarn build && mocha test/**/*.spec.* --timeout 4000"
913
},
1014
"dependencies": {
1115
"@babel/core": "^7.28.0",
@@ -52,7 +56,7 @@
5256
},
5357
"files": [
5458
"index.js",
55-
"empty.js"
59+
"index.d.ts"
5660
],
5761
"license": "MIT",
5862
"repository": {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2022",
4+
"module": "commonjs",
5+
"esModuleInterop": true,
6+
"strict": true,
7+
"skipLibCheck": true,
8+
"noImplicitAny": true,
9+
"declaration": true,
10+
},
11+
"include": [
12+
"index.ts"
13+
]
14+
}

0 commit comments

Comments
 (0)