-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
const path = require('path') | ||
const dotenv = require('dotenv') | ||
const dotenvExpand = require('dotenv-expand') | ||
const webpack = require('webpack') | ||
|
||
function getModeName() { | ||
const index = process.argv.indexOf('--mode') | ||
return process.argv[index + 1] || '' | ||
} | ||
|
||
function loadEnvironment(envPath) { | ||
try { | ||
const env = dotenv.config({ path: envPath, debug: process.env.DEBUG }) | ||
dotenvExpand(env) | ||
} catch (err) { | ||
// only ignore error if file is not found | ||
if (err.toString().indexOf('ENOENT') < 0) { | ||
throw new Error(err.toString()) | ||
} | ||
} | ||
} | ||
|
||
function overrideCracoConfig({ cracoConfig, pluginOptions = {} }) { | ||
const mode = getModeName() | ||
|
||
const dir = pluginOptions.path || process.cwd() | ||
|
||
const basePath = path.resolve(dir, `.env${mode ? `.${mode}` : ''}`) | ||
const basePath = path.resolve(process.cwd(), `.env${mode ? `.${mode}` : ''}`) | ||
const localPath = `${basePath}.local` | ||
|
||
const load = envPath => { | ||
try { | ||
const env = dotenv.config({ path: envPath, debug: process.env.DEBUG }) | ||
return env.parsed | ||
} catch (err) { | ||
// only ignore error if file is not found | ||
if (err.toString().indexOf('ENOENT') < 0) { | ||
throw new Error(err.toString()) | ||
} | ||
} | ||
} | ||
loadEnvironment(localPath) | ||
loadEnvironment(basePath) | ||
|
||
const plugin = new webpack.EnvironmentPlugin({ | ||
...pluginOptions.vars, | ||
...load(basePath), | ||
...load(localPath), | ||
}) | ||
const plugin = new webpack.EnvironmentPlugin(pluginOptions.variables) | ||
|
||
const originalPlugins = cracoConfig.webpack?.plugins | ||
|
||
if (originalPlugins) { | ||
if ('add' in originalPlugins) { | ||
originalPlugins.add.push(plugin) | ||
if (cracoConfig.webpack) { | ||
if (cracoConfig.webpack.plugins) { | ||
const webpackPlugins = cracoConfig.webpack.plugins | ||
;(webpackPlugins.add || webpackPlugins).push(plugin) | ||
} else { | ||
originalPlugins.push(plugin) | ||
cracoConfig.webpack.plugins = [plugin] | ||
} | ||
} else { | ||
cracoConfig.webpack.plugins = { add: [plugin] } | ||
cracoConfig.webpack = { | ||
plugins: [plugin] | ||
} | ||
} | ||
|
||
return cracoConfig | ||
} | ||
|
||
module.exports = { | ||
overrideCracoConfig, | ||
overrideCracoConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters