-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
132 lines (119 loc) · 3.7 KB
/
webpack.config.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var webpack = require('webpack');
var path = require('path');
var assign = require('object-assign');
var nodeExternals = require('webpack-node-externals');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
module.exports = function getConfig(options) {
var options = options || {};
var isProd = (options.BUILD_ENV || process.env.BUILD_ENV) === 'PROD';
var isWeb = (options.TARGET_ENV || process.env.TARGET_ENV) === 'WEB';
var isTest = (options.TARGET_ENV || process.env.TARGET_ENV) === 'TEST';
// get library details from JSON config
var libraryDesc = require('./package.json').library;
var libraryName = libraryDesc.name;
// determine output file name
var outputName = buildLibraryOutputName(libraryDesc, isWeb, isProd);
var outputFolder = isWeb ? 'dist' : 'lib';
// get base config
var config;
// for the web
if(isWeb){
config = assign(getBaseConfig(isProd), {
output: {
path: path.join(__dirname, outputFolder),
filename: outputName,
library: 'geoOnFire',
libraryTarget: 'umd',
umdNamedDefine: true
}
});
} else if (isTest) {
// test build - needed to run test bundle in a headless browser
config = {
entry: ['babel-polyfill', path.join(__dirname, './test/main.spec.js')],
module: {
loaders: [
{test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: "babel-loader"},
]
},
resolve: {
root: [path.resolve('./src'), 'node_modules/'],
extensions: ['', '.js'],
},
output: {
path: path.join(__dirname, 'test_dist'),
filename: 'test.bundle.js',
libraryTarget: 'umd',
},
};
}
// for the backend
else {
config = assign(getBaseConfig(isProd), {
output: {
path: path.join(__dirname, outputFolder),
filename: outputName,
library: libraryName,
libraryTarget: 'commonjs2'
},
target: 'node',
node: {
__dirname: true,
__filename: true
},
externals: [nodeExternals()]
});
}
//config.plugins.push(new CleanWebpackPlugin([outputFolder]));
return config;
}
/**
* Build base config
* @param {Boolean} isProd [description]
* @return {[type]} [description]
*/
function getBaseConfig(isProd) {
// get library details from JSON config
var libraryDesc = require('./package.json').library;
var libraryEntryPoint = path.join('src', libraryDesc.entry);
// generate webpack base config
return {
entry: path.join(__dirname, libraryEntryPoint),
output: {
// ommitted - will be filled according to target env
},
module: {
preLoaders: [
{test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: "eslint-loader"}
],
loaders: [
{test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: "babel-loader"},
]
},
eslint: {
configFile: './.eslintrc'
},
resolve: {
root: path.resolve('./src'),
extensions: ['', '.js']
},
devtool: isProd ? null : '#eval-source-map',
debug: !isProd,
plugins: isProd ? [
new webpack.DefinePlugin({'process.env': {'NODE_ENV': '"production"'}}),
new UglifyJsPlugin({ minimize: true })
// Prod plugins here
] : [
new webpack.DefinePlugin({'process.env': {'NODE_ENV': '"development"'}})
// Dev plugins here
]
};
}
function buildLibraryOutputName(libraryDesc, isWeb, isProd){
if(isWeb){
return libraryDesc["dist-web"] || [libraryDesc.name, 'web', (isProd ? 'min.js' : 'js')].join('.');
} else {
return libraryDesc["dist-node"] || [libraryDesc.name, (isProd ? 'min.js' : 'js')].join('.');
}
}