forked from Lobos/react-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-webpack-config.js
127 lines (112 loc) · 3.75 KB
/
make-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
var path = require("path")
var webpack = require("webpack")
var ExtractTextPlugin = require("extract-text-webpack-plugin")
function extsToRegExp(exts) {
return new RegExp("\\.(" + exts.map(function(ext) {
return ext.replace(/\./g, "\\.")
}).join("|") + ")(\\?.*)?$")
}
function loadersByExtension(obj) {
var loaders = []
Object.keys(obj).forEach(function(key) {
var exts = key.split("|")
var value = obj[key]
var entry = {
extensions: exts,
test: extsToRegExp(exts)
}
if(Array.isArray(value)) {
entry.loaders = value
} else if(typeof value === "string") {
entry.loader = value
} else {
Object.keys(value).forEach(function(valueKey) {
entry[valueKey] = value[valueKey]
})
}
loaders.push(entry)
})
return loaders
}
module.exports = function(options) {
var minimize = options.minimize || process.argv.indexOf('--min') > 0
var entry = options.entry
var output = {
path: options.path || "./",
filename: options.filename || "./js/[name].js",
//chunkFilename: (options.devServer ? "[id].js" : "[name].js") + (options.longTermCaching ? "?[chunkhash]" : ""),
sourceMapFilename: "./js/[file].map",
pathinfo: options.debug,
library: options.library,
libraryTarget: options.libraryTarget || "umd"
}
// loaders ===========================================================================
var cssLoader = minimize ? "css-loader?localIdentName=[hash:base64:8]" : "css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]",
lessLoader = cssLoader + '!less-loader'
var loaders = loadersByExtension({
"jsx": options.hotComponents ? "babel" : "babel-loader?stage=0",
"js": options.hotComponents ? "babel" : "babel-loader?stage=0",
"json": "file-loader?name=./json/[name].json",
//"txt": "raw-loader",
"png|jpg|jpeg|gif": "url-loader?limit=10000&name=./images/[name].[ext]",
"ttf|eot|woff|woff2|otf|svg": "file-loader?name=./font/[name].[ext]",
//"wav|mp3": "file-loader",
//"html": "html-loader",
//"md|markdown": ["html-loader", "markdown-loader"]
"css": options.separateStylesheet ? ExtractTextPlugin.extract("style-loader", cssLoader, { publicPath: '.' }) : "style-loader!" + cssLoader,
"less": options.separateStylesheet ? ExtractTextPlugin.extract("style-loader", lessLoader, { publicPath: '.' }) : "style-loader!" + lessLoader
})
loaders.push({ test: /(\.js|\.jsx)$/, loader: 'eslint-loader', exclude: /(node_modules|\.css$|\.less$)/ })
// externals ========================================================================
var externals = []
if (options.externals) {
Object.keys(options.externals).forEach(function (key) {
var val = options.externals[key]
var ext = {}
ext[key] = {root:val,commonjs2:key,commonjs:key,amd:key}
externals.push(ext)
})
}
// plugins ==========================================================================
var plugins = [
new webpack.PrefetchPlugin("react"),
new webpack.PrefetchPlugin("react/lib/ReactComponentBrowserEnvironment")
]
if(options.separateStylesheet) {
plugins.push(new ExtractTextPlugin("[name].css"))
}
if(minimize) {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
})
)
plugins.push(new webpack.optimize.OccurenceOrderPlugin())
plugins.push(new webpack.NoErrorsPlugin())
plugins.push(new webpack.optimize.DedupePlugin())
plugins.push(
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
})
)
}
if (options.plugins) {
plugins = plugins.concat(options.plugins)
}
return {
entry: entry,
output: output,
target: options.target || "web",
module: {
loaders: loaders
},
devtool: options.devtool,
debug: options.debug,
externals: externals,
plugins: plugins
}
}