-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
363 lines (330 loc) · 10.1 KB
/
gulpfile.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Task running and building through Gulp.
* http://gulpjs.com/
*
* Overall, use config files (like .babelrc) to manage
* options for processes. This will allow moving away from
* Gulp more easily if desired.
*/
'use strict';
// Catch things
process.on('unhandledRejection', console.error);
// Dependencies
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const ejs = require('gulp-ejs');
const rename = require('gulp-rename');
const noopener = require('gulp-noopener');
const eslint = require('gulp-eslint');
const stylelint = require('gulp-stylelint');
const sass = require('gulp-sass');
const htmlhint = require('gulp-htmlhint');
const autoprefixer = require('gulp-autoprefixer');
const include = require('gulp-file-include');
const sourcemaps = require('gulp-sourcemaps');
const gutil = require('gulp-util');
const a11y = require('gulp-a11y');
const transform = require('gulp-transform');
const runSequence = require('run-sequence');
const browserSync = require('browser-sync').create();
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const webpackConfig = require('./webpack.config.js');
const del = require('del');
const gulpContent = require('./lib/gulp-content.js');
const gulpPublish = require('./lib/gulp-publish.js');
const _ = require('lodash');
const r = require('request');
const request = require('cached-request')(r);
const jest = require('./lib/gulp-jest.js');
const pkg = require('./package.json');
const parseData = require('./app/parse-incoming-data.js');
const config = exists('config.custom.json')
? require('./config.custom.json')
: require('./config.json');
const argv = require('yargs').argv;
require('dotenv').load({ silent: true });
// Register svelte to be able to require
require('svelte/ssr/register')({});
// Process base html templates/pages (not templates used in front-end JS)
gulp.task('html', () => {
const content = exists('content.json') ? require('./content.json') : {};
return gulp
.src(['pages/**/*.ejs.html', '!pages/**/_*.ejs.html'])
.pipe(
include({
prefix: '@@',
basepath: '@file'
})
)
.pipe(
ejs({ config: config, content: content, package: pkg, _: _ }).on(
'error',
gutil.log
)
)
.pipe(
rename(function(path) {
path.basename = path.basename.replace('.ejs', '');
})
)
.pipe(noopener.warn())
.pipe(gulp.dest('build/'));
});
// Lint HTML (happens after HTML build process). The "stylish" version
// is more succinct but its less helpful to find issues.
gulp.task('html:lint', ['html'], () => {
return gulp
.src('build/*.html')
.pipe(htmlhint('.htmlhintrc'))
.pipe(htmlhint.reporter('htmlhint-stylish'))
.pipe(a11y())
.pipe(a11y.reporter());
});
gulp.task('html:lint:details', ['html'], () => {
return gulp
.src('build/*.html')
.pipe(htmlhint('.htmlhintrc'))
.pipe(htmlhint.reporter())
.pipe(a11y())
.pipe(a11y.reporter());
});
// Server-side rendering of
gulp.task('components', done => {
let data = {};
// Render
function render(content, file) {
const component = require(file.path);
const { html, css } = component.render(data);
return `<style type="text/css">${css.code}</style>\n\n${html}`;
}
// Get data
request.setCacheDirectory(path.join(__dirname, '.cache-component-data'));
request(
{
url: config.data.camps,
ttl: 60 * 10
},
(error, response, body) => {
if (error) {
return new gutil.PluginError('components', { message: error });
}
let parsed = JSON.parse(body);
data.camps = parseData(parsed.items ? parsed.items : parsed);
// Do each file
return gulp
.src('components/**/*.html')
.pipe(transform('utf-8', render))
.pipe(
rename({
prefix: 'component-'
})
)
.pipe(gulp.dest('build/'))
.on('end', done);
}
);
});
// Content tasks
gulp.task('content', gulpContent.getContent(gulp, config));
gulp.task('content:create', gulpContent.createSheet(gulp, config));
gulp.task('content:open', gulpContent.openContent(gulp, config));
gulp.task('content:owner', gulpContent.share(gulp, config, 'owner'));
gulp.task('content:share', gulpContent.share(gulp, config, 'writer'));
// Lint JS
gulp.task('js:lint', () => {
return gulp
.src(['app/**/*.js', 'gulpfile.js'])
.pipe(eslint())
.pipe(eslint.format());
});
// Lint styles/css
gulp.task('styles:lint', () => {
return gulp.src(['styles/**/*.scss']).pipe(
stylelint({
failAfterError: false,
reporters: [{ formatter: 'string', console: true }]
})
);
});
// Compile styles
gulp.task('styles', ['styles:lint'], () => {
return gulp
.src('styles/index.scss')
.pipe(sourcemaps.init())
.pipe(
sass({
outputStyle: 'compressed',
includePaths: [path.join(__dirname, 'node_modules')]
}).on('error', sass.logError)
)
.pipe(
autoprefixer({
// browsers: See browserlist file
cascade: false
})
)
.pipe(
rename(path => {
path.basename =
path.basename === 'index' ? 'styles.bundle' : path.basename;
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('build/'));
});
// Build JS
gulp.task('js', ['js:lint', 'js:test'], () => {
// Use the webpack.config.js to manage locations and options.
return gulp
.src('app/index.js')
.pipe(webpackStream(webpackConfig, webpack))
.pipe(gulp.dest('build'));
});
// Assets
gulp.task('assets', () => {
// Copy a couple files to root for more global support
gulp.src(['./assets/images/favicons/favicon.ico']).pipe(gulp.dest('build'));
return gulp.src('assets/**/*').pipe(gulp.dest('build/assets'));
});
// Clean build
gulp.task('clean', () => {
return del(['build/**/*']);
});
// Testing ,manully using jest module because
gulp.task(
'js:test',
jest('js:test', {
rootDir: __dirname,
testMatch: ['**/*.test.js'],
testPathIgnorePatterns: ['acceptance'],
setupFiles: ['./tests/globals.js']
})
);
gulp.task(
'js:test:acceptance',
jest('js:test:acceptance', {
rootDir: __dirname,
// Not sure why full path is needed
testMatch: [path.join(__dirname, 'tests/acceptance/*.test.js')]
})
);
// Web server for development. Do build first to ensure something is there.
gulp.task('server', ['build'], () => {
// Proxy the dev version of news-platform. (assumes the host file has been set up)
// https://github.com/MinneapolisStarTribune/news-platform
//
// We serve the build files in a way that can be used by the serve_static
// function in news-platform. This means that the path is the same, but
// the domain changes; locally we serve it from here at localhost:3000,
// but for production it runs from static.startribune.com.
//
// news-platform knows about the local domain through the ASSETS_STATIC_URL
// environment variable. This can be in a .env file in your locally running
// news-platform.
//
// The publish.production can change location but the "route" option below
// will need to change so local and production act the same.
//
// serve_static function for reference:
// https://github.com/MinneapolisStarTribune/news-platform/blob/1a56bd11892f79e5d48a9263bed2db7c5539fc60/app/Extensions/helpers/url.php#L272
// Make rewrite rules
let rewriteRules = [];
_.each(config.cms.componentMapping, (component, id) => {
rewriteRules.push({
match: new RegExp(
`<div class="${id}">([\\s\\S]*)<\\/div>\\s*<!-- end ${id} -->`,
'im'
),
fn: function(request) {
// Make sure its only for the CMS pages and we have something
// to replace it with
if (
request.originalUrl.indexOf('show=1&cache=trash') &&
exists(`build/${component}`)
) {
let inject = fs.readFileSync(`build/${component}`, 'utf-8');
// Handle rewriting any production path urls for build
inject = inject.replace(
new RegExp(config.publish.production.url, 'ig'),
'/'
);
return `<div class="${id}">${inject}</div>`;
}
return `<div class="${id}">$1</div>`;
}
});
});
// Without CMS
if (argv.cms === false) {
return browserSync.init({
port: 3000,
server: './build/',
files: './build/**/*',
rewriteRules: argv.rewrite === false ? [] : rewriteRules
});
}
// With CMS
return browserSync.init({
port: 3000,
proxy:
'https://' +
(argv.mobile ? 'vm-m' : 'vm-www') +
'.startribune.com/x/' +
(argv['cms-id'] ? argv['cms-id'] : config.cms.id) +
'?show=1&cache=trash',
serveStatic: [
{
route: '/' + config.publish.production.path,
dir: './build'
}
],
files: './build/**/*',
rewriteRules: argv.rewrite === false ? [] : rewriteRules,
logLevel: argv.debug ? 'debug' : 'info'
});
});
// Watch for building
gulp.task('watch', () => {
gulp.watch(['styles/**/*.scss'], ['styles']);
gulp.watch(['components/**/*.html'], ['components']);
gulp.watch(
['pages/**/*', 'config.*json', 'package.json', 'content.json'],
['html']
);
gulp.watch(['app/**/*', 'components/**/*.html', 'config.json'], ['js']);
gulp.watch(['assets/**/*'], ['assets']);
gulp.watch(['config.*json'], ['publish:build']);
});
// Publishing
gulp.task(
'publish',
['publish:token', 'publish:confirm'],
gulpPublish.publish(gulp)
);
gulp.task('publish:token', gulpPublish.createToken(gulp));
gulp.task('publish:build', gulpPublish.buildConfig(gulp));
gulp.task('publish:confirm', gulpPublish.confirmToken(gulp));
gulp.task('publish:open', gulpPublish.openURL(gulp));
// Full build
gulp.task('build', [
'publish:build',
'assets',
'html',
'components',
'styles',
'js'
]);
gulp.task('default', ['build']);
// Deploy (build and publish)
gulp.task('deploy', done => {
return runSequence('clean', 'build', 'publish', done);
});
gulp.task('deploy:open', ['publish:open']);
// Server and watching (development)
gulp.task('develop', ['server', 'watch']);
// Check file/fir exists
function exists(file) {
return fs.existsSync(path.join(__dirname, file));
}