-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package-lock.json | |
node_modules | ||
dist | ||
vite_dist | ||
coverage | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`replaceCompleteConfig 1`] = ` | ||
"<!DOCTYPE html> | ||
<html lang=\\"en\\"> | ||
<head> | ||
<meta charset=\\"UTF-8\\" /> | ||
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" /> | ||
<meta name=\\"test\\" content=\\"{{ VITE_TEST_IN_HEAD }}\\" /> | ||
<title>Vite Test html</title> | ||
</head> | ||
<body> | ||
<h1>Hello {{ VITE_TEST_PERSON_NAME }}</h1> | ||
<script type=\\"application/ecmascript\\">window.config = Object.freeze({\\"VITE_TEST_IN_HEAD\\":\\"test-in-head-value\\",\\"VITE_TEST_PERSON_NAME\\":\\"max mustermann\\"});</script> | ||
</body> | ||
</html> | ||
" | ||
`; | ||
exports[`replaceIndividualKeys 1`] = ` | ||
"<!DOCTYPE html> | ||
<html lang=\\"en\\"> | ||
<head> | ||
<meta charset=\\"UTF-8\\" /> | ||
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" /> | ||
<meta name=\\"test\\" content=\\"test-in-head-value\\" /> | ||
<title>Vite Test html</title> | ||
</head> | ||
<body> | ||
<h1>Hello max mustermann</h1> | ||
<script type=\\"application/ecmascript\\">window.config = Object.freeze({% VITE_RT_CONFIG %});</script> | ||
</body> | ||
</html> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EmittedFile } from "rollup"; | ||
import { test, expect, vi, beforeAll } from "vitest"; | ||
import { runtimeConfig } from "@/index"; | ||
|
||
const mockPluginContext = { | ||
emitFile: (_emittedFile: EmittedFile) => {}, | ||
}; | ||
|
||
type MockHook = (this: typeof mockPluginContext, ...args: any) => Promise<void>; | ||
|
||
beforeAll(() => { | ||
vi.spyOn(mockPluginContext, "emitFile"); | ||
vi.mock("../src/scripts", () => { | ||
return { | ||
readPatchRuntimeConfigScript() { | ||
return Promise.resolve("test_patch_script"); | ||
}, | ||
readPatchRuntimeConfigBinary(target: string) { | ||
return Promise.resolve(`test_patch_${target}_binary`); | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
||
test.concurrent("generateBundle hook", async () => { | ||
const plugin = runtimeConfig({ | ||
emitPatchScript: true, | ||
emitGnuLinuxPatchBinary: true, | ||
emitAlpinePatchBinary: true, | ||
}); | ||
await (plugin.generateBundle as MockHook).call(mockPluginContext); | ||
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{ | ||
type: "asset", | ||
fileName: "patch_runtime_config.js", | ||
source: "test_patch_script", | ||
}); | ||
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{ | ||
type: "asset", | ||
fileName: "patch_runtime_config.gnu_linux.bin", | ||
source: "test_patch_gnu_linux_binary", | ||
}); | ||
expect(mockPluginContext.emitFile).toHaveBeenCalledWith(<EmittedFile>{ | ||
type: "asset", | ||
fileName: "patch_runtime_config.alpine.bin", | ||
source: "test_patch_alpine_binary", | ||
}); | ||
}); | ||
|
||
test.concurrent("configResolved hook", () => { | ||
const plugin = runtimeConfig(); | ||
(plugin.configResolved as MockHook).call(mockPluginContext); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { test, expect, beforeAll } from "vitest"; | ||
import { readFile } from "node:fs/promises"; | ||
import * as Path from "path"; | ||
import * as patch_html from "@/patch_html"; | ||
|
||
let html: string; | ||
let mock_cfg: patch_html.ConfigLike = { | ||
envPrefix: "VITE_", | ||
env: { | ||
VITE_TEST_IN_HEAD: "test-in-head-value", | ||
VITE_TEST_PERSON_NAME: "max mustermann", | ||
}, | ||
}; | ||
|
||
beforeAll(async () => { | ||
html = await readFile(Path.join(__dirname, "test.html"), { | ||
encoding: "utf8", | ||
}); | ||
}); | ||
|
||
test.concurrent("replaceIndividualKeys", () => { | ||
expect(patch_html.replaceIndividualKeys(html, mock_cfg)).toMatchSnapshot(); | ||
}); | ||
|
||
test.concurrent("replaceCompleteConfig", () => { | ||
expect(patch_html.replaceCompleteConfig(html, mock_cfg)).toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta name="test" content="{{ VITE_TEST_IN_HEAD }}" /> | ||
<title>Vite Test html</title> | ||
</head> | ||
<body> | ||
<h1>Hello {{ VITE_TEST_PERSON_NAME }}</h1> | ||
<script type="application/ecmascript"> | ||
window.config = Object.freeze({% VITE_RT_CONFIG %}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
resolve: { | ||
alias: { | ||
"@": "src/", | ||
}, | ||
}, | ||
test: { | ||
coverage: { | ||
enabled: true, | ||
provider: "istanbul", | ||
all: true, | ||
include: ["src/**/*.ts"], | ||
}, | ||
}, | ||
}); |