forked from rip-tyang/site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
146 lines (113 loc) · 3.77 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
require 'coffee-script/register'
fs = require 'fs'
path = require 'path'
gulp = require 'gulp'
$ = require('gulp-load-plugins')()
del = require 'del'
runSequence = require 'run-sequence'
webpack = require 'webpack'
WebpackDevServer = require 'webpack-dev-server'
webpackConfig = require './webpack.config'
webpackStream = require 'webpack-stream'
http = require 'http'
paths =
src: './src'
dist: './dist'
entry = fs.readdirSync path.join(paths.src, 'coffee')
.filter (e) -> /\.coffee$/.test e
.filter (e) -> !(/^_/.test(e))
.map (e) -> e[0..-8]
port = 5000
############################################
# dev
############################################
gulp.task 'pug', ->
# uncache data.coffee from require cache
delete require.cache[require.resolve("#{paths.src}/pug/data")]
gulp.src("#{paths.src}/pug/[^_]*.pug")
.pipe $.plumber()
.pipe $.pug
locals: require("#{paths.src}/pug/data")
pretty: true
.pipe gulp.dest(paths.dist)
gulp.task 'copy:assets', ->
gulp.src("#{paths.src}/assets/**/*", { base: paths.src })
.pipe gulp.dest(paths.dist)
gulp.task 'copy:miscellaneous', ->
gulp.src("#{paths.src}/miscellaneous/**/*",
{ base: "#{paths.src}/miscellaneous" })
.pipe gulp.dest(paths.dist)
gulp.task 'copy', ['copy:assets', 'copy:miscellaneous']
gulp.task 'clean', del.bind(null, [paths.dist])
gulp.task 'server', ->
config =
contentBase: paths.dist
proxy: require './proxy'
stats:
colors: true
timings: true
assets: true
hash: true
chunks: false
option =
build: 'debug'
port: port
entry: entry
webpack_dev_runner = webpack webpackConfig(option)
wds = new WebpackDevServer(webpack_dev_runner, config)
# reload browser when changes detected
wds.app.get '/reload', (req, res) ->
wds.sockWrite wds.sockets, 'ok'
res.sendStatus 200
wds.listen 5000, 'localhost', (err) ->
throw new $.util.PluginError('webpack-dev-server', err) if err
reload = ->
http.get "http://localhost:#{port}/reload", ->
$.util.log 'Reloading...'
gulp.watch ['src/pug/**/*'], ['pug', reload]
gulp.watch ['src/pug/data.coffee'], ['pug', reload]
gulp.watch ['src/assets/**/*'], ['copy:assets', reload]
gulp.watch ['src/miscellaneous/**'], ['copy:miscellaneous', reload]
gulp.task 'build', ->
runSequence 'clean', ['pug', 'copy', 'webpack']
gulp.task 'serve', ->
runSequence 'clean', ['pug', 'copy', 'server']
############################################
# test
############################################
gulp.task 'test', ->
gulp.src('src/coffee/test/*', { read: false })
.pipe $.mocha({ reporter: 'nyan' })
############################################
# dist
############################################
gulp.task 'dist:webpack', ->
option =
build: 'production'
port: port
entry: entry
gulp.src "#{paths.src}/coffee/main.js"
.pipe webpackStream(webpackConfig(option))
.pipe gulp.dest(paths.dist)
gulp.task 'dist:pug', ->
pug_data = require "#{paths.src}/pug/data"
manifest_data = require "#{paths.dist}/manifest.json"
pug_data.manifest = {}
pug_data.manifest[key] = v for key, v of manifest_data
gulp.src("#{paths.src}/pug/[^_]*.pug")
.pipe $.plumber()
.pipe $.pug
locals: pug_data
.pipe gulp.dest(paths.dist)
gulp.task 'dist:copy:assets', ->
gulp.src("#{paths.src}/assets/**/*", { base: paths.src })
.pipe gulp.dest(paths.dist)
gulp.task 'dist:copy:miscellaneous', ->
gulp.src("#{paths.src}/miscellaneous/**/*",
{ base: "#{paths.src}/miscellaneous" })
.pipe gulp.dest(paths.dist)
gulp.task 'dist:copy', ['dist:copy:assets', 'dist:copy:miscellaneous']
gulp.task 'dist:build', ->
runSequence 'test', 'clean', ['dist:webpack', 'dist:copy'], 'dist:pug'
gulp.task 'dist', ['dist:build']
gulp.task 'default', ['dist:build']