Skip to content

Instantly share code, notes, and snippets.

@AWolf81
Last active February 6, 2021 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AWolf81/edb0872c1cb8b4e96ec4bfbf2f15fc22 to your computer and use it in GitHub Desktop.
Save AWolf81/edb0872c1cb8b4e96ec4bfbf2f15fc22 to your computer and use it in GitHub Desktop.
Demo for injecting the version from package.json with Snowpack
SNOWPACK_PUBLIC_SOME_OTHER_ENV=just a test
<!-- placed in public folder -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="module" src="dist/index.js"></script>
</head>
<body>
%SNOWPACK_PUBLIC_VERSION%
<br/>
Snowpack plugin env loaded "someOtherEnv":
%SNOWPACK_PUBLIC_SOME_OTHER_ENV%
</body>
</html>
// placed in src folder
// you can use your version with import.meta
console.log(import.meta.env)
// plugin for injecting version from package.json
module.exports = () => {
return {
name: "inject-version",
async config() {
const {version} = require("./package.json")
process.env["SNOWPACK_PUBLIC_VERSION"] = version
}
}
}
/** @type {import("snowpack").SnowpackUserConfig } */
module.exports = {
mount: {
public: {url: '/', static: true},
src: {url: '/dist'},
},
plugins: [
"@snowpack/plugin-dotenv",
"./inject-version"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment