Skip to content

Commit

Permalink
fix cannot load the advanced configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
htdangkhoa committed Jan 23, 2022
1 parent 3b1bd12 commit b66c8df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
42 changes: 24 additions & 18 deletions webpack/plugins/dotenv-webpack-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ const NODE_ENV = process.env.NODE_ENV;

const envPath = path.resolve(process.cwd(), `.env`);

const loadEnv = (...paths) => {
const envFiles = [
`${envPath}.${NODE_ENV}.local`,
/**
* Don't include `.env.local` for `test` environment
* since normally you expect tests to produce the same
* results for everyone
*/
NODE_ENV !== 'test' && `${envPath}.local`,
`${envPath}.${NODE_ENV}`,
envPath,
...paths,
].filter(Boolean);

envFiles.forEach((file) => {
if (fs.existsSync(file)) {
dotenvExpand(dotenv.config({ path: file }));
}
});
};
exports.loadEnv = loadEnv;

class DotenvWebpackPlugin {
/**
* @param {Object} [options]
Expand All @@ -22,23 +44,7 @@ class DotenvWebpackPlugin {

this.options = { ...options };

const envFiles = [
`${envPath}.${NODE_ENV}.local`,
/**
* Don't include `.env.local` for `test` environment
* since normally you expect tests to produce the same
* results for everyone
*/
NODE_ENV !== 'test' && `${envPath}.local`,
`${envPath}.${NODE_ENV}`,
this.options.path || envPath,
].filter(Boolean);

envFiles.forEach((file) => {
if (fs.existsSync(file)) {
dotenvExpand(dotenv.config({ path: file }));
}
});
loadEnv(this.options.path);
}

/**
Expand Down Expand Up @@ -84,4 +90,4 @@ class DotenvWebpackPlugin {
}
}

module.exports = DotenvWebpackPlugin;
exports.DotenvWebpackPlugin = DotenvWebpackPlugin;
4 changes: 3 additions & 1 deletion webpack/webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const ESLintPlugin = require('eslint-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const eslintFormatter = require('react-dev-utils/eslintFormatter');

const DotenvWebpackPlugin = require('./plugins/dotenv-webpack-plugin');
const { DotenvWebpackPlugin, loadEnv } = require('./plugins/dotenv-webpack-plugin');

// load default env
loadEnv();
const emitErrorsAsWarnings = process.env.ESLINT_NO_DEV_ERRORS === 'true';
const disableESLintPlugin = process.env.DISABLE_ESLINT_PLUGIN === 'true';

Expand Down

0 comments on commit b66c8df

Please sign in to comment.