forked from jsforce/jsforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
144 lines (131 loc) · 3.93 KB
/
gulpfile.babel.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
import gulp from 'gulp';
import fs from 'fs';
import path from 'path';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import sourcemaps from 'gulp-sourcemaps';
import rename from 'gulp-rename';
import terser from 'gulp-terser';
let coreModules;
const commonModules = [
'inherits', 'util', 'events', 'lodash/core', 'readable-stream', 'multistream'
];
const apiModules = [
'analytics', 'apex', 'bulk', 'chatter', 'metadata', 'soap', 'streaming', 'tooling'
];
const scriptTasks = [{
name: 'build:scripts:all',
entries: './index.js',
standalone: 'jsforce',
output: 'jsforce',
}, {
name: 'build:scripts:core',
entries: './core.js',
standalone: 'jsforce',
output: 'jsforce-core',
}]
.concat(
apiModules.map((api) => ({
name: `build:scripts:api:${api}`,
entries: `./lib/api/${api}.js`,
standalone: `jsforce.modules.api.${api[0].toUpperCase() + api.substring(1)}`,
output: `jsforce-api-${api}`,
transform: () => {
const swappingModules = commonModules.concat(coreModules);
return [
[ 'require-swapper',
{
baseDir: './lib/',
fn: 'window.jsforce.require',
modules: swappingModules,
}
]
]
}
}))
);
scriptTasks.forEach(({ name, entries, standalone, transform = () => [], output }) => {
gulp.task(name, () => {
return browserify({
entries,
standalone,
transform: transform(),
debug: true,
}).bundle()
.pipe(source(output + '.js'))
.pipe(gulp.dest('./build'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(rename(output + '.min.js'))
.pipe(terser())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./build'));
});
});
gulp.task('build:required', (cb) => {
fs.readdir('./lib', function(err, files) {
if (err) { return cb(err); }
coreModules =
files.filter((f) => /\.js$/.test(f) && !/^(jsforce|require|_required)\.js$/.test(f))
.map((f) => './' + f.replace(/\.js$/, ''));
var requireFile = './lib/_required.js';
var code = [
'// This file content is dynamically created in build script',
'"use strict";',
'module.exports = {',
commonModules.concat(coreModules)
.map((module) => ` '${module}': require('${module}')`)
.join(',\n'),
'};'
].join('\n');
fs.writeFileSync(requireFile, code, 'utf8');
cb();
});
});
gulp.task('build:scripts', gulp.parallel(...scriptTasks.map(({ name }) => name)));
gulp.task('build', gulp.series('build:required', 'build:scripts'));
const testScripts =
fs.readdirSync('./test')
.filter((filename) => /\.test\.js$/.test(filename))
.map((filename) => path.join('./test', filename));
gulp.task('build:test', () => {
return browserify({
entries: testScripts,
debug: true,
transform: [
'espowerify',
'envify',
[ 'require-swapper',
{
baseDir: './lib/',
fn: 'window.jsforce.require',
modules: ['./*'],
}
]
],
}).bundle()
.pipe(source('./test.js'))
.pipe(gulp.dest('./build'));
});
if (require('./package.json').devDependencies['require-swapper']) {
let shouldWarn = false;
gulp.on('error', ({ error }) => {
if (error.stack.includes('/esprima/') && error.stack.includes('/require-swapper/')) {
// Only warn once.
shouldWarn = true;
}
});
process.on('exit', () => {
if (shouldWarn) {
console.error(`
WARNING: mixmaxhq/jsforce has been altered to pin the version of esprima that require-swapper
depends on to 4.x. This change was made in https://github.com/mixmaxhq/jsforce/pull/80, and may
break if the require-swapper dependency gets altered. The error message indicates that
require-swapper likely broke on our use of ES6 features.
`);
}
});
}
gulp.task('build:all', gulp.parallel('build', 'build:test'));
gulp.task('default', gulp.series('build'));