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
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
remove behavior changes
  • Loading branch information
beeequeue committed Mar 4, 2021
commit 5378c4e31cf14f554214980d037b4575225f0324
23 changes: 4 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const interpolate = (env, vars) => {
return env
}

const isMainThreadElectron = (target) =>
target.startsWith('electron') && target.endsWith('main')

class Dotenv {
/**
* The dotenv-webpack plugin.
Expand All @@ -35,14 +32,8 @@ class Dotenv {
}, config)

this.checkDeprecation()
}

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

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

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

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

// 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') && !isMainThreadElectron(target)) {
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
formatted['process.env'] = '"MISSING_ENV_VAR"'
}
// fix in case of missing
formatted['process.env'] = '{}'

return formatted
}
Expand Down
45 changes: 0 additions & 45 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,49 +456,4 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
)
})
})

describe('process.env stubbing', () => {
const expectToBeStubbed = (result) => {
expect(result).toMatch('const TEST = "testing"')
expect(result).toMatch('const TEST2 = "MISSING_ENV_VAR".TEST2')
expect(result).toMatch('const NODE_ENV = "development"')
expect(result).toMatch('const MONGOLAB_USER = "MISSING_ENV_VAR".MONGOLAB_USER')
}

const expectNotToBeStubbed = (result) => {
expect(result).toMatch('const TEST = "testing"')
expect(result).toMatch('const TEST2 = process.env.TEST2')
expect(result).toMatch('const NODE_ENV = "development"')
expect(result).toMatch('const MONGOLAB_USER = process.env.MONGOLAB_USER')
}

const plugin = new DotenvPlugin({ path: envSimple })
const cases = [
['web', true],
['es5', true],
['es2020', true],
['electron-renderer', true],
['electron9-renderer', true],
['electron-preload', true],
['node', false],
['node14', false],
['electron-main', false],
['electron9-main', false]
]

test.each(cases)('%s', (target, shouldStub, done) => {
compile(
getConfig(target, plugin),
(result) => {
if (shouldStub) {
expectToBeStubbed(result)
} else {
expectNotToBeStubbed(result)
}

done()
}
)
})
})
})