From 77ce6be30c9cb8054ebf69a4943b984eed90152e Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:15:27 -0300 Subject: [PATCH] Add template tsconfigs for users to extend from (#4439) * Add tsconfig templates to extend from * Add changeset * Right order for assign parameters * Add tsconfigs to export map --- .changeset/healthy-kangaroos-smoke.md | 6 +++ examples/basics/tsconfig.json | 14 +----- examples/blog/tsconfig.json | 14 +----- examples/docs/tsconfig.json | 14 +----- examples/env-vars/tsconfig.json | 14 +----- examples/framework-alpine/tsconfig.json | 14 +----- examples/framework-lit/tsconfig.json | 14 +----- examples/framework-multiple/tsconfig.json | 14 +----- examples/framework-preact/tsconfig.json | 14 +----- examples/framework-react/tsconfig.json | 14 +----- examples/framework-solid/tsconfig.json | 12 +----- examples/framework-svelte/tsconfig.json | 14 +----- examples/framework-vue/tsconfig.json | 14 +----- examples/minimal/tsconfig.json | 14 +----- examples/non-html-pages/tsconfig.json | 14 +----- examples/portfolio/tsconfig.json | 14 +----- examples/ssr/tsconfig.json | 14 +----- examples/subpath/tsconfig.json | 14 +----- examples/with-markdown-plugins/tsconfig.json | 14 +----- examples/with-markdown-shiki/tsconfig.json | 14 +----- examples/with-mdx/tsconfig.json | 14 +----- examples/with-nanostores/tsconfig.json | 14 +----- examples/with-tailwindcss/tsconfig.json | 14 +----- examples/with-vite-plugin-pwa/tsconfig.json | 14 +----- examples/with-vitest/astro.config.ts | 2 +- examples/with-vitest/tsconfig.json | 17 +------- packages/astro/package.json | 3 ++ .../tsconfigs/base.json} | 11 ++--- packages/astro/tsconfigs/strict.json | 9 ++++ .../tsconfigs/strictest.json} | 23 +++------- packages/create-astro/package.json | 1 + packages/create-astro/src/index.ts | 43 ++++++++++++++----- pnpm-lock.yaml | 27 ++++++++++++ 33 files changed, 119 insertions(+), 343 deletions(-) create mode 100644 .changeset/healthy-kangaroos-smoke.md rename packages/{create-astro/tsconfigs/tsconfig.strict.json => astro/tsconfigs/base.json} (54%) create mode 100644 packages/astro/tsconfigs/strict.json rename packages/{create-astro/tsconfigs/tsconfig.stricter.json => astro/tsconfigs/strictest.json} (57%) diff --git a/.changeset/healthy-kangaroos-smoke.md b/.changeset/healthy-kangaroos-smoke.md new file mode 100644 index 000000000..cc5592d6f --- /dev/null +++ b/.changeset/healthy-kangaroos-smoke.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'create-astro': patch +--- + +Add tsconfig templates for users to extend from diff --git a/examples/basics/tsconfig.json b/examples/basics/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/basics/tsconfig.json +++ b/examples/basics/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/blog/tsconfig.json b/examples/blog/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/blog/tsconfig.json +++ b/examples/blog/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/docs/tsconfig.json b/examples/docs/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/docs/tsconfig.json +++ b/examples/docs/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/env-vars/tsconfig.json b/examples/env-vars/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/env-vars/tsconfig.json +++ b/examples/env-vars/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/framework-alpine/tsconfig.json b/examples/framework-alpine/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/framework-alpine/tsconfig.json +++ b/examples/framework-alpine/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/framework-lit/tsconfig.json b/examples/framework-lit/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/framework-lit/tsconfig.json +++ b/examples/framework-lit/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/framework-multiple/tsconfig.json b/examples/framework-multiple/tsconfig.json index 8885491e4..2d48ed5fd 100644 --- a/examples/framework-multiple/tsconfig.json +++ b/examples/framework-multiple/tsconfig.json @@ -1,17 +1,7 @@ { + "extends": "astro/tsconfigs/base", "compilerOptions": { - // Enable top-level await, and other modern ESM features. - "target": "ESNext", - "module": "ESNext", // Needed for TypeScript intellisense in the template inside Vue files - "jsx": "preserve", - // 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 + "jsx": "preserve" } } diff --git a/examples/framework-preact/tsconfig.json b/examples/framework-preact/tsconfig.json index 766e72684..bdd1b5a88 100644 --- a/examples/framework-preact/tsconfig.json +++ b/examples/framework-preact/tsconfig.json @@ -1,18 +1,8 @@ { + "extends": "astro/tsconfigs/base", "compilerOptions": { // Preact specific settings "jsx": "react-jsx", - "jsxImportSource": "preact", - // 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 + "jsxImportSource": "preact" } } diff --git a/examples/framework-react/tsconfig.json b/examples/framework-react/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/framework-react/tsconfig.json +++ b/examples/framework-react/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/framework-solid/tsconfig.json b/examples/framework-solid/tsconfig.json index 807b3fac7..cb2c53c66 100644 --- a/examples/framework-solid/tsconfig.json +++ b/examples/framework-solid/tsconfig.json @@ -1,18 +1,8 @@ { + "extends": "astro/tsconfigs/base", "compilerOptions": { // Solid specific settings "jsx": "preserve", "jsxImportSource": "solid-js", - // 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 } } diff --git a/examples/framework-svelte/tsconfig.json b/examples/framework-svelte/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/framework-svelte/tsconfig.json +++ b/examples/framework-svelte/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/framework-vue/tsconfig.json b/examples/framework-vue/tsconfig.json index 8885491e4..2d48ed5fd 100644 --- a/examples/framework-vue/tsconfig.json +++ b/examples/framework-vue/tsconfig.json @@ -1,17 +1,7 @@ { + "extends": "astro/tsconfigs/base", "compilerOptions": { - // Enable top-level await, and other modern ESM features. - "target": "ESNext", - "module": "ESNext", // Needed for TypeScript intellisense in the template inside Vue files - "jsx": "preserve", - // 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 + "jsx": "preserve" } } diff --git a/examples/minimal/tsconfig.json b/examples/minimal/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/minimal/tsconfig.json +++ b/examples/minimal/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/non-html-pages/tsconfig.json b/examples/non-html-pages/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/non-html-pages/tsconfig.json +++ b/examples/non-html-pages/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/portfolio/tsconfig.json b/examples/portfolio/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/portfolio/tsconfig.json +++ b/examples/portfolio/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/ssr/tsconfig.json b/examples/ssr/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/ssr/tsconfig.json +++ b/examples/ssr/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/subpath/tsconfig.json b/examples/subpath/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/subpath/tsconfig.json +++ b/examples/subpath/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-markdown-plugins/tsconfig.json b/examples/with-markdown-plugins/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-markdown-plugins/tsconfig.json +++ b/examples/with-markdown-plugins/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-markdown-shiki/tsconfig.json b/examples/with-markdown-shiki/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-markdown-shiki/tsconfig.json +++ b/examples/with-markdown-shiki/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-mdx/tsconfig.json b/examples/with-mdx/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-mdx/tsconfig.json +++ b/examples/with-mdx/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-nanostores/tsconfig.json b/examples/with-nanostores/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-nanostores/tsconfig.json +++ b/examples/with-nanostores/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-tailwindcss/tsconfig.json b/examples/with-tailwindcss/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-tailwindcss/tsconfig.json +++ b/examples/with-tailwindcss/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-vite-plugin-pwa/tsconfig.json b/examples/with-vite-plugin-pwa/tsconfig.json index e9e9245fd..d78f81ec4 100644 --- a/examples/with-vite-plugin-pwa/tsconfig.json +++ b/examples/with-vite-plugin-pwa/tsconfig.json @@ -1,15 +1,3 @@ { - "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 - } + "extends": "astro/tsconfigs/base" } diff --git a/examples/with-vitest/astro.config.ts b/examples/with-vitest/astro.config.ts index ad7965b1a..882e6515a 100644 --- a/examples/with-vitest/astro.config.ts +++ b/examples/with-vitest/astro.config.ts @@ -1,4 +1,4 @@ import { defineConfig } from 'astro/config'; // https://astro.build/config -export default defineConfig(); +export default defineConfig({}); diff --git a/examples/with-vitest/tsconfig.json b/examples/with-vitest/tsconfig.json index be8e3ea96..d78f81ec4 100644 --- a/examples/with-vitest/tsconfig.json +++ b/examples/with-vitest/tsconfig.json @@ -1,18 +1,3 @@ { - "compilerOptions": { - // Preact specific settings - "jsx": "react-jsx", - "jsxImportSource": "preact", - // 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, - // Add type definitions for our Astro runtime. - "types": ["astro/client"] - } + "extends": "astro/tsconfigs/base" } diff --git a/packages/astro/package.json b/packages/astro/package.json index d674ef69e..2479a28ff 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -32,6 +32,8 @@ "./client": "./client.d.ts", "./client-base": "./client-base.d.ts", "./astro-jsx": "./astro-jsx.d.ts", + "./tsconfigs/*.json": "./tsconfigs/*", + "./tsconfigs/*": "./tsconfigs/*.json", "./jsx/*": "./dist/jsx/*", "./jsx-runtime": "./dist/jsx-runtime/index.js", "./config": "./config.mjs", @@ -65,6 +67,7 @@ }, "files": [ "components", + "tsconfigs", "dist", "astro.js", "config.d.ts", diff --git a/packages/create-astro/tsconfigs/tsconfig.strict.json b/packages/astro/tsconfigs/base.json similarity index 54% rename from packages/create-astro/tsconfigs/tsconfig.strict.json rename to 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/astro/tsconfigs/strict.json b/packages/astro/tsconfigs/strict.json new file mode 100644 index 000000000..bc87a68e0 --- /dev/null +++ b/packages/astro/tsconfigs/strict.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "./base.json", + "compilerOptions": { + "strict": true, + // Error when a value import is only used as a type. + "importsNotUsedAsValues": "error" + } +} diff --git a/packages/create-astro/tsconfigs/tsconfig.stricter.json b/packages/astro/tsconfigs/strictest.json similarity index 57% rename from packages/create-astro/tsconfigs/tsconfig.stricter.json rename to 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!'); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8daa0eeec..300361cb5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2108,6 +2108,7 @@ importers: astro-scripts: workspace:* chai: ^4.3.6 chalk: ^5.0.1 + comment-json: ^4.2.3 degit: ^2.8.4 execa: ^6.1.0 kleur: ^4.1.4 @@ -2119,6 +2120,7 @@ importers: yargs-parser: ^21.0.1 dependencies: chalk: 5.0.1 + comment-json: 4.2.3 degit: 2.8.4 execa: 6.1.0 kleur: 4.1.5 @@ -9479,6 +9481,10 @@ packages: resolution: {integrity: sha512-sNRaPGh9nnmdC8Zf+pT3UqP8rnWj5Hf9wiFGsX3wUQ2yVSIhO2ShFwCoceIPpB41QF6i2OEmrHmCo36xronCVA==} dev: false + /array-timsort/1.0.3: + resolution: {integrity: sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==} + dev: false + /array-union/2.1.0: resolution: {integrity: sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==} engines: {node: '>=8'} @@ -10050,6 +10056,17 @@ packages: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} dev: true + /comment-json/4.2.3: + resolution: {integrity: sha512-SsxdiOf064DWoZLH799Ata6u7iV658A11PlWtZATDlXPpKGJnbJZ5Z24ybixAi+LUUqJ/GKowAejtC5GFUG7Tw==} + engines: {node: '>= 6'} + dependencies: + array-timsort: 1.0.3 + core-util-is: 1.0.3 + esprima: 4.0.1 + has-own-prop: 2.0.0 + repeat-string: 1.6.1 + dev: false + /common-ancestor-path/1.0.1: resolution: {integrity: sha512-L3sHRo1pXXEqX8VU28kfgUY+YGsk09hPqZiZmLacNib6XNTCM8ubYeT7ryXQw8asB1sKgcU5lkB7ONug08aB8w==} dev: false @@ -11784,6 +11801,11 @@ packages: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} + /has-own-prop/2.0.0: + resolution: {integrity: sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==} + engines: {node: '>=8'} + dev: false + /has-package-exports/1.3.0: resolution: {integrity: sha512-e9OeXPQnmPhYoJ63lXC4wWe34TxEGZDZ3OQX9XRqp2VwsfLl3bQBy7VehLnd34g3ef8CmYlBLGqEMKXuz8YazQ==} dependencies: @@ -15141,6 +15163,11 @@ packages: unified: 10.1.2 dev: true + /repeat-string/1.6.1: + resolution: {integrity: sha512-PV0dzCYDNfRi1jCDbJzpW7jNNDRuCOG/jI5ctQcGKt/clZD+YcPS3yIlWuTJMmESC8aevCFmWJy5wjAFgNqN6w==} + engines: {node: '>=0.10'} + dev: false + /require-directory/2.1.1: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'}