1
- // Expects to be run with tsx
2
1
// @ts -check
3
2
4
3
import ts from "typescript" ;
@@ -7,18 +6,24 @@ import { fileURLToPath } from "node:url";
7
6
import { Logger , Options , TSConfigReader } from "typedoc" ;
8
7
import { existsSync , mkdirSync , readdirSync , readFileSync , rmSync , writeFileSync } from "node:fs" ;
9
8
import { ok } from "node:assert" ;
9
+ import { createRequire } from "node:module" ;
10
10
11
11
const browserBundleFolders = [
12
12
"/utils-common/" ,
13
13
"/models/" ,
14
14
"/serialization/" ,
15
15
] ;
16
16
17
- const localesDir = join (
17
+ const localesDirTs = join (
18
18
fileURLToPath ( import . meta. url ) ,
19
19
"../../src/lib/internationalization/locales" ,
20
20
) ;
21
21
22
+ const localesDir = join (
23
+ fileURLToPath ( import . meta. url ) ,
24
+ "../../dist/lib/internationalization/locales" ,
25
+ ) ;
26
+
22
27
const distDir = join (
23
28
fileURLToPath ( import . meta. url ) ,
24
29
"../../dist/browser-locales" ,
@@ -56,12 +61,12 @@ const service = ts.createLanguageService(
56
61
const program = service . getProgram ( ) ;
57
62
ok ( program , "Failed to get program for i18n analysis" ) ;
58
63
59
- const sf = program . getSourceFile ( join ( localesDir , "en.cts" ) ) ;
64
+ const sf = program . getSourceFile ( join ( localesDirTs , "en.cts" ) ) ;
60
65
ok ( sf , "Failed to get source file" ) ;
61
66
62
67
const moduleSymbol = program . getTypeChecker ( ) . getSymbolAtLocation ( sf ) ;
63
68
const translatable = moduleSymbol ?. exports ?. get (
64
- "export=" as ts . __String ,
69
+ /** @type { ts.__String } */ ( "export=" ) ,
65
70
) ;
66
71
ok ( translatable , "Failed to get translatable symbol" ) ;
67
72
74
79
) ;
75
80
const translatableObj = translatable . valueDeclaration . expression . expression ;
76
81
77
- const bundleUsedTranslationKeys : string [ ] = [ ] ;
82
+ /** @type {string[] } */
83
+ const bundleUsedTranslationKeys = [ ] ;
78
84
79
85
translatableObj . forEachChild ( ( child ) => {
80
86
ok ( ts . isPropertyAssignment ( child ) ) ;
@@ -90,27 +96,29 @@ translatableObj.forEachChild((child) => {
90
96
91
97
service . dispose ( ) ;
92
98
93
- const enLocale = ( await import ( join ( localesDir , "en.cts" ) ) ) . default ;
99
+ const req = createRequire ( import . meta. url ) ;
100
+ const enLocale = req ( join ( localesDir , "en.cjs" ) ) ;
94
101
95
102
rmSync ( distDir , { recursive : true , force : true } ) ;
96
103
mkdirSync ( distDir , { recursive : true } ) ;
97
104
98
105
for ( const locale of readdirSync ( localesDir ) ) {
106
+ if ( ! locale . endsWith ( ".cjs" ) ) continue ;
99
107
console . log ( `Processing ${ locale } ` ) ;
100
108
101
109
const browserTranslations = { } ;
102
- const translations = ( await import ( join ( localesDir , locale ) ) ) . default ;
110
+ const translations = req ( join ( localesDir , locale ) ) ;
103
111
for ( const key of bundleUsedTranslationKeys ) {
104
112
browserTranslations [ key ] = translations [ key ] || enLocale [ key ] ;
105
113
}
106
114
107
115
writeFileSync (
108
- join ( distDir , locale . replace ( ".cts " , ".js" ) ) ,
116
+ join ( distDir , locale . replace ( ".cjs " , ".js" ) ) ,
109
117
`export default ${ JSON . stringify ( browserTranslations , null , 4 ) } \n` ,
110
118
) ;
111
119
112
120
writeFileSync (
113
- join ( distDir , locale . replace ( ".cts " , ".d.ts" ) ) ,
121
+ join ( distDir , locale . replace ( ".cjs " , ".d.ts" ) ) ,
114
122
`const translations: Record<string, string>;\nexport default translations;\n` ,
115
123
) ;
116
124
}
0 commit comments