Skip to content

Commit

Permalink
feat: support for deno.jsonc in vite plugin (#253)
Browse files Browse the repository at this point in the history
* fix: support for deno.jsonc in vite plugin

* tweak
  • Loading branch information
ryuapp authored Jan 16, 2025
1 parent ae5d22d commit bfd1b3f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
29 changes: 28 additions & 1 deletion src/vite/island-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,32 @@ 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' },
},
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: {
Expand All @@ -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: {
Expand All @@ -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' },
Expand Down
20 changes: 10 additions & 10 deletions src/vite/island-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit bfd1b3f

Please sign in to comment.