-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
111 lines (97 loc) · 2.95 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
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');
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';
// 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' : './';
// get base config
var config;
// for the web
if(isWeb){
config = assign(getBaseConfig(isProd), {
output: {
path: path.join(__dirname, outputFolder),
filename: outputName,
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true
}
});
}
// 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"'}}),
// 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('.');
}
}