forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
150 lines (137 loc) · 4.51 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
const babel = require('gulp-babel');
const browserify = require('browserify');
const derequire = require('gulp-derequire');
const gulp = require('gulp');
const insert = require('gulp-insert');
const path = require('path');
const rename = require('gulp-rename');
const source = require('vinyl-source-stream');
const uglify = require('gulp-uglify');
const watch = require('gulp-watch');
const BUILD = process.env.PARSE_BUILD || 'browser';
const VERSION = require('./package.json').version;
const transformRuntime = ["@babel/plugin-transform-runtime", {
"corejs": 3,
"helpers": true,
"regenerator": true,
"useESModules": false
}];
const PRESETS = {
'browser': ["@babel/preset-typescript", ["@babel/preset-env", {
"targets": "> 0.25%, not dead"
}]],
'weapp': ["@babel/preset-typescript", ["@babel/preset-env", {
"targets": "> 0.25%, not dead"
}], '@babel/react'],
'node': ["@babel/preset-typescript", ["@babel/preset-env", {
"targets": { "node": "14" }
}]],
'react-native': ["@babel/preset-typescript", 'module:metro-react-native-babel-preset'],
};
const PLUGINS = {
'browser': [transformRuntime, '@babel/plugin-proposal-class-properties', 'inline-package-json',
['transform-inline-environment-variables', {'exclude': ['SERVER_RENDERING']}]],
'weapp': [transformRuntime, '@babel/plugin-proposal-class-properties', 'inline-package-json',
['transform-inline-environment-variables', {'exclude': ['SERVER_RENDERING']}]],
'node': ['inline-package-json', 'transform-inline-environment-variables'],
'react-native': ['inline-package-json', 'transform-inline-environment-variables']
};
const DEV_HEADER = (
'/**\n' +
' * Parse JavaScript SDK v' + VERSION + '\n' +
' *\n' +
' * The source tree of this library can be found at\n' +
' * https://github.com/ParsePlatform/Parse-SDK-JS\n' +
' */\n'
);
const FULL_HEADER = (
'/**\n' +
' * Parse JavaScript SDK v' + VERSION + '\n' +
' *\n' +
' * Copyright 2015-present Parse Platform\n' +
' * All rights reserved.\n' +
' *\n' +
' * The source tree of this library can be found at\n' +
' * https://github.com/ParsePlatform/Parse-SDK-JS\n' +
' *\n' +
' * This source code is licensed under the license found in the LICENSE\n' +
' * file in the root directory of this source tree. Additional legal\n' +
' * information can be found in the NOTICE file in the same directory.\n' +
' */\n'
);
function compileTask(stream) {
return stream
.pipe(babel({
presets: PRESETS[BUILD],
plugins: PLUGINS[BUILD],
}))
// Second pass to kill BUILD-switched code
.pipe(babel({
plugins: ['minify-dead-code-elimination'],
}))
.pipe(gulp.dest(path.join('lib', BUILD)));
}
gulp.task('compile', function() {
return compileTask(gulp.src('src/*.*(js|ts)'));
});
gulp.task('browserify', function(cb) {
const stream = browserify({
builtins: ['_process', 'events'],
entries: 'lib/browser/Parse.js',
standalone: 'Parse'
})
.exclude('xmlhttprequest')
.ignore('_process')
.bundle();
stream.on('end', () => {
cb();
});
return stream.pipe(source('parse.js'))
.pipe(derequire())
.pipe(insert.prepend(DEV_HEADER))
.pipe(gulp.dest('./dist'));
});
gulp.task('browserify-weapp', function(cb) {
const stream = browserify({
builtins: ['_process', 'events'],
entries: 'lib/weapp/Parse.js',
standalone: 'Parse'
})
.exclude('xmlhttprequest')
.ignore('_process')
.bundle();
stream.on('end', () => {
cb();
});
return stream.pipe(source('parse.weapp.js'))
.pipe(derequire())
.pipe(insert.prepend(DEV_HEADER))
.pipe(gulp.dest('./dist'));
});
gulp.task('minify', function() {
return gulp.src('dist/parse.js')
.pipe(uglify())
.pipe(insert.prepend(FULL_HEADER))
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('./dist'))
});
gulp.task('minify-weapp', function() {
return gulp.src('dist/parse.weapp.js')
.pipe(uglify())
.pipe(insert.prepend(FULL_HEADER))
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('./dist'))
});
gulp.task('watch', function() {
if (BUILD === 'browser') {
const watcher = gulp.watch('src/*.*(js|ts)', { ignoreInitial: false }, gulp.series('compile', 'browserify', 'minify'));
watcher.on('add', function(path) {
console.log(`File ${path} was added`);
});
watcher.on('change', function(path) {
console.log(`File ${path} was changed`);
});
return watcher;
}
return compileTask(watch('src/*.*(js|ts)', { ignoreInitial: false, verbose: true }));
});