-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGulpfile.coffee
142 lines (128 loc) · 3.72 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
fs = require 'fs'
del = require 'del'
_ = require 'lodash'
log = require 'loga'
gulp = require 'gulp'
webpack = require 'webpack'
mocha = require 'gulp-mocha'
spawn = require('child_process').spawn
autoprefixer = require 'autoprefixer'
coffeelint = require 'gulp-coffeelint'
webpackStream = require 'webpack-stream'
WebpackDevServer = require 'webpack-dev-server'
ExtractTextPlugin = require 'extract-text-webpack-plugin'
config = require './src/config'
paths =
static: './src/static/**/*'
coffee: ['./*.coffee', './src/**/*.coffee']
unitTests: ['./src/**/test.coffee', './src/**/*.test.coffee']
root: './src/root.coffee'
dist: './dist'
build: './build'
webpackBase =
module:
exprContextRegExp: /$^/
exprContextCritical: false
resolve:
extensions: ['.coffee', '.js', '.json', '']
output:
filename: 'bundle.js'
publicPath: '/'
postcss: -> [autoprefixer({})]
gulp.task 'dev', ['dev:webpack-server', 'dev:server']
gulp.task 'test', ['test:lint', 'test:unit']
gulp.task 'dist', ['dist:scripts', 'dist:static']
gulp.task 'watch', -> gulp.watch paths.coffee, ['test']
gulp.task 'test:lint', ->
gulp.src paths.coffee
.pipe coffeelint()
.pipe coffeelint.reporter()
gulp.task 'test:unit', ->
gulp.src paths.unitTests
.pipe mocha()
gulp.task 'dev:static', ->
gulp.src paths.static
.pipe gulp.dest paths.build
gulp.task 'dev:server', ['dev:static'], do ->
devServer = null
process.on 'exit', -> devServer?.kill()
->
devServer?.kill()
devServer = spawn 'coffee', ['src/server/start.coffee'], {stdio: 'inherit'}
gulp.task 'dev:webpack-server', ->
compiler = webpack _.defaultsDeep {
devtool: 'inline-source-map'
entry: [
"webpack-dev-server/client?#{config.WEBPACK_DEV_URL}"
'webpack/hot/dev-server'
paths.root
]
output:
path: __dirname
publicPath: "#{config.WEBPACK_DEV_URL}/"
module:
loaders: [
{test: /\.coffee$/, loader: 'coffee'}
{test: /\.json$/, loader: 'json'}
{test: /\.styl$/, loaders: [
'style-loader'
'css-loader',
'postcss-loader',
'stylus-loader?paths[]=node_modules'
]}
]
plugins: [
new webpack.HotModuleReplacementPlugin()
new webpack.DefinePlugin
'process.env': _.mapValues process.env, (val) -> JSON.stringify val
]
}, webpackBase
new WebpackDevServer compiler,
publicPath: "#{config.WEBPACK_DEV_URL}/"
hot: true
noInfo: true
.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 '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:scripts', ['dist:clean'], ->
scriptsConfig = _.defaultsDeep {
devtool: 'source-map'
plugins: [
new webpack.optimize.UglifyJsPlugin
mangle:
except: ['process']
new ExtractTextPlugin 'bundle.css'
]
output:
filename: '[hash].bundle.js'
module:
loaders: [
{test: /\.coffee$/, loader: 'coffee'}
{test: /\.json$/, loader: 'json'}
{
test: /\.styl$/
loader: ExtractTextPlugin.extract [
'css-loader',
'postcss-loader',
'stylus-loader?paths[]=node_modules'
]
}
]
}, webpackBase
gulp.src paths.root
.pipe webpackStream scriptsConfig, null, (err, stats) ->
if err
console.error err
return
statsJson = JSON.stringify {hash: stats.toJson().hash}
fs.writeFileSync "#{__dirname}/#{paths.dist}/stats.json", statsJson
.pipe gulp.dest paths.dist