From 0f55644e463780190a731b37805abb12b25c26e9 Mon Sep 17 00:00:00 2001 From: Ilia Byzov <17595195+vuode@users.noreply.github.com> Date: Tue, 28 Dec 2021 18:16:45 +0300 Subject: [PATCH] fix: return explicit dot to config --- README.md | 4 ++-- src/index.js | 9 ++++----- test/fixtures/index.js | 4 ++-- test/index.test.js | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5c773e0..d4dc8f1 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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'. }) ] ... diff --git a/src/index.js b/src/index.js index a19e9b0..3dbab8e 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } @@ -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) === '\\$') { @@ -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"' @@ -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 diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 7d73a4b..d27e482 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -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 diff --git a/test/index.test.js b/test/index.test.js index bfa3a18..47135ec 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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( @@ -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/