Skip to content

Commit

Permalink
feat(webpack): include AEGIR_ prefixed env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 8, 2017
1 parent f87d6d3 commit a4adb59
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/config/webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const merge = require('webpack-merge')
const webpack = require('webpack')

const utils = require('../../utils')
const base = require('./base')
Expand All @@ -21,7 +22,10 @@ function webpackConfig () {
filename: entry.split('/').pop(),
library: libraryName,
path: utils.getPathToDist()
}
},
plugins: [
new webpack.DefinePlugin(utils.getEnv('production').stringified)
]
}, userConfig)
})
}
Expand Down
31 changes: 31 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,34 @@ exports.getListrConfig = () => {
renderer: isCI ? VerboseRenderer : UpdateRenderer
}
}

/**
* Get current env variables for inclusion.
*
* @param {string} [env='development']
*
* @returns {Object}
*/
exports.getEnv = (env) => {
const PREFIX = /^AEGIR_/i
const raw = Object.keys(process.env)
.filter((key) => PREFIX.test(key))
.reduce((env, key) => {
env[key] = process.env[key]
return env
}, {
NODE_ENV: process.env.NODE_ENV || env || 'development'
})

const stringifed = {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key])
return env
}, {})
}

return {
raw: raw,
stringified: stringifed
}
}
10 changes: 9 additions & 1 deletion test/config/__snapshots__/webpack.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ Object {
"performance": Object {
"hints": false,
},
"plugins": Array [],
"plugins": Array [
DefinePlugin {
"definitions": Object {
"process.env": Object {
"NODE_ENV": "production",
},
},
},
],
"resolve": Object {
"alias": Object {
"http": "stream-http",
Expand Down
9 changes: 9 additions & 0 deletions test/config/webpack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ describe('config - webpack', () => {
},
getPathToNodeModules () {
return 'aegir/node_modules'
},
getEnv () {
return {
stringified: {
'process.env': {
NODE_ENV: 'production'
}
}
}
}
}))

Expand Down
20 changes: 20 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@ describe('utils', () => {
it('getPathToNodeModules', () => {
expect(utils.getPathToNodeModules()).toEqual(expect.stringMatching(/node_modules$/))
})

it('getEnv', () => {
process.env.AEGIR_TEST = 'hello'

const env = utils.getEnv()
expect(env.raw).toEqual({
NODE_ENV: 'test',
AEGIR_TEST: 'hello'
})
expect(env.stringified).toEqual({
'process.env': {
NODE_ENV: '"test"',
AEGIR_TEST: '"hello"'
}
})

process.env.NODE_ENV = ''
expect(utils.getEnv('production').raw).toHaveProperty('NODE_ENV', 'production')
process.env.NODE_ENV = 'test'
})
})

0 comments on commit a4adb59

Please # to comment.