Skip to content

Commit f1e029d

Browse files
committed
feat(vite-plugin-translate): support initializeScript
1 parent b08daaa commit f1e029d

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

packages/vite-plugin-translate/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ export default defineConfig({
2121
plugins: [
2222
ViteTranslatePlugin({
2323
version: '4.0.3',
24+
initializeScript: `
25+
translate.language.setLocal('english');
26+
translate.execute();
27+
`,
2428
}),
2529
],
2630
})
2731
```
2832

2933
`ViteTranslatePlugin` will read `bundle/<version>/translate.js` from the installed package, emit it to the build output with a version and timestamp in the file name, and inject the script tag into HTML automatically.
3034

35+
When `initializeScript` is provided, the plugin injects it as a second script tag after `translate.js`, so the initialization code runs only after `translate.js` has been loaded and executed.
36+
3137
Supported versions: `3.18.66`, `4.0.3`.
3238

3339
## License

packages/vite-plugin-translate/src/index.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PluginOption, ResolvedConfig } from 'vite'
1+
import type { HtmlTagDescriptor, PluginOption, ResolvedConfig } from 'vite'
22
import { readFile } from 'node:fs/promises'
33
import { ensureTrailingSlash } from './utils'
44

@@ -23,13 +23,47 @@ async function readBundleSource(version: VERSION): Promise<string> {
2323
}
2424
}
2525

26+
function createScriptTags(options: {
27+
script?: { children: string } | { src: string }
28+
initializeScript?: string
29+
}): HtmlTagDescriptor[] {
30+
const tags: HtmlTagDescriptor[] = []
31+
32+
if (options.script) {
33+
if ('children' in options.script) {
34+
tags.push({ children: options.script.children, tag: 'script' })
35+
}
36+
else {
37+
tags.push({ attrs: { src: options.script.src }, tag: 'script' })
38+
}
39+
}
40+
41+
if (options.initializeScript?.trim()) {
42+
tags.push({ children: options.initializeScript, tag: 'script' })
43+
}
44+
45+
return tags
46+
}
47+
2648
export interface PluginOptions {
49+
/**
50+
* @description: 是否自动注入translate.js脚本到html中,默认为true
51+
*/
2752
inject?: boolean
53+
54+
/**
55+
* @description: translate.js版本,必填项
56+
*/
2857
version: VERSION
58+
59+
/**
60+
* @description: 在 translate.js 加载并执行后注入执行的初始化脚本
61+
*/
62+
initializeScript?: string
2963
}
3064

3165
export function ViteTranslatePlugin(options: PluginOptions): PluginOption | undefined {
32-
const { inject = true, version } = options
66+
const { inject = true, version, initializeScript } = options
3367

3468
if (!inject) {
3569
return
@@ -74,14 +108,20 @@ export function ViteTranslatePlugin(options: PluginOptions): PluginOption | unde
74108
if (config.command === 'serve') {
75109
return {
76110
html,
77-
tags: [{ children: await loadBundleSource(), tag: 'script' }],
111+
tags: createScriptTags({
112+
script: { children: await loadBundleSource() },
113+
initializeScript,
114+
}),
78115
}
79116
}
80117

81118
const src = `${publicPath}${emittedFileName}`
82119
return {
83120
html,
84-
tags: [{ attrs: { src }, tag: 'script' }],
121+
tags: createScriptTags({
122+
script: { src },
123+
initializeScript,
124+
}),
85125
}
86126
},
87127
}

0 commit comments

Comments
 (0)