-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: publish @geprog/vite-plugin-env-config (#1)
* feat: publish @geprog/vite-plugin-env-config * docs: change wording * docs: improve motivation
- Loading branch information
Showing
22 changed files
with
7,458 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist/ | ||
coverage/ |
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 @@ | ||
/* eslint-env node */ | ||
|
||
require('@geprog/eslint-config/patch/modern-module-resolution'); | ||
|
||
module.exports = { | ||
extends: ['@geprog', '@geprog/eslint-config/jest'], | ||
|
||
env: { | ||
'shared-node-browser': true, | ||
}, | ||
|
||
parserOptions: { | ||
project: ['./tsconfig.eslint.json'], | ||
tsconfigRootDir: __dirname, | ||
extraFileExtensions: ['.cjs'], | ||
}, | ||
}; |
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 @@ | ||
name: 'Lint PR' | ||
|
||
on: | ||
pull_request_target: | ||
types: | ||
- opened | ||
- edited | ||
- synchronize | ||
|
||
jobs: | ||
main: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# https://github.com/amannn/action-semantic-pull-request/releases | ||
- uses: amannn/[email protected] | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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,43 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Cache pnpm modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}- | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6 | ||
run_install: true | ||
|
||
- name: Build | ||
run: pnpm run build | ||
|
||
- name: Release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: pnpm run release |
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,135 @@ | ||
name: Tests | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
unit-tests: | ||
name: Unit tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Cache pnpm modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}- | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6 | ||
run_install: true | ||
|
||
- name: Unit test | ||
run: pnpm run test | ||
|
||
- uses: artiomtr/[email protected] | ||
if: "github.event_name == 'pull_request'" | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
skip-step: all | ||
|
||
typecheck: | ||
name: Typecheck | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Cache pnpm modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}- | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6 | ||
run_install: true | ||
|
||
- name: Typecheck | ||
run: pnpm run typecheck | ||
|
||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Cache pnpm modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}- | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6 | ||
run_install: true | ||
|
||
- name: Lint | ||
run: pnpm run lint | ||
|
||
check-format: | ||
name: Check format | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
|
||
- name: Cache pnpm modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.pnpm-store | ||
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}- | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6 | ||
run_install: true | ||
|
||
- name: Check format | ||
run: pnpm run lint:format |
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,7 @@ | ||
node_modules | ||
dist | ||
coverage/ | ||
junit.xml | ||
*.tgz | ||
report.json | ||
.pnpm-debug.log |
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,4 @@ | ||
dist/ | ||
coverage/ | ||
pnpm-lock.yaml | ||
report.json |
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,8 @@ | ||
{ | ||
"semi": true, | ||
"trailingComma": "all", | ||
"singleQuote": true, | ||
"printWidth": 120, | ||
"tabWidth": 2, | ||
"endOfLine": "lf" | ||
} |
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,3 @@ | ||
{ | ||
"extends": "@geprog/semantic-release-config" | ||
} |
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,63 @@ | ||
# @geprog/vite-plugin-env-config | ||
|
||
Vite plugin for providing configurations from environment variables at runtime. | ||
|
||
## Usage | ||
|
||
Add `envConfig` plugin to `vite.config.js / vite.config.ts`: | ||
|
||
```js | ||
// vite.config.js / vite.config.ts | ||
import { envConfig } from '@geprog/vite-plugin-env-config'; | ||
|
||
export default { | ||
plugins: [envConfig()], | ||
}; | ||
``` | ||
|
||
Add a template file to your project at `./src/assets/env-config.template.js`: | ||
|
||
```js | ||
// ./src/assets/env-config.template.js | ||
(function (window) { | ||
window.env = window.env || {}; | ||
|
||
// add a line for each environment variable | ||
window['env']['BACKEND_URL'] = '${BACKEND_URL}'; | ||
})(this); | ||
``` | ||
|
||
Include `/assets/env-config.js` in your `index.html`: | ||
|
||
```html | ||
<!-- index.html --> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<script src="/assets/env-config.js"></script> | ||
</head> | ||
</html> | ||
``` | ||
|
||
To access the environment variables use the built-in getter: | ||
|
||
```ts | ||
import { getEnvConfig } from '@geprog/vite-plugin-env-config'; | ||
|
||
const backendURL = getEnvConfig('BACKEND_URL'); | ||
``` | ||
|
||
For production use `envsubst` as outlined in the [next section](#motivation). | ||
|
||
## Motivation | ||
|
||
To adhere to the principles of the [twelve-factor app](https://12factor.net/config) | ||
you might need to access environment variables that are set when your frontend server starts. | ||
Instead of building your frontend on startup, | ||
you can use a config template like the one above and populate it using `envsubst`: | ||
|
||
```Dockerfile | ||
CMD ["/bin/sh", "-c", "envsubst < ./src/assets/env-config.template.js > ./dist/assets/env-config.js && exec nginx -g 'daemon off;'"] | ||
``` | ||
|
||
`@geprog/vite-plugin-env-config` provides the same functionality for your development environment. |
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,22 @@ | ||
import { Config } from '@jest/types'; | ||
import { pathsToModuleNameMapper } from 'ts-jest/utils'; | ||
|
||
import { compilerOptions } from './tsconfig.json'; | ||
|
||
const moduleNameMapper = { | ||
...pathsToModuleNameMapper(compilerOptions.paths, { | ||
prefix: '<rootDir>/' + compilerOptions.baseUrl + '/', | ||
}), | ||
} as Config.InitialOptions['moduleNameMapper']; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
roots: ['<rootDir>/test'], | ||
moduleNameMapper, | ||
testEnvironment: 'jest-environment-jsdom', | ||
collectCoverage: true, | ||
coverageReporters: ['json'], | ||
testLocationInResults: true, | ||
}; | ||
|
||
export default config; |
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,56 @@ | ||
{ | ||
"name": "@geprog/vite-plugin-env-config", | ||
"version": "0.0.0-semantic-release", | ||
"description": "Vite Plugin for providing config from environment variables at runtime", | ||
"homepage": "https://geprog.com", | ||
"repository": "github:geprog/vite-plugin-env-config", | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.cjs.js" | ||
} | ||
}, | ||
"main": "./dist/index.cjs.js", | ||
"module": "./dist/index.esm.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"/dist" | ||
], | ||
"scripts": { | ||
"build": "rollup -c rollup.config.ts --configPlugin rollup-plugin-typescript2", | ||
"clean": "rm -rf dist/ node_modules/", | ||
"lint": "eslint --max-warnings 0 .", | ||
"lint:format": "prettier --check .", | ||
"start": "pnpm run build -- --watch", | ||
"test": "jest --forceExit --detectOpenHandles --testLocationInResults --json --outputFile=report.json", | ||
"test:watch": "pnpm run test -- --watch", | ||
"typecheck": "tsc --noEmit", | ||
"release": "semantic-release" | ||
}, | ||
"devDependencies": { | ||
"@geprog/eslint-config": "0.0.1", | ||
"@geprog/semantic-release-config": "1.0.0", | ||
"@jest/types": "26.6.2", | ||
"@types/jest": "26.0.23", | ||
"@types/node": "17.0.10", | ||
"eslint": "7.29.0", | ||
"jest": "26.6.3", | ||
"prettier": "2.3.1", | ||
"rollup": "2.60.2", | ||
"rollup-plugin-dts": "4.0.1", | ||
"rollup-plugin-typescript2": "0.31.1", | ||
"semantic-release": "18.0.0", | ||
"ts-jest": "26.5.6", | ||
"ts-node": "10.0.0", | ||
"tslib": "2.3.1", | ||
"typescript": "4.5.2", | ||
"vite": "2.7.13" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^2.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Oops, something went wrong.