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

feat: expanded rootDir tsconfig support, --typescript to use src/ directory by default; expanded docs #27667

Closed
wants to merge 9 commits into from

Conversation

ctjlewis
Copy link
Contributor

@ctjlewis ctjlewis commented Aug 2, 2021

Feature

Changes

Myself and other users ran into trouble with next build type checking unwanted files (outside of rootDir in my case, more specific like excluding TS files in public/ for others).

rootDir and include tsconfig fields prevent unnecessary type checking

Originally, because next build would throw during type checking for files not included in rootDir, I thought it did not respect the rootDir option - but having reviewed/refactored the code, it clear it actually does, and furthermore does not distinguish between any of the tsconfig fields outside of the few fields it overrides (docs were modified to clarify this in multiple locations).

However, if rootDir is configured without adjusting the include patterns from **/*.ts to src/**/*.ts etc., TypeScript will prioritize the includes field and attempt to check the matched files anyway. This was noted in multiple points, especially in the src/ Directory docs.

This PR also adds docs and a test for this situation, demonstrating how to use the rootDir and includes tsconfig fields to ensure that next build ignores type errors in unchecked files. For the use case along the lines of "I have files in public/ I want to ignore," documentation now clarifies in multiple places that next build will honor tsconfig's exclude field (as well as most others).

src/ configuration by default for create-next-app --typescript

Finally, the TypeScript template is updated to use src/ directory configuration by default to minimize the chances of unnecessary type checking. This will likely be seen as an opinionated change, but adding the --typescript flag was itself an opinionated change which I added in #24655, and I think this is the right design choice.

Footnote

My first step was to work through the runTypeCheck.ts logic, which I refactored and commented. This did not end up being necessary, and these changes can be discarded without affecting any of the main goals of this PR, but given that it makes the code easier to reason about, I would recommend leaving the changes in.

@ijjk ijjk added create-next-app Related to our CLI tool for quickly starting a new Next.js application. type: documentation examples Issue/PR related to examples type: next labels Aug 2, 2021
@ctjlewis
Copy link
Contributor Author

ctjlewis commented Aug 2, 2021

Sorry for the early submit - was actually just trying to submit a PR to my own fork and test the CI there before opening an actual PR.

EDIT: CI seems to be temporarily broken for one test, tried to work around but can't figure it out.

EDIT 2: CI does not seem to be in good shape (Failed to fetch timings data Error: request status: 524).

@ctjlewis ctjlewis changed the title Type check update feat: expanded rootDir tsconfig support, --typescript to use src/ directory by default; expanded docs Aug 2, 2021
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ctjlewis ctjlewis force-pushed the type-check-update branch 2 times, most recently from 10b48e4 to c2c35b6 Compare August 2, 2021 04:00
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ctjlewis
Copy link
Contributor Author

ctjlewis commented Aug 2, 2021

build CI step keeps throwing Failed to fetch timings data Error: request status: 524, seems nondeterministic, wasn't happening at first.

Gonna shelve this for the night, no idea how to proceed with busted CI.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ctjlewis ctjlewis marked this pull request as draft August 2, 2021 07:19
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Aug 2, 2021

Failing test suites

Commit: fc321d9

test/unit/webpack-config-overrides.unit.test.js

Expand output

● Test suite failed to run

/home/runner/work/next.js/next.js/packages/next/dist/build/webpack/config/blocks/css/loaders/global.js: No element indexed by 1

  at ArraySet_at [as at] (../node_modules/jest-config/node_modules/source-map/lib/array-set.js:109:9)
      at Array.map (<anonymous>)
      at run.next (<anonymous>)

@ijjk
Copy link
Member

ijjk commented Aug 2, 2021

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary ctjlewis/next.js type-check-update Change
buildDuration 16.3s 15s -1.4s
buildDurationCached 3.5s 3.5s -63ms
nodeModulesSize 50.1 MB 50.1 MB ⚠️ +4.79 kB
Page Load Tests Overall increase ✓
vercel/next.js canary ctjlewis/next.js type-check-update Change
/ failed reqs 0 0
/ total time (seconds) 2.648 2.51 -0.14
/ avg req/sec 943.94 995.92 +51.98
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.513 1.283 -0.23
/error-in-render avg req/sec 1652.75 1948.08 +295.33
Client Bundles (main, webpack, commons)
vercel/next.js canary ctjlewis/next.js type-check-update Change
745.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.2 kB 42.2 kB
main-HASH.js gzip 22.8 kB 22.8 kB
webpack-HASH.js gzip 1.5 kB 1.5 kB
Overall change 66.7 kB 66.7 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary ctjlewis/next.js type-check-update Change
polyfills-HASH.js gzip 31.1 kB 31.1 kB
Overall change 31.1 kB 31.1 kB
Client Pages
vercel/next.js canary ctjlewis/next.js type-check-update Change
_app-HASH.js gzip 980 B 980 B
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 329 B 329 B
dynamic-HASH.js gzip 2.52 kB 2.52 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 903 B 903 B
image-HASH.js gzip 4.1 kB 4.1 kB
index-HASH.js gzip 261 B 261 B
link-HASH.js gzip 1.66 kB 1.66 kB
routerDirect..HASH.js gzip 319 B 319 B
script-HASH.js gzip 387 B 387 B
withRouter-HASH.js gzip 320 B 320 B
bb14e60e810b..30f.css gzip 125 B 125 B
Overall change 12.8 kB 12.8 kB
Client Build Manifests
vercel/next.js canary ctjlewis/next.js type-check-update Change
_buildManifest.js gzip 490 B 490 B
Overall change 490 B 490 B
Rendered Page Sizes
vercel/next.js canary ctjlewis/next.js type-check-update Change
index.html gzip 530 B 530 B
link.html gzip 542 B 542 B
withRouter.html gzip 524 B 524 B
Overall change 1.6 kB 1.6 kB

