{"id":42830650,"url":"https://github.com/weavedev/jit-env-webpack-plugin","last_synced_at":"2026-01-30T11:27:31.361Z","repository":{"id":46808955,"uuid":"385587047","full_name":"weavedev/jit-env-webpack-plugin","owner":"weavedev","description":null,"archived":false,"fork":false,"pushed_at":"2022-10-07T12:50:50.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-27T05:38:50.310Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weavedev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-13T11:54:49.000Z","updated_at":"2021-10-22T08:07:33.000Z","dependencies_parsed_at":"2023-01-19T15:03:57.264Z","dependency_job_id":null,"html_url":"https://github.com/weavedev/jit-env-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/weavedev/jit-env-webpack-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weavedev%2Fjit-env-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weavedev%2Fjit-env-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weavedev%2Fjit-env-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weavedev%2Fjit-env-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weavedev","download_url":"https://codeload.github.com/weavedev/jit-env-webpack-plugin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weavedev%2Fjit-env-webpack-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28911821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-30T08:15:08.179Z","status":"ssl_error","status_checked_at":"2026-01-30T08:14:31.507Z","response_time":66,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-30T11:27:30.651Z","updated_at":"2026-01-30T11:27:31.349Z","avatar_url":"https://github.com/weavedev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jit-env-webpack-plugin\n\n[![MIT](https://img.shields.io/github/license/weavedev/jit-env-webpack-plugin.svg)](https://github.com/weavedev/jit-env-webpack-plugin/blob/master/LICENSE)\n[![NPM](https://img.shields.io/npm/v/@weavedev/jit-env-webpack-plugin.svg)](https://www.npmjs.com/package/@weavedev/jit-env-webpack-plugin)\n\nInjects .env.json files into the web application for development and adds an injection point for just-in-time injection when used in production.\n\n## Install\n\n```\nnpm i @weavedev/jit-env-webpack-plugin\n```\n\n## Why‽\n\nThe reason we created this plugin is that we want our staging containers to be used in production without having to rebuild them. This plugin adds a bit of code that allows you to inject a JSON env into your project with minimal effort when used in production. It also allows you to inject your local env files while developing locally.\n\n## Usage\n\n### `webpack.config.js`\n\n```js\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\nconst JitEnvWebpackPlugin = require('@weavedev/jit-env-webpack-plugin');\n\nmodule.exports = {\n  plugins: [\n    new HtmlWebpackPlugin(),\n    new JitEnvWebpackPlugin(),\n  ],\n};\n```\n\n### Options\n\nJitEnvWebpackPlugin provides the following options.\n\n```js\nnew JitEnvWebpackPlugin({\n    /**\n     * The default env file to use. This is usefull if you want to provide an\n     * example structure or provide a default config to a testing environment.\n     * This config is also used when generating type definitions with the\n     * `emitTypes` option.\n     */\n    defaultEnv: \"./default.env.json\",\n\n    /**\n     * The path to a local env file to use. If the file can not be found the\n     * `defaultEnv` file is used and a warning is shown in the browser's\n     * console.\n     *\n     * Add this path to your .gitignore to prevent developers from adding their\n     * local env file to the repository.\n     */\n    userEnv: \"./user.env.json\",\n\n    /**\n     * Generate a simple TypeScript types file from the `defaultEnv` file. You\n     * may need to import this file somewhere (depending on your TypeScript\n     * configuration) for TypeScript to find the types.\n     *\n     * This file will also export an env object to use if you don't want to use\n     * `window.env` in your project.\n     */\n    emitTypes: \"./src/myEnv.ts\",\n\n    /**\n     * If the emitted TypeScript types file causes linting issues you can\n     * provide a prefix string that will be added to the `emitTypes` file\n     * before it is emitted.\n     *\n     * You probably don't need this because most linters allow you to exclude\n     * files in the linter's configuration, but it is here if you need it.\n     */\n    emitTypesPrefix: \"/* tslint:disable */\",\n\n    /**\n     * Allows you to omit the `?` from the emitted type declarations.\n     */\n    emitTypesNonOptional: true,\n});\n```\n\n###### NOTE\n\nThese options should really only be used while developing your application. See the full config example below for more details on using JitEnvWebpackPlugin in production.\n\n### Usage in code\n\nIf we have an env file...\n\n```json\n{\n    \"baseUrl\": \"http://localhost:8001\",\n    \"devMode\": true\n}\n```\n\n...we can use the variables in our code like this:\n\n```js\nif (window.env.devMode) {\n    console.log(`Using dev mode with API: ${window.env.baseUrl}`);\n}\n```\n\n...and in TypeScript like this:\n\n```ts\n// Configure this path in the `emitTypes` option\nimport { env } from './myEnv.ts';\n\nif (env.devMode) {\n    console.log(`Using dev mode with API: ${env.baseUrl}`);\n}\n```\n\n### Emit TypeScript types\n\nYou can configure a target path to emit a TypeScript types file that will be generated from the default env file.\n\nBy configuring JitEnvWebpackPlugin like this:\n\n```js\nnew JitEnvWebpackPlugin({\n    defaultEnv: \"./default.env.json\",\n    emitTypes: \"./src/myEnv.ts\",\n});\n```\n\nIf your default env file looks like this...\n\n```json\n{\n    \"baseUrl\": \"http://localhost:8001\",\n    \"devMode\": true,\n    \"retry\": 3,\n    \"servers\": {\n        \"cdn\": \"http://localhost:8002\",\n        \"s3\": \"http://localhost:9000\"\n    },\n    \"users\": [\n        {\n            \"name\": \"Will\",\n            \"id\": 1\n        },\n        {\n            \"name\": \"Matt\",\n            \"id\": 2\n        }\n    ]\n}\n```\n\n...JitEnvWebpackPlugin will generate a TypeScript types file that looks like this:\n\n```ts\n// This file was generated by JitEnvWebpackPlugin.\n//\n// If this file causes linting issues, you can pass a linting disable string\n// with the emitTypesPrefix option.\n\nif (window.env === undefined) {\n  throw new Error(\"[JIT-ENV] Missing env\");\n}\n\nexport const env: Window['env'] = window.env;\n\ndeclare global {\n  interface Window {\n    env: {\n      \"baseUrl\"?: string;\n      \"devMode\"?: boolean;\n      \"retry\"?: number;\n      \"servers\"?: {\n        \"cdn\"?: string;\n        \"s3\"?: string;\n      };\n      \"users\"?: {\n        \"name\"?: string;\n        \"id\"?: number;\n      }[];\n    };\n  }\n}\n\n```\n\n### Full `webpack.config.js` example\n\n```js\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\nconst JitEnvWebpackPlugin = require('@weavedev/jit-env-webpack-plugin');\n\n/**\n * When building for production you use:\n * \n * webpack --env.production\n */\nconst PROD = env.production;\n\nconst jitEnvWebpackPlugin = PROD ? new JitEnvWebpackPlugin() : new JitEnvWebpackPlugin({\n    defaultEnv: \"./default.env.json\",\n    userEnv: \"./user.env.json\",\n    emitTypes: \"./src/myEnv.ts\",\n});\n\nmodule.exports = {\n  plugins: [\n    new HtmlWebpackPlugin(),\n    jitEnvWebpackPlugin,\n  ],\n};\n```\n\n### Replacing in CI/CD or in containers\n\n```sh\n# Load path to your env file\nexport REPLACE_CONFIG=/path/to/config.env.json\n\n# Load env file contents\nCONFIG=$(cat $REPLACE_CONFIG | tr -d '\\n' | tr -d '\\r' | sed 's/\"/\\\\\"/g' | sed \"s/\\//\\\\\\\\\\//g\");\n\n# Inject env file contents into your index.html\nsed -i \"s/\\\"___INJECT_ENV___\\\"/$CONFIG/g\" /path/to/index.html;\n```\n\nWe use a Dockerfile that mounts a JSON file and uses a script with the above as entrypoint to handle this step.\n\n## License\n\n[MIT](https://github.com/weavedev/store/blob/master/LICENSE)\n\nMade by [Paul Gerarts](https://github.com/gerarts) and [Weave](https://weave.nl)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweavedev%2Fjit-env-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweavedev%2Fjit-env-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweavedev%2Fjit-env-webpack-plugin/lists"}