├── renovate.json ├── .eslintrc.js ├── .eslintignore ├── commitlint.config.js ├── lib ├── logger.js └── module.js ├── .gitignore ├── jest.config.js ├── husky.config.js ├── .editorconfig ├── test ├── fixture │ ├── no-definitions │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── no-object │ │ └── nuxt.config.js │ ├── env-variables-as-function │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── spa │ │ ├── pages │ │ │ └── index.vue │ │ └── nuxt.config.js │ ├── ssr │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ └── custom-build.extend │ │ ├── pages │ │ └── index.vue │ │ └── nuxt.config.js ├── spa.test.js ├── ssr.test.js ├── no-definitions.test.js ├── custom-build.extend.test.js ├── env-variables-as-function.test.js ├── no-object.test.js └── __snapshots__ │ ├── ssr.test.js.snap │ ├── no-definitions.test.js.snap │ ├── custom-build.extend.test.js.snap │ ├── env-variables-as-function.test.js.snap │ └── spa.test.js.snap ├── CHANGELOG.md ├── .circleci └── config.yml ├── LICENSE ├── package.json └── README.md /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: [ 4 | '@nuxtjs' 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | lib/plugin.js 9 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /lib/logger.js: -------------------------------------------------------------------------------- 1 | const consola = require('consola') 2 | 3 | module.exports = consola.withScope('nuxt:separate-env') 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | testEnvironment: 'node', 3 | collectCoverage: true, 4 | collectCoverageFrom: [ 5 | 'lib/**/*.js', 6 | '!lib/plugin.js' 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', 4 | 'pre-commit': 'yarn lint', 5 | 'pre-push': 'yarn lint' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /test/fixture/no-definitions/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | rootDir: resolve(__dirname, '../../..'), 6 | buildDir: resolve(__dirname, '.nuxt'), 7 | srcDir: __dirname, 8 | render: { 9 | resourceHints: false 10 | }, 11 | modules: [ 12 | { handler: require('../../../') } 13 | ], 14 | env: {} 15 | } 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | ## 0.0.1 (2018-10-13) 7 | 8 | 9 | ### Bug Fixes 10 | 11 | * **test:** mock consola on each test ([40e966a](https://github.com/nuxt-community/separate-env-module/commit/40e966a)) 12 | -------------------------------------------------------------------------------- /test/fixture/no-object/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | rootDir: resolve(__dirname, '../../..'), 6 | buildDir: resolve(__dirname, '.nuxt'), 7 | srcDir: __dirname, 8 | render: { 9 | resourceHints: false 10 | }, 11 | modules: [ 12 | { handler: require('../../../') } 13 | ], 14 | env: { 15 | server: 'no object', 16 | client: 'no object', 17 | normal: 'Hi' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/fixture/env-variables-as-function/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | rootDir: resolve(__dirname, '../../..'), 6 | buildDir: resolve(__dirname, '.nuxt'), 7 | srcDir: __dirname, 8 | render: { 9 | resourceHints: false 10 | }, 11 | modules: [ 12 | { handler: require('../../../') } 13 | ], 14 | env: { 15 | server: () => ({ 16 | ONLY_SERVER: 'yup', 17 | DIFFERENT: 'server', 18 | SECRET: 'nowhereIncluded' 19 | }), 20 | client: { 21 | ONLY_CLIENT: 'okay', 22 | DIFFERENT: 'client' 23 | }, 24 | normal: 'Hi' 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/fixture/spa/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 23 | -------------------------------------------------------------------------------- /test/fixture/ssr/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | rootDir: resolve(__dirname, '../../..'), 6 | buildDir: resolve(__dirname, '.nuxt'), 7 | srcDir: __dirname, 8 | render: { 9 | resourceHints: false 10 | }, 11 | modules: [ 12 | { handler: require('../../../') } 13 | ], 14 | env: { 15 | server: { 16 | ONLY_SERVER: 'yup', 17 | DIFFERENT: 'server', 18 | SECRET: 'nowhereIncluded', 19 | TYPE: true 20 | }, 21 | client: { 22 | ONLY_CLIENT: 'okay', 23 | DIFFERENT: 'client', 24 | TYPE: 10 25 | }, 26 | normal: 'Hi' 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/fixture/ssr/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 23 | -------------------------------------------------------------------------------- /test/fixture/no-definitions/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 23 | -------------------------------------------------------------------------------- /test/fixture/custom-build.extend/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 23 | -------------------------------------------------------------------------------- /test/fixture/env-variables-as-function/pages/index.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 23 | -------------------------------------------------------------------------------- /test/fixture/spa/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | mode: 'spa', 6 | rootDir: resolve(__dirname, '../../..'), 7 | buildDir: resolve(__dirname, '.nuxt'), 8 | srcDir: __dirname, 9 | render: { 10 | resourceHints: false 11 | }, 12 | modules: [ 13 | { handler: require('../../../') } 14 | ], 15 | env: { 16 | server: { 17 | ONLY_SERVER: 'yup', 18 | DIFFERENT: 'server', 19 | SECRET: 'nowhereIncluded', 20 | TYPE: true 21 | }, 22 | client: { 23 | ONLY_CLIENT: 'okay', 24 | DIFFERENT: 'client', 25 | TYPE: 10 26 | }, 27 | normal: 'Hi' 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/spa.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const { waitFor } = require('@nuxt/utils-edge') 5 | const getPort = require('get-port') 6 | 7 | describe('spa', () => { 8 | let nuxt 9 | 10 | beforeAll(async () => { 11 | nuxt = new Nuxt(require('./fixture/spa/nuxt.config')) 12 | await nuxt.ready() 13 | await new Builder(nuxt).build() 14 | await waitFor(2000) 15 | await nuxt.listen(await getPort()) 16 | }) 17 | 18 | afterAll(async () => { 19 | await nuxt.close() 20 | }) 21 | 22 | test('render', async () => { 23 | const { html } = await nuxt.renderRoute('/') 24 | expect(html).toMatchSnapshot() 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /test/ssr.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const { waitFor } = require('@nuxt/utils-edge') 5 | const getPort = require('get-port') 6 | 7 | describe('ssr', () => { 8 | let nuxt 9 | 10 | beforeAll(async () => { 11 | nuxt = new Nuxt(require('./fixture/ssr/nuxt.config')) 12 | await nuxt.ready() 13 | await new Builder(nuxt).build() 14 | await waitFor(2000) 15 | await nuxt.listen(await getPort()) 16 | }) 17 | 18 | afterAll(async () => { 19 | await nuxt.close() 20 | }) 21 | 22 | test('render', async () => { 23 | const { html } = await nuxt.renderRoute('/') 24 | expect(html).toMatchSnapshot() 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /test/no-definitions.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const { waitFor } = require('@nuxt/utils-edge') 5 | const getPort = require('get-port') 6 | 7 | describe('no definitions', () => { 8 | let nuxt 9 | 10 | beforeAll(async () => { 11 | nuxt = new Nuxt(require('./fixture/no-definitions/nuxt.config')) 12 | await nuxt.ready() 13 | await new Builder(nuxt).build() 14 | await waitFor(2000) 15 | await nuxt.listen(await getPort()) 16 | }) 17 | 18 | afterAll(async () => { 19 | await nuxt.close() 20 | }) 21 | 22 | test('render', async () => { 23 | const { html } = await nuxt.renderRoute('/') 24 | expect(html).toMatchSnapshot() 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /test/custom-build.extend.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const { waitFor } = require('@nuxt/utils-edge') 5 | const getPort = require('get-port') 6 | 7 | describe('honor custom build.extend function', () => { 8 | let nuxt 9 | 10 | beforeAll(async () => { 11 | nuxt = new Nuxt(require('./fixture/custom-build.extend/nuxt.config')) 12 | await nuxt.ready() 13 | await new Builder(nuxt).build() 14 | await waitFor(2000) 15 | await nuxt.listen(await getPort()) 16 | }) 17 | 18 | afterAll(async () => { 19 | await nuxt.close() 20 | }) 21 | 22 | test('render', async () => { 23 | const { html } = await nuxt.renderRoute('/') 24 | expect(html).toMatchSnapshot() 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /test/env-variables-as-function.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const { waitFor } = require('@nuxt/utils-edge') 5 | const getPort = require('get-port') 6 | 7 | describe('correctly assign env variables from function', () => { 8 | let nuxt 9 | 10 | beforeAll(async () => { 11 | nuxt = new Nuxt(require('./fixture/env-variables-as-function/nuxt.config')) 12 | await nuxt.ready() 13 | await new Builder(nuxt).build() 14 | await waitFor(2000) 15 | await nuxt.listen(await getPort()) 16 | }) 17 | 18 | afterAll(async () => { 19 | await nuxt.close() 20 | }) 21 | 22 | test('render', async () => { 23 | const { html } = await nuxt.renderRoute('/') 24 | expect(html).toMatchSnapshot() 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /test/fixture/custom-build.extend/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | dev: true, 5 | rootDir: resolve(__dirname, '../../..'), 6 | buildDir: resolve(__dirname, '.nuxt'), 7 | srcDir: __dirname, 8 | render: { 9 | resourceHints: false 10 | }, 11 | modules: [ 12 | { handler: require('../../../') } 13 | ], 14 | env: { 15 | server: { 16 | ONLY_SERVER: 'yup', 17 | DIFFERENT: 'server' 18 | }, 19 | client: { 20 | ONLY_CLIENT: 'okay', 21 | DIFFERENT: 'client' 22 | }, 23 | normal: 'Hi' 24 | }, 25 | build: { 26 | extend (config) { 27 | const DefinePlugin = config.plugins.find(p => p.constructor.name === 'DefinePlugin') 28 | Object.assign(DefinePlugin.definitions, { 'process.env.ONLY_SERVER': JSON.stringify('nope') }) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/no-object.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const logger = require('../lib/logger') 5 | 6 | logger.mockTypes(() => jest.fn()) 7 | 8 | describe('no object', () => { 9 | let nuxt 10 | 11 | beforeAll(async () => { 12 | nuxt = new Nuxt(require('./fixture/no-object/nuxt.config')) 13 | await nuxt.ready() 14 | await new Builder(nuxt).build() 15 | }) 16 | 17 | afterAll(async () => { 18 | await nuxt.close() 19 | }) 20 | 21 | test('should display logger error', () => { 22 | expect(logger.error).toHaveBeenNthCalledWith(1, 'Could not assign client env variables. Please provide an object or a function that returns an object!') 23 | expect(logger.error).toHaveBeenNthCalledWith(2, 'Could not assign server env variables. Please provide an object or a function that returns an object!') 24 | }) 25 | }) 26 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/node 6 | steps: 7 | # Checkout repository 8 | - checkout 9 | 10 | # Restore cache 11 | - restore_cache: 12 | key: yarn-cache-{{ checksum "yarn.lock" }} 13 | 14 | # Install dependencies 15 | - run: 16 | name: Install Dependencies 17 | command: NODE_ENV=dev yarn 18 | 19 | # Keep cache 20 | - save_cache: 21 | key: yarn-cache-{{ checksum "yarn.lock" }} 22 | paths: 23 | - "node_modules" 24 | 25 | # Lint 26 | - run: 27 | name: Lint 28 | command: yarn lint 29 | 30 | # Tests 31 | - run: 32 | name: Tests 33 | command: yarn jest 34 | 35 | # Coverage 36 | - run: 37 | name: Coverage 38 | command: yarn codecov 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Nuxt Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nuxtjs/separate-env", 3 | "version": "0.0.1", 4 | "description": "Separated environment variables for server and client builds", 5 | "keywords": [ 6 | "nuxtjs", 7 | "nuxt", 8 | "env", 9 | "environment", 10 | "separate", 11 | "variables" 12 | ], 13 | "license": "MIT", 14 | "contributors": [ 15 | "Alexander Lichter " 16 | ], 17 | "main": "lib/module.js", 18 | "repository": "nuxt-community/separate-env-module", 19 | "publishConfig": { 20 | "access": "public" 21 | }, 22 | "scripts": { 23 | "dev": "nuxt test/fixture/ssr", 24 | "lint": "eslint --ext .js,.vue lib test", 25 | "test": "yarn lint && jest", 26 | "release": "yarn test && standard-version && git push --follow-tags && npm publish" 27 | }, 28 | "files": [ 29 | "lib" 30 | ], 31 | "dependencies": { 32 | "consola": "^2.15.0" 33 | }, 34 | "devDependencies": { 35 | "@commitlint/cli": "latest", 36 | "@commitlint/config-conventional": "latest", 37 | "@nuxtjs/eslint-config": "latest", 38 | "codecov": "latest", 39 | "eslint": "latest", 40 | "get-port": "latest", 41 | "husky": "latest", 42 | "jest": "latest", 43 | "nuxt-edge": "latest", 44 | "standard-version": "latest" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test/__snapshots__/ssr.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`ssr render 1`] = ` 4 | " 5 | 6 | 7 | 27 | 28 | 29 |

server

{"ONLY_CLIENT":"undefined","ONLY_SERVER":"yup","DIFFERENT":"server","TYPE":true,"normal":"Hi"}
30 | 31 | 32 | " 33 | `; 34 | -------------------------------------------------------------------------------- /test/__snapshots__/no-definitions.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`no definitions render 1`] = ` 4 | " 5 | 6 | 7 | 27 | 28 | 29 |

server

{"ONLY_CLIENT":"undefined","ONLY_SERVER":"undefined","DIFFERENT":"undefined","TYPE":"undefined","normal":"undefined"}
30 | 31 | 32 | " 33 | `; 34 | -------------------------------------------------------------------------------- /test/__snapshots__/custom-build.extend.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`honor custom build.extend function render 1`] = ` 4 | " 5 | 6 | 7 | 27 | 28 | 29 |

server

{"ONLY_CLIENT":"undefined","ONLY_SERVER":"nope","DIFFERENT":"server","TYPE":"undefined","normal":"Hi"}
30 | 31 | 32 | " 33 | `; 34 | -------------------------------------------------------------------------------- /test/__snapshots__/env-variables-as-function.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`correctly assign env variables from function render 1`] = ` 4 | " 5 | 6 | 7 | 27 | 28 | 29 |

server

{"ONLY_CLIENT":"undefined","ONLY_SERVER":"yup","DIFFERENT":"server","TYPE":"undefined","normal":"Hi"}
30 | 31 | 32 | " 33 | `; 34 | -------------------------------------------------------------------------------- /lib/module.js: -------------------------------------------------------------------------------- 1 | const logger = require('./logger') 2 | 3 | module.exports = function () { 4 | const { server, client, ...env } = this.nuxt.options.env 5 | // Set all other env variables except nested server/clients 6 | this.nuxt.options.env = env 7 | 8 | const assignEnvVariables = (config, { isServer }) => { 9 | logger.debug(`Assigning ${isServer ? 'server' : 'client'} env variables now`) 10 | 11 | let definitionsToAdd = isServer ? server : client 12 | 13 | if (typeof definitionsToAdd === 'undefined') { 14 | // No definitions for server/client provided on purpose 15 | return 16 | } 17 | 18 | if (typeof definitionsToAdd === 'function') { 19 | definitionsToAdd = definitionsToAdd() 20 | } 21 | 22 | if (typeof definitionsToAdd !== 'object') { 23 | logger.error(`Could not assign ${isServer ? 'server' : 'client'} env variables. Please provide an object or a function that returns an object!`) 24 | return 25 | } 26 | 27 | const DefinePlugin = config.plugins.find(p => p.constructor.name === 'DefinePlugin') 28 | const definitionsToAddWithPrefix = applyPrefix(definitionsToAdd) 29 | 30 | Object.assign(DefinePlugin.definitions, definitionsToAddWithPrefix) 31 | } 32 | 33 | const oldExtendFunction = this.options.build.extend 34 | 35 | this.options.build.extend = (...args) => { 36 | assignEnvVariables(...args) 37 | if (oldExtendFunction) { 38 | oldExtendFunction(...args) 39 | } 40 | } 41 | } 42 | 43 | function applyPrefix (definitionsToAdd) { 44 | return Object.entries(definitionsToAdd).reduce((newObject, [key, value]) => { 45 | // Logic from https://github.com/nuxt/nuxt.js/blob/820f0fae1a94793a2b9cf75e398531ab77e5bc8c/lib/builder/webpack/base.js#L70 46 | newObject[`process.env.${key}`] = ['boolean', 'number'].includes(typeof value) ? value : JSON.stringify(value) 47 | return newObject 48 | }, {}) 49 | } 50 | 51 | module.exports.meta = require('../package.json') 52 | -------------------------------------------------------------------------------- /test/__snapshots__/spa.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`spa render 1`] = ` 4 | " 5 | 6 | 7 | 8 | 9 | 10 |
Loading...
11 | 12 | 13 | " 14 | `; 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Separate env module - Tear your variables apart! 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Circle CI][circle-ci-src]][circle-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![License][license-src]][license-href] 8 | 9 | > Separated environment variables for `server` and `client` build 10 | 11 | [📖 **Release Notes**](./CHANGELOG.md) 12 | 13 | :warning: With Nuxt v2.13 you might want to use the [new runtime config](https://nuxtjs.org/blog/moving-from-nuxtjs-dotenv-to-runtime-config) instead of this. 14 | 15 | ## Setup 16 | 17 | 1. Add `@nuxtjs/separate-env` dependency to your project 18 | 19 | ```bash 20 | yarn add @nuxtjs/separate-env # or npm install @nuxtjs/separate-env 21 | ``` 22 | 23 | 2. Add `@nuxtjs/separate-env` to the `modules` section of `nuxt.config.js` 24 | 25 | ```js 26 | { 27 | modules: [ 28 | '@nuxtjs/separate-env' 29 | ], 30 | env: { 31 | // Your environment variables here (see Configuration section below) 32 | } 33 | } 34 | ``` 35 | 36 | ## Configuration 37 | 38 | To define environment variables only available on `server`/`client` side, 39 | use the `env` key of your `nuxt.config.js` and nest the variables 40 | in a `server` or `client` object: 41 | 42 | ```js 43 | { 44 | env: { 45 | server: { 46 | ONLY_SERVER: 'yup', 47 | DIFFERENT_ON_BOTH: 'server' 48 | }, 49 | client: { 50 | ONLY_CLIENT: 'okay', 51 | DIFFERENT_ON_BOTH: 'client' 52 | }, 53 | normalEnvVariableThatWillBeAvailableEverywhere: 'Hi' 54 | } 55 | } 56 | ``` 57 | 58 | That's it! You are good to go. 59 | 60 | ## Caveats 61 | 62 | **IMPORTANT:** Be aware that `server-side` means on **every** first render of your application. 63 | Your secret tokens won't be included anywhere **except where you use them** 64 | 65 | ## Development 66 | 67 | 1. Clone this repository 68 | 2. Install dependencies using `yarn install` or `npm install` 69 | 3. Start development server using `npm run dev` 70 | 71 | ## License 72 | 73 | [MIT License](./LICENSE) 74 | 75 | Copyright (c) - Nuxt Community 76 | 77 | 78 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/separate-env/latest.svg?style=flat-square 79 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/separate-env 80 | 81 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/separate-env.svg?style=flat-square 82 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/separate-env 83 | 84 | [circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/separate-env-module.svg?style=flat-square 85 | [circle-ci-href]: https://circleci.com/gh/nuxt-community/separate-env-module 86 | 87 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/separate-env-module.svg?style=flat-square 88 | [codecov-href]: https://codecov.io/gh/nuxt-community/separate-env-module 89 | 90 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/separate-env.svg?style=flat-square 91 | [license-href]: https://npmjs.com/package/@nuxtjs/separate-env 92 | --------------------------------------------------------------------------------