Webpack 4 Mode (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary ctjlewis/next.js type-check-update Change
buildDuration 12.4s 11.7s -683ms
buildDurationCached 4.6s 4.7s ⚠️ +88ms
nodeModulesSize 50.1 MB 50.1 MB ⚠️ +4.79 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary ctjlewis/next.js type-check-update Change
/ failed reqs 0 0
/ total time (seconds) 2.53 2.53
/ avg req/sec 988.24 988.19 ⚠️ -0.05
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.372 1.382 ⚠️ +0.01
/error-in-render avg req/sec 1822.36 1808.49 ⚠️ -13.87
Client Bundles (main, webpack, commons)
vercel/next.js canary ctjlewis/next.js type-check-update Change
17.HASH.js gzip 185 B 185 B
677f882d2ed8..HASH.js gzip 13.8 kB 13.8 kB
framework.HASH.js gzip 41.9 kB 41.9 kB
main-HASH.js gzip 10.6 kB 10.6 kB
webpack-HASH.js gzip 1.19 kB 1.19 kB
Overall change 67.7 kB 67.7 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary ctjlewis/next.js type-check-update Change
polyfills-HASH.js gzip 31.3 kB 31.3 kB
Overall change 31.3 kB 31.3 kB
Client Pages
vercel/next.js canary ctjlewis/next.js type-check-update Change
_app-HASH.js gzip 965 B 965 B
_error-HASH.js gzip 3.74 kB 3.74 kB
amp-HASH.js gzip 552 B 552 B
css-HASH.js gzip 333 B 333 B
dynamic-HASH.js gzip 2.71 kB 2.71 kB
head-HASH.js gzip 2.97 kB 2.97 kB
hooks-HASH.js gzip 911 B 911 B
index-HASH.js gzip 231 B 231 B
link-HASH.js gzip 1.64 kB 1.64 kB
routerDirect..HASH.js gzip 298 B 298 B
script-HASH.js gzip 2.94 kB 2.94 kB
withRouter-HASH.js gzip 294 B 294 B
e025d2764813..52f.css gzip 125 B 125 B
Overall change 17.7 kB 17.7 kB
Client Build Manifests
vercel/next.js canary ctjlewis/next.js type-check-update Change
_buildManifest.js gzip 499 B 499 B
Overall change 499 B 499 B
Rendered Page Sizes
vercel/next.js canary ctjlewis/next.js type-check-update Change
index.html gzip 578 B 578 B
link.html gzip 591 B 591 B
withRouter.html gzip 571 B 571 B
Overall change 1.74 kB 1.74 kB
Commit: fc321d9

@ctjlewis
Copy link
Contributor Author

ctjlewis commented Aug 9, 2021

The status of this draft is basically that it's purely experimental, including the TS "src/ by default" principle (which was added as a way to guarantee there would never be redundant type-checking).

I think that change should be retained if the idea of refactoring all templates to use a src/ directory by default is reviewed favorably, which it probably would not be, but I would encourage considering this de novo. Otherwise, tsconfig could be configured to only include pages/** and types/** by default so as to prevent redundant type-checking out-of-the-box without relying on the rootDir field.

In short, thoughts on all of this are needed before a concrete PR can be sent in.

@ijjk ijjk force-pushed the canary branch 3 times, most recently from df8579c to 47e5ebe Compare October 25, 2022 16:15
@ijjk ijjk force-pushed the canary branch 2 times, most recently from e078ebe to 6b863fe Compare December 2, 2022 05:49
Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

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

Gonna close this as stale, we did partially migrate to src dir pattern though for the next package!

@ijjk ijjk closed this Jun 15, 2023
@ctjlewis
Copy link
Contributor Author

ctjlewis commented Jun 15, 2023 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
create-next-app Related to our CLI tool for quickly starting a new Next.js application. examples Issue/PR related to examples locked type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants