-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
187 lines (156 loc) · 4.21 KB
/
Copy pathgulpfile.js
File metadata and controls
187 lines (156 loc) · 4.21 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
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
'use strict';
require('colors');
let changed = require('gulp-changed');
let extract = require('gulp-livereload-mithril');
let fs = require('fs');
let gap = require('gulp-append-prepend');
let getWebpackConf = require('./gulpfile.webpack.js');
let gls = require('gulp-live-server');
let glob = require('glob');
let gulp = require('gulp');
let httpServer = require('http-server');
let open = require('open');
let webpack = require('webpack-stream');
const DEV_DIST_PATH = 'dev-dist';
const PROD_DIST_PATH = 'dist';
function doOnce(cb) {
let first = true;
return function(elseCb) {
if(first) {
cb();
} else {
elseCb();
}
first = false;
}
}
function getCore(cb) {
let config = getWebpackConf();
config.output = {
filename: 'widgets.js'
};
cb(config);
}
function watchCoreWithLiveReload(onbuild) {
getCore(config => {
function buildWithLiveReload() {
onbuild(gulp.src('src/main.js')
.pipe(webpack(config))
.pipe(gap.appendText('var livereload=document.createElement("script");livereload.src="http://localhost:35729/livereload.js";document.head.appendChild(livereload);\n'))
.pipe(gulp.dest(DEV_DIST_PATH)));
}
buildWithLiveReload();
gulp.watch([ 'src/*', 'src/**/*' ], buildWithLiveReload);
});
}
function getWidgets(cb) {
glob('widgets/**/main.js', function(err, files) {
files.forEach(file => {
let path = file.replace(/\/src\/main\.js$/, '');
let moduleName = path.split('/').slice(-1)[0];
try {
let config = getWebpackConf(path);
config.module.loaders.push({
test: /\.js/,
loader: 'webpack-append',
query: 'var Widgets = SearchSpring.Widgets, m = Widgets.Utilities.m;'
});
config.output = {
filename: 'widget.js'
};
cb(file, path, config);
} catch(e) {
console.log(e);
return;
}
});
});
}
function watchWidgetsWithLiveReload(onbuild) {
getWidgets(function(file, path, config) {
function buildWithLiveReload() {
onbuild(gulp.src(file)
.pipe(webpack(config))
.pipe(gap.appendText('var st8less=document.createElement("script");st8less.src="/' + DEV_DIST_PATH + '/' + path + '/st8less.js";document.head.appendChild(st8less);\n'))
.pipe(extract({
inject: false
}))
.pipe(changed(DEV_DIST_PATH + '/' + path, {
hasChanged: changed.compareSha1Digest
}))
.pipe(gulp.dest(DEV_DIST_PATH + '/' + path)));
}
buildWithLiveReload();
gulp.watch([ path + '/src/*', path + '/src/**/*' ], buildWithLiveReload);
});
}
function runTests(done) {
var Server = require('karma').Server;
return new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
}
gulp.task('test', function(done) {
runTests(done);
});
gulp.task('build/widgets', function() {
getWidgets(function(file, path, config) {
gulp.src(file)
.pipe(webpack(config))
.pipe(gulp.dest(PROD_DIST_PATH + '/' + path));
});
});
gulp.task('build/core', function() {
getCore(config => {
config.plugins = config.plugins || [];
config.plugins.push(new (require('webpack')).optimize.UglifyJsPlugin());
gulp.src('src/main.js')
.pipe(webpack(config))
.pipe(gulp.dest(PROD_DIST_PATH));
});
});
gulp.task('build', function() {
runTests(function(failed) {
if(failed != 0) {
return;
}
gulp.start('build/core');
gulp.start('build/widgets');
});
});
gulp.task('develop', function() {
let portfinder = require('portfinder');
portfinder.basePort = 8080;
portfinder.getPort((err, port) => {
if(err) {
throw err;
}
let server = httpServer.createServer({
root: __dirname,
showDir: true,
autoIndex: true
});
server.listen(port, '0.0.0.0', function() {
let liveReloadServer = gls.static('/', 3000);
liveReloadServer.start();
watchCoreWithLiveReload(function(stream) {
stream.pipe(liveReloadServer.notify());
});
watchWidgetsWithLiveReload(function(stream) {
stream.pipe(liveReloadServer.notify());
});
// TODO: Open browser after widgets and core have built once
let previewPage = 'http://localhost:' + port + '/examples';
open(previewPage, function(err) {
if(err) {
throw err;
}
});
gulp.start('test');
gulp.watch('src/*', [ 'test' ]);
gulp.watch('src/**/*', [ 'test' ]);
gulp.watch('widgets/**/*', [ 'test' ]);
});
});
});