Skip to content

Commit

Permalink
feat: disable stubbing when prefix is set
Browse files Browse the repository at this point in the history
  • Loading branch information
vuode committed Dec 27, 2021
1 parent 52ba2c6 commit 17a55ae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Due to the fact that we use `webpack.DefinePlugin` under the hood, we cannot sup

We automatically replace any remaining `process.env`s in these environments with `"MISSING_ENV_VAR"` to avoid these errors.

When the `prefix` option is set, `process.env`s will not be stubbed.

If you are running into issues where you or another package you use interfaces with `process.env`, it might be best to set `ignoreStub: true` and make sure you always reference variables that exist within your code (See [this issue](https://github.com/mrsteele/dotenv-webpack/issues/271) for more information).

## Properties
Expand All @@ -115,7 +117,7 @@ Use the following properties to configure your instance.
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
* **defaults** (`false`) - Adds support for `dotenv-defaults`. If set to `true`, uses `./.env.defaults`. If a string, uses that location for a defaults file. Read more at [npm](https://www.npmjs.com/package/dotenv-defaults).
* **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
* **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables.
* **prefix** (`'process.env'`) - The prefix to use before the name of your env variables.

The following example shows how to set any/all arguments.

Expand All @@ -130,7 +132,7 @@ module.exports = {
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
silent: true, // hide any errors
defaults: false, // load '.env.defaults' as the default values if empty.
prefix: 'import.meta.env.' // reference your env variables as 'import.meta.env.ENV_VAR'.
prefix: 'import.meta.env' // reference your env variables as 'import.meta.env.ENV_VAR'.
})
]
...
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Dotenv {
* @param {Boolean|String} [options.safe=false] - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
* @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
* @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
* @param {String} [options.prefix=process.env.] - The prefix, used to denote environment variables.
* @param {String} [options.prefix=process.env] - The prefix, used to denote environment variables.
* @returns {webpack.DefinePlugin}
*/
constructor (config = {}) {
this.config = Object.assign({}, {
path: './.env',
prefix: 'process.env.'
prefix: 'process.env'
}, config)
}

Expand Down Expand Up @@ -128,7 +128,7 @@ class Dotenv {
const { expand, prefix } = this.config
const formatted = Object.keys(variables).reduce((obj, key) => {
const v = variables[key]
const vKey = `${prefix}${key}`
const vKey = `${prefix}.${key}`
let vValue
if (expand) {
if (v.substring(0, 2) === '\\$') {
Expand All @@ -151,10 +151,10 @@ class Dotenv {
// https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
// However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
// https://webpack.js.org/configuration/target
// Also, if the prefix option is set to any value other than 'process.env', it will not be stubbed
if (this.shouldStub({ target, version })) {
const replace = this.config.prefix.slice(0, -1) // Remove the dot in the end of prefix
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
formatted[replace] = '"MISSING_ENV_VAR"'
formatted['process.env'] = '"MISSING_ENV_VAR"'
}

return formatted
Expand All @@ -169,6 +169,8 @@ class Dotenv {

return targets.every(
target =>
// If configured prefix is 'process.env'
this.config.prefix === 'process.env' &&
// If we're not configured to never stub
this.config.ignoreStub !== true &&
// And
Expand Down
38 changes: 33 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,44 +491,72 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
})

describe('Alternative prefix', () => {
const prefix = 'meta.env'

test('Should include environment variables from .env file in the root dir.', (done) => {
expectResultsToContainReplacements(
new DotenvPlugin({ prefix: 'meta.env.' }),
new DotenvPlugin({ prefix }),
altDefaultEnvResult,
done
)
})

test('Should include environment variables from provided .env file.', (done) => {
expectResultsToContainReplacements(
new DotenvPlugin({ path: envSimple, prefix: 'meta.env.' }),
new DotenvPlugin({ path: envSimple, prefix }),
altSimpleResult,
done
)
})

test('Should include default environment variables from .env.defaults with .env overrides.', (done) => {
expectResultsToContainReplacements(
new DotenvPlugin({ defaults: true, prefix: 'meta.env.' }),
new DotenvPlugin({ defaults: true, prefix }),
altDefaultsResult,
done
)
})

test('Should include default environment variables from provided defaults file with .env overrides.', (done) => {
expectResultsToContainReplacements(
new DotenvPlugin({ defaults: envDefaults, prefix: 'meta.env.' }),
new DotenvPlugin({ defaults: envDefaults, prefix }),
altDefaultsResult2,
done
)
})

test('Should include environment variables with empty vars from provided .env file.', (done) => {
expectResultsToContainReplacements(
new DotenvPlugin({ path: envOneEmpty, prefix: 'meta.env.' }),
new DotenvPlugin({ path: envOneEmpty, prefix }),
altOneEmptyResult,
done
)
})

describe('Stubbing when prefix is set', () => {
const notStubbed = [
'const TEST_ALT = "testing"',
'const TEST2_ALT = meta.env.TEST2',
// Replacement of NODE_ENV to mode, specified in webpack config,
// is inteded behaviour of webpack
// @see https://webpack.js.org/configuration/mode/
'const NODE_ENV = "development"',
'const BASIC = process.env.BASIC',
'const MONGOLAB_USER = process.env.MONGOLAB_USER',
'const MONGOLAB_PORT = process.env.MONGOLAB_PORT'
]

test('Should not stab if prefix is set to value other than process.env', (done) => {
const plugin = new DotenvPlugin({ prefix, path: envSimple })

compile(getConfig('web', plugin), (result) => {
notStubbed.forEach((expectation) => {
expect(result).toMatch(expectation)
})

done()
})
})
})
})
})

0 comments on commit 17a55ae

Please sign in to comment.