-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGulpfile.coffee
263 lines (239 loc) · 6.98 KB
/
Gulpfile.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
fs = require 'fs'
del = require 'del'
_defaults = require 'lodash/defaults'
_defaultsDeep = require 'lodash/defaultsDeep'
_map = require 'lodash/map'
_mapValues = require 'lodash/mapValues'
log = require 'loga'
gulp = require 'gulp'
gutil = require 'gulp-util'
webpack = require 'webpack'
autoprefixer = require 'autoprefixer'
manifest = require 'gulp-manifest'
spawn = require('child_process').spawn
coffeelint = require 'gulp-coffeelint'
webpackStream = require 'webpack-stream'
gulpSequence = require 'gulp-sequence'
WebpackDevServer = require 'webpack-dev-server'
HandleCSSLoader = require 'webpack-handle-css-loader'
UglifyJSPlugin = require 'uglifyjs-webpack-plugin'
MiniCssExtractPlugin = require 'mini-css-extract-plugin'
Visualizer = require('webpack-visualizer-plugin')
BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
s3Upload = require 'gulp-s3-upload'
argv = require('yargs').argv
config = require './src/config'
Language = require './src/lang'
paths = require './gulp_paths'
webpackBase =
mode: 'development'
module:
exprContextRegExp: /$^/
exprContextCritical: false
resolve:
extensions: ['.coffee', '.js', '.json']
output:
filename: 'bundle.js'
publicPath: '/'
s3 = s3Upload {
accessKeyId: process.env.RADIOACTIVE_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.RADIOACTIVE_AWS_SECRET_ACCESS_KEY
}
gulp.task 'dev', ['dev:webpack-server', 'watch:dev:server']
# TODO: 'dist:manifest' - appcache
gulp.task 'dist', gulpSequence(
'dist:clean'
['dist:scripts', 'dist:static']
'dist:concat'
'dist:s3'
)
gulp.task 'watch', ->
gulp.watch paths.coffee, ['dev:server']
gulp.task 'watch:dev:server', ['dev:server'], ->
gulp.watch paths.coffee, ['dev:server']
gulp.task 'lint', ->
gulp.src paths.coffee
.pipe coffeelint()
.pipe coffeelint.reporter()
gulp.task 'dev:server', ['build:static:dev'], do ->
devServer = null
process.on 'exit', -> devServer?.kill()
->
devServer?.kill()
devServer = spawn 'coffee', ['bin/dev_server.coffee'], {stdio: 'inherit'}
devServer.on 'close', (code) ->
if code is 8
gulp.log 'Error detected, waiting for changes'
gulp.task 'dev:webpack-server', ->
entries = [
"webpack-dev-server/client?#{config.WEBPACK_DEV_URL}"
'webpack/hot/dev-server'
paths.root
]
handleLoader = new HandleCSSLoader {
minimize: false,
extract: false,
sourceMap: false,
cssModules: false
postcss: [
autoprefixer {
browsers: ['> 3% in US', 'last 2 firefox versions']
}
]
}
compiler = webpack _defaultsDeep {
devtool: 'inline-source-map'
entry: entries
output:
path: __dirname
publicPath: "#{config.WEBPACK_DEV_URL}/"
module:
rules: [
{test: /\.coffee$/, loader: 'coffee-loader'}
handleLoader.css()
handleLoader.styl()
]
plugins: [
new webpack.HotModuleReplacementPlugin()
# new webpack.IgnorePlugin /\.json$/, /lang/
new webpack.DefinePlugin
'process.env': _mapValues process.env, (val) -> JSON.stringify val
]
}, webpackBase
webpackOptions =
publicPath: "#{config.WEBPACK_DEV_URL}/"
hot: true
headers: 'Access-Control-Allow-Origin': '*'
noInfo: true
disableHostCheck: true
if config.DEV_USE_HTTPS
console.log 'using https'
webpackOptions.https = true
webpackOptions.key = fs.readFileSync './bin/fam-dev.key'
webpackOptions.cert = fs.readFileSync './bin/fam-dev.crt'
new WebpackDevServer compiler, webpackOptions
.listen config.WEBPACK_DEV_PORT, (err) ->
if err
log.error err
else
log.info
event: 'webpack_server_start'
message: "Webpack listening on port #{config.WEBPACK_DEV_PORT}"
gulp.task 'build:static:dev', ->
gulp.src paths.static
.pipe gulp.dest paths.build
gulp.task 'dist:clean', (cb) ->
del paths.dist + '/*', cb
gulp.task 'dist:static', ['dist:clean'], ->
gulp.src paths.static
.pipe gulp.dest paths.dist
gulp.task 'dist:sw', ->
gulp.src paths.sw
.pipe webpackStream(_defaultsDeep({
mode: 'production'
optimization: {
minimizer: [
new UglifyJSPlugin {
sourceMap: false
uglifyOptions:
mangle:
reserved: ['process']
}
]
}
module:
rules: [
{test: /\.coffee$/, loader: 'coffee-loader'}
]
output:
filename: 'service_worker.js'
plugins: []
resolve:
extensions: ['.coffee', '.js', '.json']
}, webpackBase), require('webpack'))
.pipe gulp.dest paths.dist
gulp.task 'dist:scripts', ['dist:clean', 'dist:sw'], ->
handleLoader = new HandleCSSLoader {
minimize: true,
extract: true,
sourceMap: false,
cssModules: false
postcss: [
autoprefixer {
browsers: ['> 3% in US', 'last 2 firefox versions']
}
]
}
_map config.LANGUAGES, (language) ->
fs.writeFileSync(
"#{__dirname}/#{paths.dist}/lang_#{language}.json"
Language.getJsonString language
)
scriptsConfig = _defaultsDeep {
mode: 'production'
optimization: {
minimizer: [
new UglifyJSPlugin {
sourceMap: false
uglifyOptions:
mangle:
reserved: ['process']
}
]
}
plugins: [
new webpack.IgnorePlugin /\.json$/, /lang/
new MiniCssExtractPlugin {
filename: "bundle.css"
}
]
output:
# TODO: '[hash].bundle.js' if we have caching issues, or use appcache
filename: 'bundle.js'
module:
rules: [
{test: /\.coffee$/, loader: 'coffee-loader'}
handleLoader.css()
handleLoader.styl()
]
}, webpackBase
gulp.src paths.root
.pipe webpackStream scriptsConfig, require('webpack'), (err, stats) ->
if err
gutil.log err
return
statsJson = JSON.stringify {hash: stats.toJson().hash, time: Date.now()}
fs.writeFileSync "#{__dirname}/#{paths.dist}/stats.json", statsJson
.pipe gulp.dest paths.dist
gulp.task 'dist:concat', ->
bundle = fs.readFileSync "#{__dirname}/#{paths.dist}/bundle.js", 'utf-8'
matches = bundle.match(/process\.env\.[a-zA-Z0-9_]+/g)
_map matches, (match) ->
key = match.replace('process.env.', '')
bundle = bundle.replace match, "'#{process.env[key]}'"
stats = JSON.parse fs.readFileSync "#{__dirname}/#{paths.dist}/stats.json"
_map config.LANGUAGES, (language) ->
lang = fs.readFileSync(
"#{__dirname}/#{paths.dist}/lang_#{language}.json", 'utf-8'
)
fs.writeFileSync(
"#{__dirname}/#{paths.dist}/bundle_#{stats.hash}_#{language}.js"
lang + bundle
, 'utf-8')
gulp.task 'dist:s3', ->
gulp.src("#{__dirname}/#{paths.dist}/bundle*")
.pipe s3 {
Bucket: 'cdn.wtf'
ACL: 'public-read'
keyTransform: (relativeFilename) ->
"d/scripts/fam/#{relativeFilename}"
}
gulp.task 'dist:manifest', ['dist:static', 'dist:scripts'], ->
gulp.src paths.manifest
.pipe manifest {
hash: true
timestamp: false
preferOnline: true
fallback: ['/ /offline.html']
}
.pipe gulp.dest paths.dist