diff options
Diffstat (limited to '')
-rw-r--r-- | packages/astro/tsconfigs/base.json (renamed from packages/create-astro/tsconfigs/tsconfig.strict.json) | 11 | ||||
-rw-r--r-- | packages/astro/tsconfigs/strictest.json (renamed from packages/create-astro/tsconfigs/tsconfig.stricter.json) | 23 | ||||
-rw-r--r-- | packages/create-astro/package.json | 1 | ||||
-rw-r--r-- | packages/create-astro/src/index.ts | 43 |
4 files changed, 46 insertions, 32 deletions
diff --git a/packages/create-astro/tsconfigs/tsconfig.strict.json b/packages/astro/tsconfigs/base.json index a2ebcb5c4..389712b23 100644 --- a/packages/create-astro/tsconfigs/tsconfig.strict.json +++ b/packages/astro/tsconfigs/base.json @@ -1,4 +1,5 @@ { + "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { // Enable top-level await, and other modern ESM features. "target": "ESNext", @@ -9,11 +10,11 @@ "resolveJsonModule": true, // Enable stricter transpilation for better output. "isolatedModules": true, - // Astro will directly run your TypeScript code, no transpilation needed. + // Astro directly run TypeScript code, no transpilation needed. "noEmit": true, - // Enable strict type checking. - "strict": true, - // Error when a value import is only used as a type. - "importsNotUsedAsValues": "error" + // Report an error when importing a file using a casing different from the casing on disk. + "forceConsistentCasingInFileNames": true, + // Properly support importing CJS modules in ESM + "esModuleInterop": true } } diff --git a/packages/create-astro/tsconfigs/tsconfig.stricter.json b/packages/astro/tsconfigs/strictest.json index 2d046f7c0..f59dc4f6c 100644 --- a/packages/create-astro/tsconfigs/tsconfig.stricter.json +++ b/packages/astro/tsconfigs/strictest.json @@ -1,20 +1,7 @@ { + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "./strict.json", "compilerOptions": { - // Enable top-level await, and other modern ESM features. - "target": "ESNext", - "module": "ESNext", - // Enable node-style module resolution, for things like npm package imports. - "moduleResolution": "node", - // Enable JSON imports. - "resolveJsonModule": true, - // Enable stricter transpilation for better output. - "isolatedModules": true, - // Astro will directly run your TypeScript code, no transpilation needed. - "noEmit": true, - // Enable strict type checking. - "strict": true, - // Error when a value import is only used as a type. - "importsNotUsedAsValues": "error", // Report errors for fallthrough cases in switch statements "noFallthroughCasesInSwitch": true, // Force functions designed to override their parent class to be specified as `override`. @@ -28,6 +15,10 @@ // Force the usage of the indexed syntax to access fields declared using an index signature. "noUncheckedIndexedAccess": true, // Report an error when the value `undefined` is given to an optional property that doesn't specify `undefined` as a valid value. - "exactOptionalPropertyTypes": true + "exactOptionalPropertyTypes": true, + // Report an error for unreachable code instead of just a warning. + "allowUnreachableCode": false, + // Report an error for unused labels instead of just a warning. + "allowUnusedLabels": false } } diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index aee43f726..ba21ac1e8 100644 --- a/packages/create-astro/package.json +++ b/packages/create-astro/package.json @@ -30,6 +30,7 @@ ], "dependencies": { "chalk": "^5.0.1", + "comment-json": "^4.2.3", "degit": "^2.8.4", "execa": "^6.1.0", "kleur": "^4.1.4", diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index c3d86912f..3e7d4f4d2 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -1,4 +1,5 @@ /* eslint no-console: 'off' */ +import { assign, parse, stringify } from 'comment-json'; import degit from 'degit'; import { execa, execaCommand } from 'execa'; import fs from 'fs'; @@ -7,7 +8,6 @@ import ora from 'ora'; import os from 'os'; import path from 'path'; import prompts from 'prompts'; -import url from 'url'; import detectPackageManager from 'which-pm-runs'; import yargs from 'yargs-parser'; import { loadWithRocketGradient, rocketAscii } from './gradient.js'; @@ -117,6 +117,7 @@ export async function main() { const hash = args.commit ? `#${args.commit}` : ''; // Don't touch the template name if a GitHub repo was provided, ex: `--template cassidoo/shopify-react-astro` + const isThirdParty = options.template.includes('/'); const templateTarget = options.template.includes('/') ? options.template : `withastro/astro/examples/${options.template}#latest`; @@ -308,7 +309,7 @@ export async function main() { { title: 'Strictest', description: 'Enable all typechecking rules', - value: 'stricter', + value: 'strictest', }, { title: 'I prefer not to use TypeScript', @@ -335,7 +336,7 @@ export async function main() { console.log(` Astro supports TypeScript inside of ".astro" component scripts, so`); console.log(` we still need to create some TypeScript-related files in your project.`); console.log(` You can safely ignore these files, but don't delete them!`); - console.log(dim(' (ex: tsconfig.json, src/types.d.ts)')); + console.log(dim(' (ex: tsconfig.json, src/env.d.ts)')); console.log(``); tsResponse.typescript = 'default'; await wait(300); @@ -344,14 +345,34 @@ 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 templateTSConfigPath = path.join(cwd, 'tsconfig.json'); + fs.readFile(templateTSConfigPath, (err, data) => { + if (err && err.code === 'ENOENT') { + // If the template doesn't have a tsconfig.json, let's add one instead + fs.writeFileSync( + templateTSConfigPath, + stringify({ extends: `astro/tsconfigs/${tsResponse.typescript}` }, null, 2) + ); + + return; + } + + const templateTSConfig = parse(data.toString()); + + if (templateTSConfig && typeof templateTSConfig === 'object') { + const result = assign(templateTSConfig, { + extends: `astro/tsconfigs/${tsResponse.typescript}`, + }); + + fs.writeFileSync(templateTSConfigPath, stringify(result, null, 2)); + } else { + console.log( + yellow( + "There was an error applying the requested TypeScript settings. This could be because the template's tsconfig.json is malformed" + ) + ); + } + }); } ora().succeed('TypeScript settings applied!'); } |