-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgulpfile.babel.js
102 lines (91 loc) · 2.89 KB
/
gulpfile.babel.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
import 'babel-polyfill'
import fs from 'fs'
import gulp from 'gulp'
import merge from 'merge-stream'
import gutil from 'gulp-util'
import pug from 'gulp-pug'
import rename from 'gulp-rename'
import webpack from 'webpack'
import WebpackDevServer from 'webpack-dev-server'
import prodConfig from './webpack/prod.config'
import devConfig from './webpack/dev.config'
const port = 3000
/*
* common tasks
*/
gulp.task('replace-webpack-code', () => {
const replaceTasks = [ {
from: './webpack/replace/JsonpMainTemplate.runtime.js',
to: './node_modules/webpack/lib/JsonpMainTemplate.runtime.js'
}, {
from: './webpack/replace/log-apply-result.js',
to: './node_modules/webpack/hot/log-apply-result.js'
} ]
replaceTasks.forEach(task => fs.writeFileSync(task.to, fs.readFileSync(task.from)))
})
/*
* dev tasks
*/
gulp.task('webpack-dev-server', () => {
let myConfig = Object.create(devConfig)
new WebpackDevServer(webpack(myConfig, (err, stats) => {
if (err) throw new gutil.PluginError('webpack', err)
gutil.log('Please load the unpacked extension with `./dev` folder.\n (see https://developer.chrome.com/extensions/getstarted#unpacked)')
}), {
publicPath: myConfig.output.publicPath,
stats: {colors: true},
noInfo: true,
hot: true,
historyApiFallback: true
// https: true
}).listen(port, 'localhost', (err) => {
if (err) throw new gutil.PluginError('webpack-dev-server', err)
gutil.log('[webpack-dev-server]', `listening at port ${port}`)
})
})
gulp.task('views:dev', () => {
return gulp.src('./chrome/views/*.pug')
.pipe(pug({
locals: {
env: 'dev',
devToolsExt: !!process.env.DEVTOOLS_EXT || true
}
}))
.pipe(gulp.dest('./dev'))
})
gulp.task('copy:dev', () => {
const manifest = gulp.src('./chrome/manifest.dev.json')
.pipe(rename('manifest.json'))
.pipe(gulp.dest('./dev'))
const assets = gulp.src('./chrome/assets/**/*').pipe(gulp.dest('./dev'))
return merge(manifest, assets)
})
/*
* build tasks
*/
gulp.task('webpack:build', (callback) => {
let myConfig = Object.create(prodConfig)
webpack(myConfig, (err, stats) => {
if (err) {
throw new gutil.PluginError('webpack:build', err)
}
gutil.log('[webpack:build]', stats.toString({ colors: true }))
callback()
})
})
gulp.task('views:build', () => {
return gulp.src('./chrome/views/*.pug')
.pipe(pug({
locals: { env: 'prod' }
}))
.pipe(gulp.dest('./build'))
})
gulp.task('copy:build', () => {
const manifest = gulp.src('./chrome/manifest.prod.json')
.pipe(rename('manifest.json'))
.pipe(gulp.dest('./build'))
const assets = gulp.src('./chrome/assets/**/*').pipe(gulp.dest('./build'))
return merge(manifest, assets)
})
gulp.task('default', [ 'replace-webpack-code', 'webpack-dev-server', 'views:dev', 'copy:dev' ])
gulp.task('build', [ 'replace-webpack-code', 'webpack:build', 'views:build', 'copy:build' ])