Skip to content

Commit

Permalink
Store script tag for global variable in head instead of body (#15)
Browse files Browse the repository at this point in the history
* Update vite.ts

changed location for global var script tag to head.

* fix: updated test snapshots + changed injecTo ifLog
  • Loading branch information
richardvanthof authored Nov 26, 2024
1 parent a6b420c commit 350b86f
Show file tree
Hide file tree
Showing 4 changed files with 6,548 additions and 14 deletions.
20 changes: 10 additions & 10 deletions __tests__/__snapshots__/vite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ exports[`VitePlugin > Assigned version > output: Global 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</head>
<body>
<div id="app"></div>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</body>
</html>
"
Expand All @@ -36,10 +36,10 @@ exports[`VitePlugin > Assigned version > output: Log 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</head>
<body>
<div id="app"></div>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</body>
</html>
"
Expand Down Expand Up @@ -92,11 +92,11 @@ exports[`VitePlugin > Name > assigned 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DvLNQ2tW.js"></script>
<script>console.log("MY_TEST_NAME_VERSION: 1.0.0")</script>
<script>__MY_TEST_NAME_VERSION__ = "1.0.0"</script>
</head>
<body>
<div id="app"></div>
<script>console.log("MY_TEST_NAME_VERSION: 1.0.0")</script>
<script>__MY_TEST_NAME_VERSION__ = "1.0.0"</script>
</body>
</html>
"
Expand All @@ -120,10 +120,10 @@ exports[`vitePlugin > Assigned version & output > output: Global 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</head>
<body>
<div id="app"></div>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</body>
</html>
"
Expand All @@ -138,10 +138,10 @@ exports[`vitePlugin > Assigned version & output > output: Log 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</head>
<body>
<div id="app"></div>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</body>
</html>
"
Expand Down Expand Up @@ -194,11 +194,11 @@ exports[`vitePlugin > name > name: Custom 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DvLNQ2tW.js"></script>
<script>console.log("MY_TEST_NAME_VERSION: 1.0.0")</script>
<script>__MY_TEST_NAME_VERSION__ = "1.0.0"</script>
</head>
<body>
<div id="app"></div>
<script>console.log("MY_TEST_NAME_VERSION: 1.0.0")</script>
<script>__MY_TEST_NAME_VERSION__ = "1.0.0"</script>
</body>
</html>
"
Expand All @@ -222,10 +222,10 @@ exports[`vitePlugin > version & output > output: Global 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</head>
<body>
<div id="app"></div>
<script>__VITE_PLUGIN_VERSION_MARK_VERSION__ = "1.0.0"</script>
</body>
</html>
"
Expand All @@ -240,10 +240,10 @@ exports[`vitePlugin > version & output > output: Log 1`] = `
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<script type="module" crossorigin src="/assets/index-DUby6dX2.js"></script>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</head>
<body>
<div id="app"></div>
<script>console.log("VITE_PLUGIN_VERSION_MARK_VERSION: 1.0.0")</script>
</body>
</html>
"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@eslint/js": "^9.9.1",
"@nuxt/kit": "^3.12.3",
"@nuxt/schema": "^3.12.3",
"@types/node": "^20.14.10",
"@types/node": "^22.10.0",
"eslint": "9.x",
"eslint-plugin-vue": "^9.27.0",
"globals": "^15.9.0",
Expand All @@ -62,7 +62,7 @@
"tsup": "^8.1.0",
"typescript": "^5.5.3",
"typescript-eslint": "^8.3.0",
"vitest": "^1.6.0"
"vitest": "^2.1.6"
},
"peerDependencies": {
"vite": "*"
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ export const vitePluginVersionMark: (options?: VitePluginVersionMarkInput) => Pl
if (ifLog) {
els.push({
tag: 'script',
injectTo: 'body',
injectTo: 'head',
children: `console.log("${printInfo}")`,
})
}
if (ifGlobal) {
els.push({
tag: 'script',
injectTo: 'body',
injectTo: 'head',
children: `__${printName}__ = "${printVersion}"`,
})
}
Expand Down
Loading

0 comments on commit 350b86f

Please sign in to comment.