Skip to content

Commit

Permalink
Fix invalid local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ponjs committed Dec 11, 2021
1 parent 597615e commit 458b215
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@ module.exports = {
{
plugin: CracoEnvPlugin,
options: {
path: __dirname,
vars: {},
},
},
],
variables: {}
}
}
]
}
```

## Configuration

You can pass an `options` object to configure the plugin.

- `options.path`
- _Default:_ root
- The directory from which `.env` files are loaded. Can be an absolute path, or a path relative to the project root.
- `options.vars`
- `options.variables`
- _Default_: `{}`
- Custom environment variables. For more detailed env parsing rules, please refer to [the documentation of `dotenv`](https://github.com/motdotla/dotenv).
- Custom additional environment variables. For more detailed env parsing rules, please refer to [the documentation of `dotenv`](https://github.com/motdotla/dotenv).

## Mode

Expand Down
53 changes: 26 additions & 27 deletions index.js
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
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "craco-plugin-env",
"description": "An environment plugin for craco",
"version": "1.0.0",
"version": "1.0.1",
"main": "index.js",
"repository": "https://github.com/ponjs/craco-plugin-env.git",
"keywords": [
Expand All @@ -15,7 +15,12 @@
"url": "https://github.com/ponjs/craco-plugin-env/issues"
},
"homepage": "https://github.com/ponjs/craco-plugin-env#readme",
"peerDependencies": {
"@craco/craco": "^6.0.0",
"react-scripts": "^4.0.0"
},
"dependencies": {
"dotenv": "^10.0.0"
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0"
}
}

0 comments on commit 458b215

Please sign in to comment.