From bfd1b3f95c31483eb703d98cf24ba7c17abbccd6 Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:31:08 +0900 Subject: [PATCH] feat: support for deno.jsonc in vite plugin (#253) * fix: support for deno.jsonc in vite plugin * tweak --- src/vite/island-components.test.ts | 29 ++++++++++++++++++++++++++++- src/vite/island-components.ts | 20 ++++++++++---------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/vite/island-components.test.ts b/src/vite/island-components.test.ts index f442637..2a22aa2 100644 --- a/src/vite/island-components.test.ts +++ b/src/vite/island-components.test.ts @@ -235,7 +235,7 @@ describe('options', () => { test.each([ { - name: 'default with both config files (deno.json preferred)', + name: 'default with both config files (prefers deno.json over tsconfig.json)', configFiles: { 'deno.json': { jsxImportSource: 'react' }, 'tsconfig.json': { jsxImportSource: 'hono/jsx' }, @@ -243,6 +243,24 @@ describe('options', () => { config: {}, expect: { hono: false, react: true }, }, + { + name: 'default with both config files (prefers deno.json over deno.jsonc)', + configFiles: { + 'deno.json': { jsxImportSource: 'react' }, + 'deno.jsonc': { jsxImportSource: 'hono/jsx' }, + }, + config: {}, + expect: { hono: false, react: true }, + }, + { + name: 'default with both config files (prefers deno.jsonc over tsconfig.json)', + configFiles: { + 'deno.jsonc': { jsxImportSource: 'react' }, + 'tsconfig.json': { jsxImportSource: 'hono/jsx' }, + }, + config: {}, + expect: { hono: false, react: true }, + }, { name: 'default with only deno.json', configFiles: { @@ -251,6 +269,14 @@ describe('options', () => { config: {}, expect: { hono: false, react: true }, }, + { + name: 'default with only deno.jsonc', + configFiles: { + 'deno.jsonc': { jsxImportSource: 'react' }, + }, + config: {}, + expect: { hono: false, react: true }, + }, { name: 'default with only tsconfig.json', configFiles: { @@ -263,6 +289,7 @@ describe('options', () => { name: 'explicit react config overrides all', configFiles: { 'deno.json': { jsxImportSource: 'hono/jsx' }, + 'deno.jsonc': { jsxImportSource: 'hono/jsx' }, 'tsconfig.json': { jsxImportSource: 'hono/jsx' }, }, config: { reactApiImportSource: 'react' }, diff --git a/src/vite/island-components.ts b/src/vite/island-components.ts index 666aa92..79405c7 100644 --- a/src/vite/island-components.ts +++ b/src/vite/island-components.ts @@ -217,18 +217,18 @@ export function islandComponents(options?: IslandComponentsOptions): Plugin { root = config.root if (!reactApiImportSource) { - const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json') - const denoJsonPath = path.resolve(process.cwd(), 'deno.json') - let tsConfigRaw: string - try { - tsConfigRaw = await fs.readFile(denoJsonPath, 'utf8') - } catch (error1) { + const tsConfigFiles = ['deno.json', 'deno.jsonc', 'tsconfig.json'] + let tsConfigRaw: string | undefined + for (const tsConfigFile of tsConfigFiles) { try { + const tsConfigPath = path.resolve(process.cwd(), tsConfigFile) tsConfigRaw = await fs.readFile(tsConfigPath, 'utf8') - } catch (error2) { - console.warn('Cannot find neither tsconfig.json nor deno.json', error1, error2) - return - } + break + } catch {} + } + if (!tsConfigRaw) { + console.warn('Cannot find tsconfig.json or deno.json(c)') + return } const tsConfig = parseJsonc(tsConfigRaw)