Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite tests to use webpack compiler, drop node 8 #360

Merged
merged 25 commits into from
Mar 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: only stub process.env if not targeting node
  • Loading branch information
beeequeue committed Mar 4, 2021
commit 6b7fa9dcf580ee81734b18f155011242d07e3a51
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ class Dotenv {
}, config)

this.checkDeprecation()
}

apply (compiler) {
const target = compiler.options.target ?? 'web'
const variables = this.gatherVariables()
const data = this.formatData(variables, target)

return new DefinePlugin(this.formatData(this.gatherVariables()))
new DefinePlugin(data).apply(compiler)
}

checkDeprecation () {
Expand Down Expand Up @@ -121,7 +127,7 @@ class Dotenv {
return ''
}

formatData (vars = {}) {
formatData (vars = {}, target) {
const { expand } = this.config
const formatted = Object.keys(vars).reduce((obj, key) => {
const v = vars[key]
Expand All @@ -144,8 +150,14 @@ class Dotenv {
return obj
}, {})

// fix in case of missing
formatted['process.env'] = '{}'
// We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
// 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
if (!target.startsWith('node') && !target.startsWith('electron')) {
beeequeue marked this conversation as resolved.
Show resolved Hide resolved
// fix in case of missing
formatted['process.env'] = '{}'
}

return formatted
}
Expand Down