Skip to content
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

fix(webpack-dev-server): add typeRoots to generated tsconfig for angular #27117

Merged
merged 25 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b1e9722
feat(webpack-dev-server): generate a local tsconfig file
jordanpowell88 Jun 22, 2023
c8ab14f
chore: remove comments
jordanpowell88 Jun 23, 2023
5e8e19d
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jun 23, 2023
cf58585
chore: use relative paths
jordanpowell88 Jun 26, 2023
49923d3
Merge branch 'jordanpowell88/angular-tsconfig' of https://github.com/…
jordanpowell88 Jun 26, 2023
1f896b1
test(webpack-dev-server): update tsconfig tests
jordanpowell88 Jun 26, 2023
c8b808a
test(webpack-dev-server): update tsconfig tests
jordanpowell88 Jun 26, 2023
0d9a97a
test(webpack-dev-server): update tsconfig tests
jordanpowell88 Jun 26, 2023
52954af
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jun 26, 2023
a2d3987
Merge branch 'jordanpowell88/angular-tsconfig' of https://github.com/…
jordanpowell88 Jun 26, 2023
d3b8dd2
test(webpack-dev-server): upt outDir path
jordanpowell88 Jun 26, 2023
e8ab248
chore: build binary
jordanpowell88 Jun 26, 2023
fe130b7
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 6, 2023
3b2bb4b
Update npm/webpack-dev-server/src/helpers/angularHandler.ts
jordanpowell88 Jul 6, 2023
b7c85c6
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 6, 2023
491a0d2
fix(webpack-dev-server): use tmp but add typeRoots
jordanpowell88 Jul 7, 2023
20112de
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 7, 2023
9810e56
chore: update workflow to build binary
jordanpowell88 Jul 7, 2023
15aca64
Merge branch 'jordanpowell88/angular-tsconfig' of https://github.com/…
jordanpowell88 Jul 7, 2023
202ccc2
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 7, 2023
eb9a225
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 17, 2023
c991d53
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 17, 2023
aec1dab
Merge branch 'develop' into jordanpowell88/angular-tsconfig
jordanpowell88 Jul 17, 2023
67b86c8
remove redundant node_modules/types
jordanpowell88 Jul 17, 2023
1434534
remove node_modules/types from assertions
jordanpowell88 Jul 17, 2023
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
26 changes: 10 additions & 16 deletions npm/webpack-dev-server/src/helpers/angularHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs-extra'
import { tmpdir } from 'os'
import * as path from 'path'
import { gte } from 'semver'
import type { Configuration, RuleSetRule } from 'webpack'
Expand Down Expand Up @@ -136,35 +135,25 @@ export async function generateTsConfig (devServerConfig: AngularWebpackDevServer
includePaths.push(...polyfills.map((p: string) => getProjectFilePath(workspaceRoot, p)))
}

const cypressTypes = getProjectFilePath(workspaceRoot, 'node_modules', 'cypress', 'types', 'index.d.ts')

includePaths.push(cypressTypes)

const tsConfigContent = JSON.stringify({
extends: getProjectFilePath(projectRoot, buildOptions.tsConfig ?? 'tsconfig.json'),
compilerOptions: {
outDir: getProjectFilePath(projectRoot, 'out-tsc/cy'),
allowSyntheticDefaultImports: true,
skipLibCheck: true,
types: ['cypress'],
},
include: includePaths,
})
}, null, 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this was just to get pretty formatting? Still needed since this is dumping to a temp dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, but makes it "pretty"er 🤣


const tsConfigPath = path.join(await getTempDir(), 'tsconfig.json')
const tsConfigPath = path.join(projectRoot, 'tsconfig.cypress.json')

// write the tsconfig.cypress.json file to the project root
await fs.writeFile(tsConfigPath, tsConfigContent)

return tsConfigPath
}

export async function getTempDir (): Promise<string> {
const cypressTempDir = path.join(tmpdir(), 'cypress-angular-ct')

await fs.ensureDir(cypressTempDir)

return cypressTempDir
}

export async function getAngularCliModules (projectRoot: string) {
let angularVersion: string

Expand Down Expand Up @@ -272,7 +261,12 @@ async function getAngularCliWebpackConfig (devServerConfig: AngularWebpackDevSer
// normalize
const projectConfig = devServerConfig.options?.projectConfig || await getProjectConfig(projectRoot)

const tsConfig = await generateTsConfig(devServerConfig, projectConfig.buildOptions)
const cypressTsConfigPath = path.join(projectRoot, 'tsconfig.cypress.json')

// check if tsconfig.cypress.json
const tsConfig = fs.existsSync(cypressTsConfigPath)
? cypressTsConfigPath
: await generateTsConfig(devServerConfig, projectConfig.buildOptions)

const buildOptions = getAngularBuildOptions(projectConfig.buildOptions, tsConfig)

Expand Down
8 changes: 3 additions & 5 deletions npm/webpack-dev-server/test/handlers/angularHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getAngularCliModules,
getAngularJson,
getProjectConfig,
getTempDir,
toPosix,
} from '../../src/helpers/angularHandler'
import '../support'
Expand Down Expand Up @@ -215,9 +214,8 @@ const expectLoadsAngularBuildOptions = (buildOptions: BuildOptions) => {
const expectGeneratesTsConfig = async (devServerConfig: AngularWebpackDevServerConfig, buildOptions: any) => {
const { projectRoot } = devServerConfig.cypressConfig
let tsConfigPath = await generateTsConfig(devServerConfig, buildOptions)
const tempDir = await getTempDir()

expect(tsConfigPath).to.eq(path.join(tempDir, 'tsconfig.json'))
expect(tsConfigPath).to.eq(path.join(projectRoot, 'tsconfig.cypress.json'))

let tsConfig = JSON.parse(await fs.readFile(tsConfigPath, 'utf8'))

Expand All @@ -228,11 +226,11 @@ const expectGeneratesTsConfig = async (devServerConfig: AngularWebpackDevServerC
outDir: toPosix(path.join(projectRoot, 'out-tsc/cy')),
allowSyntheticDefaultImports: true,
skipLibCheck: true,
types: ['cypress'],
},
include: [
toPosix(path.join(projectRoot, 'src/**/*.cy.ts')),
toPosix(path.join(projectRoot, 'src/polyfills.ts')),
toPosix(path.join(projectRoot, 'node_modules/cypress/types/index.d.ts')),
],
})

Expand All @@ -256,11 +254,11 @@ const expectGeneratesTsConfig = async (devServerConfig: AngularWebpackDevServerC
outDir: toPosix(path.join(projectRoot, 'out-tsc/cy')),
allowSyntheticDefaultImports: true,
skipLibCheck: true,
types: ['cypress'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to confirm, this is the fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually that is not the fix. The issue is because we are using a temp file for the tsconfig any types that get added to the extended tsconfig types array aren't able to resolve. Currently we manually add the Cypress types to the includes array. But with this option we don't need to do this and we just just cypress to that types array.

We could potentially resolve those paths sourcing typeRoots from the extended tsconfig but it still doesn't allow users to make any additional changes to the tsconfig. Having a tsconfig.cypress.json follows the same ergonomics of a typical Angular project tsconfig.app.json, tsconfig.spec.json, etc

},
include: [
toPosix(path.join(projectRoot, 'src/**/*.cy.ts')),
toPosix(supportFile),
toPosix(path.join(projectRoot, 'node_modules/cypress/types/index.d.ts')),
],
})
}