forked from zendesk/linksf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.js
166 lines (146 loc) · 5.64 KB
/
run.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
/**
* React Static Boilerplate
* https://github.com/kriasoft/react-static-boilerplate
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* eslint-disable no-console, global-require */
const fs = require('fs');
const del = require('del');
const ejs = require('ejs');
const webpack = require('webpack');
const firebase = require('firebase-tools');
const browserSync = require('browser-sync');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
// TODO: Update configuration settings
const config = {
title: 'Link-SF', // Your website title
url: 'https://link-sf.com', // Your website URL
project: 'link-sf', // Firebase project. See README.md -> How to Deploy
trackingID: 'UA-XXXXX-Y', // Google Analytics Site's ID
};
const tasks = new Map(); // The collection of automation tasks ('clean', 'build', 'publish', etc.)
function run(task) {
const start = new Date();
console.log(`Starting '${task}'...`);
return Promise.resolve().then(() => tasks.get(task)()).then(() => {
console.log(`Finished '${task}' after ${new Date().getTime() - start.getTime()}ms`);
}, err => console.error(err.stack));
}
//
// Clean up the output directory
// -----------------------------------------------------------------------------
tasks.set('clean', () => del(['public/dist/*', '!public/dist/.git'], { dot: true }));
//
// Copy ./index.html into the /public folder
// -----------------------------------------------------------------------------
tasks.set('html', () => {
const webpackConfig = require('./webpack.config');
const assets = JSON.parse(fs.readFileSync('./public/dist/assets.json', 'utf8'));
const template = fs.readFileSync('./public/index.ejs', 'utf8');
const render = ejs.compile(template, { filename: './public/index.ejs' });
const output = render({ debug: webpackConfig.debug, bundle: assets.main.js, config });
fs.writeFileSync('./public/index.html', output, 'utf8');
});
//
// Generate sitemap.xml
// -----------------------------------------------------------------------------
tasks.set('sitemap', () => {
const urls = require('./routes.json')
.filter(x => !x.path.includes(':'))
.map(x => ({ loc: x.path }));
const template = fs.readFileSync('./public/sitemap.ejs', 'utf8');
const render = ejs.compile(template, { filename: './public/sitemap.ejs' });
const output = render({ config, urls });
fs.writeFileSync('public/sitemap.xml', output, 'utf8');
});
//
// Bundle JavaScript, CSS and image files with Webpack
// -----------------------------------------------------------------------------
tasks.set('bundle', () => {
const webpackConfig = require('./webpack.config');
return new Promise((resolve, reject) => {
webpack(webpackConfig).run((err, stats) => {
if (err) {
reject(err);
} else {
console.log(stats.toString(webpackConfig.stats));
resolve();
}
});
});
});
//
// Build website into a distributable format
// -----------------------------------------------------------------------------
tasks.set('build', () => Promise.resolve()
.then(() => run('clean'))
.then(() => run('bundle'))
.then(() => run('html'))
.then(() => run('sitemap'))
);
//
// Build and publish the website
// -----------------------------------------------------------------------------
tasks.set('publish', () => {
global.DEBUG = process.argv.includes('--debug') || false;
return run('build')
.then(() => firebase.login({ nonInteractive: false }))
.then(() => firebase.deploy({
project: config.project,
cwd: __dirname,
}))
.then(() => { setTimeout(() => process.exit()); });
});
//
// Build website and launch it in a browser for testing (default)
// -----------------------------------------------------------------------------
tasks.set('start', () => {
global.HMR = !process.argv.includes('--no-hmr'); // Hot Module Replacement (HMR)
const template = fs.readFileSync('./public/index.ejs', 'utf8');
const render = ejs.compile(template, { filename: './public/index.ejs' });
const output = render({ debug: true, bundle: '/dist/main.js', config });
fs.writeFileSync('./public/index.html', output, 'utf8');
const webpackConfig = require('./webpack.config');
const bundler = webpack(webpackConfig);
return new Promise(resolve => {
browserSync({
server: {
baseDir: 'public',
middleware: [
webpackDevMiddleware(bundler, {
// IMPORTANT: dev middleware can't access config, so we should
// provide publicPath by ourselves
publicPath: webpackConfig.output.publicPath,
// pretty colored output
stats: webpackConfig.stats,
// for other settings see
// http://webpack.github.io/docs/webpack-dev-middleware.html
}),
// bundler should be the same as above
webpackHotMiddleware(bundler),
// Serve index.html for all unknown requests
(req, res, next) => {
if (req.headers.accept.startsWith('text/html')) {
req.url = '/index.html'; // eslint-disable-line no-param-reassign
}
next();
},
],
},
// no need to watch '*.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
files: [
'public/**/*.css',
'public/**/*.html',
],
});
resolve();
});
});
// Execute the specified task or default one. E.g.: node run build
run(process.argv[2] || 'start');