forked from popcodeorg/popcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
150 lines (133 loc) · 3.94 KB
/
gulpfile.babel.js
File metadata and controls
150 lines (133 loc) · 3.94 KB
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
/* eslint-env node */
import fs from 'fs';
import https from 'https';
import gulp from 'gulp';
import concat from 'gulp-concat';
import sourcemaps from 'gulp-sourcemaps';
import cssnano from 'cssnano';
import gutil from 'gulp-util';
import forOwn from 'lodash/forOwn';
import git from 'git-rev-sync';
import config from './src/config';
import postcss from 'gulp-postcss';
import cssnext from 'postcss-cssnext';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackConfiguration from './webpack.config';
const browserSync = require('browser-sync').create();
const srcDir = 'src';
const baseDir = 'static';
const distDir = `${baseDir}/compiled`;
const stylesheetsDir = `${srcDir}/css`;
const bowerComponents = 'bower_components';
const cssnextBrowsers = [];
const supportedBrowsers =
JSON.parse(fs.readFileSync('./config/browsers.json'));
forOwn(supportedBrowsers, (version, browser) => {
let browserForCssnext = browser;
if (browser === 'msie') {
browserForCssnext = 'ie';
}
cssnextBrowsers.push(`${browserForCssnext} >= ${version}`);
});
gulp.task('env', () => {
process.env.GIT_REVISION = git.short();
if (gulp.env.production) {
process.env.NODE_ENV = 'production';
}
});
gulp.task('fonts', () => gulp.
src([
`${bowerComponents}/inconsolata-webfont/fonts/inconsolata-regular.*`,
`${bowerComponents}/fontawesome/fonts/fontawesome-webfont.*`,
`${bowerComponents}/roboto-webfont-bower/fonts/` +
'Roboto-{Bold,Regular}-webfont.*',
]).
pipe(gulp.dest(`${distDir}/fonts`))
);
gulp.task('css', () => {
const processors = [cssnext({browsers: cssnextBrowsers})];
if (gutil.env.production) {
processors.push(cssnano());
}
return gulp.
src([
`${bowerComponents}/normalize-css/normalize.css`,
`${stylesheetsDir}/**/*.css`,
]).
pipe(concat('application.css')).
pipe(sourcemaps.init({loadMaps: true})).
pipe(postcss(processors)).
pipe(sourcemaps.write('./')).
pipe(gulp.dest(distDir)).
pipe(browserSync.stream());
});
gulp.task('js', ['env'], () => {
const productionWebpackConfig = Object.create(webpackConfiguration);
productionWebpackConfig.plugins = productionWebpackConfig.plugins.concat(
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {warnings: false},
output: {comments: false},
sourceMap: true,
})
);
return new Promise((resolve, reject) => {
webpack(productionWebpackConfig, (err, stats) => {
if (err) {
reject(err);
return;
}
gutil.log('[webpack:build]', stats.toString({
colors: true,
}));
resolve();
});
});
});
gulp.task('build', ['fonts', 'css', 'js']);
gulp.task('syncFirebase', () => new Promise((resolve, reject) => {
fs.readFile(`${__dirname}/config/firebase-auth.json`, (err, data) => {
if (err) {
reject(err);
}
const firebaseSecret = process.env.FIREBASE_SECRET;
if (!firebaseSecret) {
reject('Missing environment variable FIREBASE_SECRET');
}
const req = https.request({
hostname: `${config.firebaseApp}.firebaseio.com`,
path: `/.settings/rules.json?auth=${firebaseSecret}`,
method: 'PUT',
}, (res) => {
if (res.statusCode === 200) {
resolve();
} else {
res.on('data', reject);
}
});
req.write(data);
req.end();
});
}));
gulp.task('dev', ['browserSync', 'fonts', 'css'], () => {
gulp.watch(`${stylesheetsDir}/**/*.css`, ['css']);
gulp.watch(`${baseDir}/*`).on('change', browserSync.reload);
});
gulp.task('browserSync', () => {
const compiler = webpack(webpackConfiguration);
compiler.plugin('invalid', browserSync.reload);
browserSync.init({
server: {
baseDir,
middleware: [webpackDevMiddleware(
compiler,
{
lazy: false,
stats: 'errors-only',
publicPath: webpackConfiguration.output.publicPath,
}
)],
},
});
});