-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.client.coffee
160 lines (128 loc) · 3.56 KB
/
webpack.client.coffee
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Webpack plugins
HtmlWebpackPlugin = require 'html-webpack-plugin'
ExtractTextPlugin = require "extract-text-webpack-plugin"
{ HotModuleReplacementPlugin } = require "webpack"
{ optimize } = require "webpack"
{ NoErrorsPlugin } = require "webpack"
# Stylus plugins
nib = require "nib"
rupture = require "rupture"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Options
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# hot: yes|no
# devServer: yes|no
# hashed: yes|no
# devtool: yes|no
# separateStyles: yes|no
# separateVendor: yes|no
# optiImgs: yes|no
# minimize: yes|no
# publicPath: {string}
# noErrors: yes|no
module.exports = (options) ->
options = options or {}
# ~~~~~~~ Entry ~~~~~~~~~
# Add "webpack/hot/dev-server" if options.hot
entry =
app:
if options.hot then ["./client.coffee", "webpack/hot/dev-server"]
else "./client.coffee"
if options.separateVendor
entry.vendor = [
"react"
"react-router"
"jquery"
]
# ~~~~~~ Output ~~~~~~~~~
output =
path: __dirname + "/__build__/public/"
publicPath:
if options.publicPath then options.publicPath
else
"/static/"
filename:
if options.hashed then "app-[hash].js"
else "app.js"
# ~~~~~~~~ Resolve ~~~~~~~~
resolve =
moduleDirectories: ['node_modules']
extensions: ['', '.webpack.js', '.web.js', '.js', '.coffee']
# ~~~~~~ devtool ~~~~~~~~
devtool = if options.devtool then "#cheap-module-eval-source-map"
# ~~~~~~ Loaders ~~~~~~~~
common_loaders = [
test: /\.(ttf|eot|svg|woff|otf)$/,
loader: 'file-loader'
,
test: /\.woff2?$/,
loader: 'url-loader?limit=10000&minetype=application/font-woff'
]
_imgLoader = [
test: /\.(jpe?g|png|gif)$/
loader:
if options.optiImgs
"file!image-webpack?optimizationLevel=7&interlaced=false"
else 'file'
]
_coffeeLoader = [
test: /\.coffee$/
loader:
if options.hot then "react-hot!coffee!cjsx"
else "coffee!cjsx"
exclude: "node_modules"
]
_styleLoaders = [
test: /\.styl$/,
loader: 'style!css!stylus'
id: 1
,
test: /\.css$/,
loader: 'style!css'
id: 2
]
if options.separateStyles
for style in _styleLoaders
style.loader = ExtractTextPlugin.extract "style",
style.loader.split('style!')[1],
id: style.id
loaders = common_loaders.concat(_coffeeLoader)
.concat(_styleLoaders)
.concat(_imgLoader)
# ~~~~~~ Stylus ~~~~~~
stylus =
use: [
nib()
rupture()
]
# ~~~~~~ Plugins ~~~~~~
plugins = []
_configHtmlPlugin =
hash: if options.hashed then yes else no
hot_reload: if options.hot then yes else no
template: './views/base.html'
filename: '../../__dist__/base.html'
inject: 'body'
plugins.push new HotModuleReplacementPlugin if options.hot
if options.separateStyles
plugins.push(
new ExtractTextPlugin 2, "vendors-[hash].css"
new ExtractTextPlugin 1, "styles-[hash].css"
)
if options.separateVendor
_filename = if options.hashed then 'vendor-[hash].js' else 'vendor.js'
plugins.push new optimize.CommonsChunkPlugin
name: 'vendor'
filename: _filename
plugins.push new HtmlWebpackPlugin _configHtmlPlugin
plugins.push new optimize.UglifyJsPlugin() if options.minimize
plugins.push new NoErrorsPlugin() if options.noErrors
# ~~~~~~ Config returned ~~~~~~~
entry: entry
output: output
resolve: resolve
devtool: devtool
stylus: stylus
module:
loaders: loaders
plugins: plugins