Skip to content

Commit

Permalink
fix: return explicit dot to config
Browse files Browse the repository at this point in the history
  • Loading branch information
vuode committed Dec 28, 2021
1 parent 17a55ae commit 0f55644
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,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 @@ -132,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
9 changes: 4 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,7 +151,6 @@ 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 })) {
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
formatted['process.env'] = '"MISSING_ENV_VAR"'
Expand All @@ -170,7 +169,7 @@ class Dotenv {
return targets.every(
target =>
// If configured prefix is 'process.env'
this.config.prefix === 'process.env' &&
this.config.prefix === 'process.env.' &&
// If we're not configured to never stub
this.config.ignoreStub !== true &&
// And
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ const WITHOUT_CURLY_BRACES_USER_RECURSIVELY = process.env.WITHOUT_CURLY_BRACES_U
const WITHOUT_CURLY_BRACES_URI_RECURSIVELY = process.env.WITHOUT_CURLY_BRACES_URI_RECURSIVELY

// Alternative prefix
const TEST_ALT = meta.env.TEST
const TEST2_ALT = meta.env.TEST2
const TEST_ALT = META_ENV_TEST
const TEST2_ALT = META_ENV_TEST2
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
})

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

test('Should include environment variables from .env file in the root dir.', (done) => {
expectResultsToContainReplacements(
Expand Down Expand Up @@ -536,7 +536,7 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
describe('Stubbing when prefix is set', () => {
const notStubbed = [
'const TEST_ALT = "testing"',
'const TEST2_ALT = meta.env.TEST2',
'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/
Expand Down

0 comments on commit 0f55644

Please sign in to comment.