-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: change default options for sourceMaps inside WBIP #31270
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
Changes from all commits
763ee9e
2539033
261e5a1
d509b71
1b0b5ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
const path = require('path') | ||
const fs = require('fs-extra') | ||
const JSON5 = require('json5') | ||
const webpack = require('webpack') | ||
const Debug = require('debug') | ||
const webpackPreprocessor = require('@cypress/webpack-preprocessor') | ||
|
@@ -17,38 +15,6 @@ const hasTsLoader = (rules) => { | |
}) | ||
} | ||
|
||
const getTSCompilerOptionsForUser = (configFilePath) => { | ||
const compilerOptions = { | ||
sourceMap: false, | ||
inlineSourceMap: true, | ||
inlineSources: true, | ||
downlevelIteration: true, | ||
} | ||
|
||
if (!configFilePath) { | ||
return compilerOptions | ||
} | ||
|
||
try { | ||
// If possible, try to read the user's tsconfig.json and see if sourceMap is configured | ||
// eslint-disable-next-line no-restricted-syntax | ||
const tsconfigJSON = fs.readFileSync(configFilePath, 'utf8') | ||
// file might have trailing commas, new lines, etc. JSON5 can parse those correctly | ||
const parsedJSON = JSON5.parse(tsconfigJSON) | ||
|
||
// if the user has sourceMap's configured, set the option to true and turn off inlineSourceMaps | ||
if (parsedJSON?.compilerOptions?.sourceMap) { | ||
compilerOptions.sourceMap = true | ||
compilerOptions.inlineSourceMap = false | ||
compilerOptions.inlineSources = false | ||
} | ||
} catch (e) { | ||
debug(`error in getTSCompilerOptionsForUser. Returning default...`, e) | ||
} finally { | ||
return compilerOptions | ||
} | ||
} | ||
|
||
const addTypeScriptConfig = (file, options) => { | ||
// shortcut if we know we've already added typescript support | ||
if (options.__typescriptSupportAdded) return options | ||
|
@@ -60,14 +26,21 @@ const addTypeScriptConfig = (file, options) => { | |
if (!rules || !Array.isArray(rules)) return options | ||
|
||
// if we find ts-loader configured, don't add it again | ||
if (hasTsLoader(rules)) return options | ||
if (hasTsLoader(rules)) { | ||
debug('ts-loader already configured, not adding again') | ||
|
||
return options | ||
} | ||
|
||
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin') | ||
// node will try to load a projects tsconfig.json instead of the node | ||
// package using require('tsconfig'), so we alias it as 'tsconfig-aliased-for-wbip' | ||
const configFile = require('tsconfig-aliased-for-wbip').findSync(path.dirname(file.filePath)) | ||
|
||
const compilerOptions = getTSCompilerOptionsForUser(configFile) | ||
const getTsConfig = require('get-tsconfig') | ||
|
||
// returns null if tsconfig cannot be found in the path/parent hierarchy | ||
const configFile = getTsConfig.getTsconfig(file.filePath) | ||
|
||
configFile ? debug(`found user tsconfig.json at ${configFile?.path} with compilerOptions: ${JSON.stringify(configFile?.config?.compilerOptions)}`) : debug('no user tsconfig.json found') | ||
|
||
webpackOptions.module.rules.push({ | ||
test: /\.tsx?$/, | ||
|
@@ -77,7 +50,6 @@ const addTypeScriptConfig = (file, options) => { | |
loader: require.resolve('ts-loader'), | ||
options: { | ||
compiler: options.typescript, | ||
compilerOptions, | ||
|
||
logLevel: 'error', | ||
silent: true, | ||
transpileOnly: true, | ||
|
@@ -88,7 +60,7 @@ const addTypeScriptConfig = (file, options) => { | |
|
||
webpackOptions.resolve.extensions = webpackOptions.resolve.extensions.concat(['.ts', '.tsx']) | ||
webpackOptions.resolve.plugins = [new TsconfigPathsPlugin({ | ||
configFile, | ||
configFile: configFile?.path, | ||
silent: true, | ||
})] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,8 @@ | |
"debug": "^4.3.4", | ||
"domain-browser": "^4.22.0", | ||
"events": "^3.3.0", | ||
"fs-extra": "^9.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
"get-tsconfig": "^4.10.0", | ||
"https-browserify": "^1.0.0", | ||
"json5": "2.2.3", | ||
"os-browserify": "^0.3.0", | ||
"path-browserify": "^1.0.1", | ||
"process": "^0.11.10", | ||
|
@@ -39,8 +38,7 @@ | |
"stream-http": "^3.2.0", | ||
"string_decoder": "1.3.0", | ||
"timers-browserify": "^2.0.12", | ||
"ts-loader": "9.4.4", | ||
"tsconfig-aliased-for-wbip": "npm:tsconfig@^7.0.0", | ||
"ts-loader": "9.5.2", | ||
"tsconfig-paths-webpack-plugin": "^3.5.2", | ||
"tty-browserify": "^0.0.1", | ||
"url": "^0.11.1", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,72 @@ import webpack from 'webpack' | |
import utils from './lib/utils' | ||
import { overrideSourceMaps } from './lib/typescript-overrides' | ||
|
||
const getTsLoaderIfExists = (rules) => { | ||
|
||
let tsLoaderRule | ||
|
||
rules.some((rule) => { | ||
mschile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!rule.use && !rule.loader) return false | ||
|
||
if (Array.isArray(rule.use)) { | ||
const foundRule = rule.use.find((use) => { | ||
return use.loader && use.loader.includes('ts-loader') | ||
}) | ||
|
||
/** | ||
* If the rule is found, it will look like this: | ||
* rules: [ | ||
* { | ||
* test: /\.tsx?$/, | ||
* exclude: [/node_modules/], | ||
* use: [{ | ||
* loader: 'ts-loader' | ||
* }] | ||
* } | ||
* ] | ||
*/ | ||
tsLoaderRule = foundRule | ||
|
||
return tsLoaderRule | ||
} | ||
|
||
if (_.isObject(rule.use) && rule.use.loader && rule.use.loader.includes('ts-loader')) { | ||
/** | ||
* If the rule is found, it will look like this: | ||
* rules: [ | ||
* { | ||
* test: /\.tsx?$/, | ||
* exclude: [/node_modules/], | ||
* use: { | ||
* loader: 'ts-loader' | ||
* } | ||
* } | ||
* ] | ||
*/ | ||
tsLoaderRule = rule.use | ||
|
||
return tsLoaderRule | ||
} | ||
|
||
tsLoaderRule = rules.find((rule) => { | ||
/** | ||
* If the rule is found, it will look like this: | ||
* rules: [ | ||
* { | ||
* test: /\.tsx?$/, | ||
* exclude: [/node_modules/], | ||
* loader: 'ts-loader' | ||
* } | ||
* ] | ||
*/ | ||
return rule.loader && rule.loader.includes('ts-loader') | ||
}) | ||
|
||
return tsLoaderRule | ||
}) | ||
|
||
return tsLoaderRule | ||
} | ||
|
||
const debug = Debug('cypress:webpack') | ||
const debugStats = Debug('cypress:webpack:stats') | ||
|
||
|
@@ -208,6 +274,37 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F | |
filename: path.basename(outputPath), | ||
}, | ||
}) | ||
.tap((opts) => { | ||
try { | ||
const tsLoaderRule = getTsLoaderIfExists(opts?.module?.rules) | ||
|
||
if (!tsLoaderRule) { | ||
debug('ts-loader not detected') | ||
|
||
return | ||
} | ||
|
||
// FIXME: To prevent disruption, we are only passing in these 4 options to the ts-loader. | ||
// We will be passing in the entire compilerOptions object from the tsconfig.json in Cypress 15. | ||
// @see https://github.com/cypress-io/cypress/issues/29614#issuecomment-2722071332 | ||
// @see https://github.com/cypress-io/cypress/issues/31282 | ||
// Cypress ALWAYS wants sourceMap set to true, regardless of the user configuration. | ||
// This is because we want to display a correct code frame in the test runner. | ||
debug(`ts-loader detected: overriding tsconfig to use sourceMap:true, inlineSourceMap:false, inlineSources:false, downlevelIteration:true`) | ||
|
||
tsLoaderRule.options = tsLoaderRule?.options || {} | ||
tsLoaderRule.options.compilerOptions = tsLoaderRule.options?.compilerOptions || {} | ||
|
||
tsLoaderRule.options.compilerOptions.sourceMap = true | ||
tsLoaderRule.options.compilerOptions.inlineSourceMap = false | ||
tsLoaderRule.options.compilerOptions.inlineSources = false | ||
tsLoaderRule.options.compilerOptions.downlevelIteration = true | ||
} catch (e) { | ||
debug('ts-loader not detected', e) | ||
|
||
return | ||
} | ||
}) | ||
.tap((opts) => { | ||
if (opts.devtool === false) { | ||
// disable any overrides if we've explicitly turned off sourcemaps | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we aren't doing anything with this until #31282 except feeding the webpack plugin the tsconfig file path