Skip to content

Commit 6b89aed

Browse files
committed
Fix importing runtime without vite
1 parent 8d3c7a3 commit 6b89aed

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

scripts/check-setup-needed.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import path from 'path'
88
import dotenv from 'dotenv'
99
import { execSync } from 'child_process'
1010
import { L10N_CAGE_DIR, MARKDOWN_L10NS } from '../src/lib/l10n.ts'
11+
import { importRuntimeWithoutVite } from './l10n/utils.ts'
1112

1213
dotenv.config()
1314

14-
const runtimeModule = await import('../src/lib/paraglide/runtime.js')
15+
const runtimeModule = await importRuntimeWithoutVite()
1516
let activeLocales = Array.from(runtimeModule.locales)
1617
let setupNeeded = false
1718
let reason = ''

scripts/l10n/run.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
MESSAGE_L10NS,
3636
MESSAGE_SOURCE
3737
} from '../../src/lib/l10n.ts'
38+
import { importRuntimeWithoutVite } from './utils.ts'
3839

3940
// Load environment variables first
4041
dotenv.config()
@@ -63,12 +64,12 @@ if (unknownArgs.length > 0) {
6364
}
6465

6566
// Ensure inlang settings are current before importing runtime
66-
execSync('tsx scripts/inlang-settings.ts', { stdio: 'ignore' })
67+
execSync('tsx scripts/inlang-settings.ts', { stdio: 'inherit' })
6768

6869
// This let / try / catch lets the ESM scan succeed in the absence of a runtime
6970
let locales: readonly string[]
7071
try {
71-
const runtime = await import('../../src/lib/paraglide/runtime.js')
72+
const runtime = await importRuntimeWithoutVite()
7273
locales = runtime.locales
7374
if (runtime.baseLocale !== 'en')
7475
throw new Error(

scripts/l10n/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,14 @@ export function cullCommentary(filePath: string, verbose = false) {
262262
console.error(`Error cleaning up file ${filePath}:`, error.message)
263263
}
264264
}
265+
266+
export async function importRuntimeWithoutVite(): Promise<
267+
typeof import('../../src/lib/paraglide/runtime.js')
268+
> {
269+
const runtimeString = await fs.readFile('src/lib/paraglide/runtime.js', 'utf-8')
270+
const patchedRuntime = runtimeString.replace('import.meta.env.SSR', 'true')
271+
const runtime = await import(
272+
'data:text/javascript;base64,' + Buffer.from(patchedRuntime).toString('base64')
273+
)
274+
return runtime
275+
}

scripts/l10ntamer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
import fs from 'fs'
1313
import path from 'path'
14-
import { locales, localizeHref } from '../src/lib/paraglide/runtime.js'
14+
import { importRuntimeWithoutVite } from './l10n/utils.js'
15+
16+
const { locales, localizeHref } = await importRuntimeWithoutVite()
1517

1618
if (localizeHref('/test', { locale: 'en' }) === '/test') {
1719
console.log('⏭️ Skipping l10ntamer - English routes not prefixed')

svelte.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import remarkToc from 'remark-toc'
99
import remarkHeadingId from 'remark-heading-id'
1010
import rehypeSlug from 'rehype-slug'
1111

12-
import { locales } from './src/lib/paraglide/runtime.js'
12+
import { locales } from './project.inlang/settings.json' assert { type: 'json' }
1313

1414
// Export configuration flags for use in build scripts
1515
export const USE_EDGE_FUNCTIONS = true

vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import path from 'path'
66
import { defineConfig } from 'vite'
77
import { isDev } from './src/lib/env'
88
import { MARKDOWN_L10NS } from './src/lib/l10n'
9-
import { locales as compiledLocales } from './src/lib/paraglide/runtime'
9+
import { importRuntimeWithoutVite } from './scripts/l10n/utils'
10+
11+
const { locales: compiledLocales } = await importRuntimeWithoutVite()
1012

1113
function getLocaleExcludePatterns(): RegExp[] {
1214
const md = path.resolve(MARKDOWN_L10NS)

0 commit comments

Comments
 (0)