This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathgulpfile.babel.js
210 lines (184 loc) · 5.54 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
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
/**
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'vinyl-buffer';
import del from 'del';
import eslint from 'gulp-eslint';
import glob from 'glob';
import gulp from 'gulp';
import gutil from 'gulp-util';
import cleanCSS from 'gulp-clean-css';
import packageJson from './package.json';
import path from 'path';
import rev from 'gulp-rev';
import sass from 'gulp-sass';
import sequence from 'run-sequence';
import source from 'vinyl-source-stream';
import {spawn} from 'child_process';
import swPrecache from 'sw-precache';
import uglify from 'gulp-uglify';
const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const THIRD_PARTY_MODULES = [
'immutable',
'isomorphic-fetch',
'react',
'react-dom',
'react-redux',
'react-router',
'redux',
'redux-actions',
'redux-promise'
];
gulp.task('clean', () => {
return del(BUILD_DIR);
});
gulp.task('bundle-app', () => {
let bundler = browserify({
entries: path.join(SRC_DIR, 'components', 'client.jsx'),
extensions: ['.jsx'],
transform: [babelify]
});
THIRD_PARTY_MODULES.forEach(module => bundler.external(module));
return bundler.bundle()
.on('error', function(error) {
gutil.log('Babelify error:', error.message);
this.emit('end');
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulp.dest(`${BUILD_DIR}/js`));
});
gulp.task('bundle-third-party', () => {
let bundler = browserify();
THIRD_PARTY_MODULES.forEach(module => bundler.require(module));
return bundler.bundle()
.on('error', function(error) {
gutil.log('Babelify error:', error.message);
this.emit('end');
})
.pipe(source('third-party.js'))
.pipe(buffer())
.on('error', gutil.log)
.pipe(gulp.dest(`${BUILD_DIR}/js`));
});
gulp.task('copy-static', () => {
return gulp.src(`${SRC_DIR}/static/**/*`)
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('sass', () => {
return gulp.src(`${SRC_DIR}/static/sass/*.scss`)
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS())
.pipe(gulp.dest(`${BUILD_DIR}/styles`));
});
gulp.task('uglify-js', () => {
return gulp.src(`${BUILD_DIR}/js/**/*`)
.pipe(uglify())
.pipe(gulp.dest(`${BUILD_DIR}/js`));
});
gulp.task('version-assets', () => {
return gulp.src(`${BUILD_DIR}/*/*`)
.pipe(rev())
.pipe(gulp.dest(`${BUILD_DIR}/rev`))
.pipe(rev.manifest())
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('generate-service-worker', () => {
let serviceWorkerFile = path.join(BUILD_DIR, 'service-worker.js');
return swPrecache.write(serviceWorkerFile, {
// Start of interesting bits!
// Ensure all our static, local assets are cached.
staticFileGlobs: [
`${BUILD_DIR}/rev/js/**/*.js`,
`${BUILD_DIR}/rev/styles/all*.css`,
`${BUILD_DIR}/images/**/*`
],
// Define the dependencies for the server-rendered /shell URL,
// so that it's kept up to date.
dynamicUrlToDependencies: {
'/shell': [
...glob.sync(`${BUILD_DIR}/rev/js/**/*.js`),
...glob.sync(`${BUILD_DIR}/rev/styles/all*.css`),
`${SRC_DIR}/views/index.handlebars`
]
},
// Brute force server worker routing:
// Tell the service worker to use /shell for all navigations.
// E.g. A request for /guides/12345 will be fulfilled with /shell
navigateFallback: '/shell',
// Various runtime caching strategies: sets up sw-toolbox handlers.
runtimeCaching: [{
urlPattern: /www\.ifixit\.com\/api\/2\.0\//,
// Effectively "stale while revalidate".
handler: 'fastest'
}, {
urlPattern: /cloudfront\.net/,
handler: 'cacheFirst',
options: {
cache: {
name: 'image-cache',
// Use sw-toolbox's LRU expiration.
maxEntries: 50
}
}
}, {
// Use a network first strategy for everything else.
default: 'networkFirst'
}],
// End of interesting bits...
cacheId: packageJson.name,
dontCacheBustUrlsMatching: /./,
logger: gutil.log,
stripPrefix: 'build/',
verbose: true
});
});
gulp.task('build:dev', ['clean'], callback => {
process.env.NODE_ENV = 'development';
sequence(
['bundle-app', 'bundle-third-party', 'copy-static', 'sass'],
'version-assets',
'generate-service-worker',
callback
);
});
gulp.task('build:dist', ['clean'], callback => {
process.env.NODE_ENV = 'production';
sequence(
['bundle-app', 'bundle-third-party', 'copy-static', 'sass', 'lint'],
'uglify-js',
'version-assets',
'generate-service-worker',
callback
);
});
gulp.task('serve', callback => {
spawn('node', ['index.js'], {stdio: 'inherit'})
.on('error', error => callback(error))
.on('exit', error => callback(error));
});
gulp.task('lint', () => {
return gulp.src([`${SRC_DIR}/**/*.{js,jsx}`, '*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError());
});
gulp.task('default', callback => {
sequence('build:dev', 'serve', callback);
});
process.on('SIGINT', process.exit);