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

create-astro combine strict tsconfigs #4301

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/strange-eggs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Don't override the default tsconfig.json anymore when enabling strict/stricter typescript mode. The config will just be amended for a better experience with single framework jsx templates (solid/preact)
1 change: 1 addition & 0 deletions packages/create-astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"dependencies": {
"chalk": "^5.0.1",
"comment-json": "^4.2.2",
"degit": "^2.8.4",
"execa": "^6.1.0",
"kleur": "^4.1.4",
Expand Down
51 changes: 44 additions & 7 deletions packages/create-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import prompts from 'prompts';
import url from 'url';
import detectPackageManager from 'which-pm-runs';
import yargs from 'yargs-parser';
import { parse, stringify, assign, CommentArray, CommentJSONValue } from 'comment-json';
import { loadWithRocketGradient, rocketAscii } from './gradient.js';
import { defaultLogLevel, logger } from './logger.js';
import { TEMPLATES } from './templates.js';
Expand Down Expand Up @@ -344,14 +345,50 @@ export async function main() {
ora().info(dim(`--dry-run enabled, skipping.`));
} else if (tsResponse.typescript) {
if (tsResponse.typescript !== 'default') {
fs.copyFileSync(
path.join(
url.fileURLToPath(new URL('..', import.meta.url)),
'tsconfigs',
`tsconfig.${tsResponse.typescript}.json`
),
path.join(cwd, 'tsconfig.json')
const strictTsconfig = parse(
fs
.readFileSync(
path.join(
url.fileURLToPath(new URL('..', import.meta.url)),
'tsconfigs',
`tsconfig.${tsResponse.typescript}.json`
)
)
.toString()
);
const templateTsconfig = parse(fs.readFileSync(path.join(cwd, 'tsconfig.json')).toString());
if (
!(
templateTsconfig &&
typeof templateTsconfig === 'object' &&
'compilerOptions' in templateTsconfig
)
) {
// template tsConfig is malformed or has no compilerOptions -> use strictTsConfig
fs.writeFileSync(path.join(cwd, 'tsconfig.json'), stringify(strictTsconfig, null, 2));
} else if (
!(
strictTsconfig &&
typeof strictTsconfig === 'object' &&
'compilerOptions' in strictTsconfig
)
) {
// this case should never happen
console.log(
yellow(
`something went wrong while enableing ${tsResponse.typescript} syntax! enabling Relaxed for now...`
)
);
} else {
const combinedCompilerOptions = assign(
templateTsconfig.compilerOptions,
strictTsconfig.compilerOptions // override the duplicate template options with the strict options
);
const combinedTsconfig = assign(templateTsconfig, {
compilerOptions: combinedCompilerOptions,
}); // leave the rest of template specific options as is only override the compiler options (tsconfig.strict.json has no other fields)
fs.writeFileSync(path.join(cwd, 'tsconfig.json'), stringify(combinedTsconfig, null, 2));
}
}
ora().succeed('TypeScript settings applied!');
}
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.