-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
181 lines (145 loc) · 4.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var gulp = require('gulp');
var del = require('del');
var plugins = require('gulp-load-plugins')();
var argv = require('yargs').argv;
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');
var p = require('./package.json'),
stringify = require('stringify');
var Server = require('karma').Server;
/* nicer browserify errors */
var gutil = require('gulp-util');
var chalk = require('chalk');
var projectName= "sd-model";
var standaloneName= "SilverDecisions.Model";
var dependencies = [];
var vendorDependencies = [];
var sdDependencies = [];
for(var k in p.dependencies){
if(p.dependencies.hasOwnProperty(k)){
dependencies.push(k);
if(k.trim().startsWith("sd-")){
sdDependencies.push(k)
}else{
vendorDependencies.push(k)
}
}
}
gulp.task('prepare-test', function(){
return gulp
.src('test/data/*.json')
.pipe(require('gulp-filelist')('data-json-filelist.json', { flatten: true }))
.pipe(gulp.dest('test'))
});
gulp.task('test', gulp.series('prepare-test', function (done) {
return runTest(true, done)
}));
gulp.task('test-watch', gulp.series('prepare-test', function (done) {
return runTest(false, done)
}));
gulp.task('clean', function (cb) {
return del(['tmp', 'dist'], cb);
});
gulp.task('build-standalone', function () {
var jsFileName = projectName;
return buildJs('./index.js', standaloneName, jsFileName, "dist/standalone")
});
gulp.task('build-module', function () {
var jsFileName = projectName;
// return buildJs('./index.js', 'sd-utils', jsFileName, "dist", sdDependencies)
var b = browserify({
debug: true,
})
.require('./index.js', {expose: projectName} )
.external(sdDependencies)
return finishBrowserifyBuild(b, jsFileName, "dist")
});
gulp.task('build', gulp.parallel('build-standalone', 'build-module'));
gulp.task('build-clean', gulp.series('clean', 'build'));
gulp.task('default', gulp.series('build-clean', 'test'));
function runTest(singleRun, done){
return new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: singleRun
}, function (err) {
done(err);
}).start();
}
function buildJs(src, standaloneName, jsFileName, dest, external) {
if(!external){
external = []
}
var b = browserify({
basedir: '.',
debug: true,
entries: [src],
cache: {},
packageCache: {},
standalone: standaloneName
}).transform(stringify, {
appliesTo: { includeExtensions: ['.html'] }
})
// .plugin(resolutions, '*')
.external(external)
return finishBrowserifyBuild(b,jsFileName, dest)
}
function buildJsDependencies(jsFileName, moduleNames, dest){
var b = browserify({
debug: true,
require: [moduleNames]
})
return finishBrowserifyBuild(b, jsFileName, dest)
}
function finishBrowserifyBuild(b, jsFileName, dest){
var pipe = b
.transform("babelify", {presets: ["@babel/preset-env"], plugins: ["transform-class-properties", "transform-object-assign", ["transform-builtin-extend", {globals: ["Error"]}]]})
.bundle()
.on('error', map_error)
.pipe(plugins.plumber({ errorHandler: onError }))
.pipe(source(jsFileName+'.js'))
.pipe(gulp.dest(dest))
.pipe(buffer());
var development = (argv.dev !== undefined);
if(!development){
pipe.pipe(sourcemaps.init({loadMaps: true}))
// .pipe(plugins.stripDebug())
.pipe(plugins.uglify({
compress: {
// drop_console: true
}
}))
.pipe(plugins.rename({ extname: '.min.js' }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(dest));
}
return pipe;
}
// error function for plumber
var onError = function (err) {
console.log('onError', err);
this.emit('end');
};
function map_error(err) {
if (err.fileName) {
// regular error
gutil.log(chalk.red(err.name)
+ ': '
+ chalk.yellow(err.fileName.replace(__dirname + '/src/js/', ''))
+ ': '
+ 'Line '
+ chalk.magenta(err.lineNumber)
+ ' & '
+ 'Column '
+ chalk.magenta(err.columnNumber || err.column)
+ ': '
+ chalk.blue(err.description))
} else {
// browserify error..
gutil.log(chalk.red(err.name)
+ ': '
+ chalk.yellow(err.message))
}
this.emit('end');
}