This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
gulp-tasks-dev.js
executable file
·164 lines (133 loc) · 4.93 KB
/
gulp-tasks-dev.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
import webpack from 'webpack';
import yargs from 'yargs';
const argv = yargs.argv;
import WebpackDevServer from 'webpack-dev-server';
import gulpOpen from 'gulp-open';
import path from 'path';
import {writeFile} from 'fs';
import deepAssign from 'deep-assign';
import gulpOptionsBuilder from './gulp-options-builder';
import gulpTasksCore from './gulp-tasks-core';
import gulpTasksTest from './gulp-tasks-test';
import gulpTasksLinters from './gulp-tasks-linters';
export function devTasks (gulp, opts) {
const runSequence = require('run-sequence').use(gulp);
gulpTasksCore(gulp, opts);
gulpTasksTest(gulp, opts);
gulpTasksLinters(gulp, opts);
const options = gulpOptionsBuilder(opts);
gulp.task('dev-preprocess', (callback) => {
if (argv.skipPreprocess) {
callback();
} else if (options.devPreprocess) {
runSequence('preprocess', options.devPreprocess, 'copy', callback);
} else {
runSequence('preprocess', 'copy', callback);
}
});
gulp.task('dev', ['dev-preprocess'], () => {
let webpackConfigPath = path.resolve(
__dirname, 'webpack.dev.config.js'
);
if (argv.config) {
webpackConfigPath = path.resolve(argv.config);
}
const config = require(webpackConfigPath);
const devServerConfig = {
contentBase: options.dist,
hot: !options.devServerDisableHot,
inline: true,
stats: {
colors: true
},
publicPath: config.output.publicPath,
historyApiFallback: true
};
if (options.watchOptions) {
devServerConfig.watchOptions = options.watchOptions;
}
if (options.devServerProxy) {
devServerConfig.proxy = options.devServerProxy;
}
if (options.devServer) {
deepAssign(devServerConfig, options.devServer);
}
if (options.webpackProfile) config.profile = true;
const compiler = webpack(config);
if (options.webpackProfile) {
compiler.plugin('done', stats => {
const profileFile = path.resolve(options.webpackProfile);
const statsString = JSON.stringify(stats.toJson());
writeFile(profileFile, statsString, (err) => {
if (err) return console.error('Failed to write webpackProfile:', err);
console.log('[webpack] Wrote webpack stats to:', profileFile);
console.log('[webpack] Analyze stats at https://webpack.github.io/analyse/');
});
});
}
const server = new WebpackDevServer(compiler, devServerConfig);
server.use('/', (req, res, next) => {
const acceptLanguageHeader = req.headers['accept-language'];
if (acceptLanguageHeader) {
const acceptedLanguages = acceptLanguageHeader.match(
/[a-zA-z\-]{2,10}/g
);
if (acceptedLanguages) {
res.cookie('languages', JSON.stringify(acceptedLanguages));
}
}
if (req.url.match(/.+\/img\//)) { // img
res.redirect(301, req.url.replace(/.*\/(img\/.*)$/, '/$1'));
} else if (req.url.match(/\/img\//)) { // img
next();
} else if (req.url.match(/.+\/video\//)) { // video
res.redirect(301, req.url.replace(/.*\/(video\/.*)$/, '/$1'));
} else if (req.url.match(/\/video\//)) { // video
next();
} else if (req.url.match(/.+\/font\//)) { // font
res.redirect(301, req.url.replace(/.*\/(font\/.*)$/, '/$1'));
} else if (req.url.match(/\/font\//)) { // font
next();
} else if (req.url.match(/.+\/.*\.[^\/]*$/)) { // file
res.redirect(301, req.url.replace(/.*\/([^\/]*)$/, '/$1'));
} else {
next();
}
});
// Always open on all ports unless overridden
const host = options.devServerHost || '0.0.0.0';
server.listen(options.devServerPort || 8080, host, (err) => {
if (err) {
console.error('[webpack-dev-server] failed to start:', err);
} else {
const protocol = (options.devServer && options.devServer.https) ? 'https' : 'http';
const openHost = (host === '0.0.0.0') ? 'localhost' : host;
const suffix = options.publicPath ? options.publicPath + '/' : '';
const openURL = protocol + '://' + openHost + ':' + options.devServerPort + suffix;
let openMsg = '[webpack-dev-server] started: ';
if (argv.skipOpen) {
openMsg += `app available at location: \u001b[33m${openURL}\u001b[39m`;
} else {
openMsg += 'opening the app in your default browser...';
}
console.log(openMsg);
if (argv.skipOpen) return;
gulp.src(__filename)
.pipe(gulpOpen({
uri: openURL
}));
}
});
server.app.get('/reload', (req, res) => {
// Tell connected browsers to reload.
server.sockWrite(server.sockets, 'ok');
res.sendStatus(200);
});
server.app.get('/invalid', (req, res) => {
// Tell connected browsers some change is about to happen.
server.sockWrite(server.sockets, 'invalid');
res.sendStatus(200);
});
});
};
export default devTasks;