-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (49 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const path = require('path')
const _ = require('lodash')
const merge = require('webpack-merge')
class WebpackEnvy {
constructor (options) {
this.defaults = {
path: './config',
filename: 'webpack.[env].js',
commonEnvId: 'common',
env: null,
root: process.cwd(),
verbose: false
}
this.settings = _.merge({}, this.defaults, options)
this.configDir = path.resolve(this.settings.root, this.settings.path)
this.placeholder = '[env]'
}
getConfig () {
return (env) => {
if (!this.settings.env) {
this.settings.env = env
}
if (_.isString(this.settings.env)) {
this.settings.env = {
id: this.settings.env
}
}
if (_.isPlainObject(this.settings.env) && !this.settings.env.id) {
this.settings.env.id = process.env.NODE_ENV || 'development'
}
if (!_.isPlainObject(this.settings.env)) {
throw new Error('webpack-envy: Please, provide a valid "env" option.')
}
if (this.settings.verbose) {
console.log('webpack-envy: running for ' + this.settings.env.id + ' environment.')
}
const commonConfigFile = path.resolve(this.configDir, this.settings.filename.replace(this.placeholder, this.settings.commonEnvId))
const envConfigFile = path.resolve(this.configDir, this.settings.filename.replace(this.placeholder, this.settings.env.id))
this.commonConfig = require(commonConfigFile)
this.envConfig = require(envConfigFile)
this.config = merge(
this.commonConfig(this.settings),
this.envConfig(this.settings)
)
return this.config
}
}
}
module.exports = WebpackEnvy