-
Notifications
You must be signed in to change notification settings - Fork 126
/
gulpfile.js
163 lines (138 loc) · 4.19 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
var fs = require('fs-extra');
var path = require('path');
var nib = require('nib');
var Promise = require('bluebird');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var UglifyJS = require('uglify-js');
/**
* Usage: gulp [task]
*
* VALID TASKS
* - (no task specified)
* builds and watches for changes
* - publish
* cleans and builds for deployment
* - build
* builds docs into dist/
*/
var MAIN_JS_FILES = ['index.js', 'apps.js', 'devkit.js', 'mobile/apps.js'];
var paths = {
'dest': 'src/serve/static/',
'js': path.join(__dirname, 'src/web/'),
'jsio': path.join(__dirname, 'node_modules', 'jsio', 'packages'),
'squill': path.join(__dirname, 'node_modules', 'squill')
};
var globs = {
'js': 'src/web/**/*.js',
'static': 'src/web/**/!(*.js|*.styl)',
'stylus': 'src/web/stylesheets/*.styl',
'stylusWatcher': 'src/web/stylesheets/**/*.styl', // catches includes too
};
// default settings
var sourcemaps = false;
var compress = true;
gulp.task('default', plugins.sequence('setup-dev', 'build', 'watch'));
gulp.task('setup-dev', function (cb) {
// overwrite defaults
sourcemaps = true;
compress = false;
cb();
});
gulp.task('watch', function () {
gulp.watch(globs.js, ['js']);
gulp.watch(globs.static, ['static']);
gulp.watch(globs.stylusWatcher, ['stylus']);
gulp.src(paths.dest);
});
var jsio = require('jsio');
var compilerPath = path.join(jsio.__env.getPath(), '..', 'compilers');
function getCompiler() {
var j = jsio.clone();
j.path.add(compilerPath);
return j('import jsio_compile.compiler');
}
gulp.task('js', function (cb) {
jsio.path.add(compilerPath);
Promise.map(MAIN_JS_FILES, function (file) {
var importString = path.join('.', file);
return new Promise(function (resolve, reject) {
getCompiler().start(['jsio_compile', paths.js, importString], {
cwd: paths.js,
environment: 'browser',
path: [paths.jsio],
includeJsio: true,
appendImport: true,
compressSources: compress,
compressResult: compress,
pathCache: {
jsio: paths.jsio,
squill: paths.squill
},
interface: {
setCompiler: function (compiler) { this.compiler = compiler; },
run: function (args, opts) { this.compiler.run(args, opts); },
onError: function (err) {
cb && cb(err);
},
onFinish: function (opts, code) {
if (!fs.existsSync(paths.dest)) {
fs.mkdirSync(paths.dest);
}
var filename = path.join(paths.dest, file);
fs.outputFile(filename, code, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
},
compress: function (filename, src, opts, cb) {
var result = UglifyJS.minify(src, {
fromString: true,
compress: {
global_defs: {
DEBUG: false
}
}
});
// console.log(filename, '-->', result.code)
cb(result.code);
}
}
});
});
})
.nodeify(cb);
});
gulp.task('stylus', function () {
return gulp
.src(globs.stylus)
.pipe(plugins.stylus({
// cross-platform css rules
use: nib(),
// inline @import-ed css files, required since we import directly from node_modules
'include css': true,
// deploy opts
sourcemap: sourcemaps,
compress: compress
}))
.pipe(plugins.rename({extname: '.css'}))
.pipe(gulp.dest(paths.dest));
});
gulp.task('static', function () {
// copy all images and static files to dist/ unmodified
return gulp
.src(globs.static)
.pipe(plugins.filter(function (file) {
return file.stat.isFile();
}))
.pipe(plugins.debug({title: 'static'}))
.pipe(gulp.dest(paths.dest));
});
gulp.task('clean', function (cb) {
require('fs-extra').remove(paths.dest, cb);
});
gulp.task('build', ['stylus', 'js', 'static']);
gulp.task('publish', plugins.sequence('clean', 'build'));