-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
180 lines (162 loc) · 4.38 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
/* eslint-env node */
'use strict';
const awspublish = require('gulp-awspublish');
const babel = require('gulp-babel');
const gulp = require('gulp');
const path = require('path');
const rename = require('gulp-rename');
const template = require('gulp-template');
const uglify = require('gulp-uglify');
const umd = require('gulp-umd');
const {version} = require('./package');
const majorVersion = version.slice(0, version.indexOf('.'));
/**
* Add package version to README.
*/
gulp.task('template:readme', function() {
return gulp
.src('README.mdt')
.pipe(template({version, majorVersion}))
.pipe(rename({extname: '.md'}))
.pipe(gulp.dest('.'));
});
/**
* UMD wrap sdk.js
*/
gulp.task('build:umd', function() {
return gulp
.src('src/sdk.js')
.pipe(
umd({
// CommonJS export name
exports: function() {
return 'Smartcar';
},
// Global namespace for Web.
namespace: function() {
return 'Smartcar';
},
// returnExports template with istanbul ignore
template: path.join(__dirname, 'build/returnExports.js'),
}),
)
.pipe(gulp.dest('dist/umd'));
});
/**
* Build sdk.js for npm publishing.
*/
gulp.task(
'build:npm',
gulp.series('build:umd', function buildNPM() {
return gulp
.src('dist/umd/sdk.js')
.pipe(babel())
.pipe(gulp.dest('dist/npm'));
}),
);
/**
* Build redirect for CDN publishing.
*/
gulp.task('build:cdn:redirect', function() {
return gulp
.src('src/redirect.js')
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest(`dist/cdn/v${majorVersion}`));
});
/**
* Build SDK for CDN publishing.
*/
gulp.task(
'build:cdn:sdk',
gulp.series('build:umd', function buildCDNSdk() {
return gulp
.src('dist/umd/sdk.js')
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest(`dist/cdn/${version}`));
}),
);
/**
* Build all JS for CDN publishing.
*/
gulp.task('build:cdn:js', gulp.parallel('build:cdn:redirect', 'build:cdn:sdk'));
/**
* Build HTML for CDN publishing
*/
gulp.task('build:cdn:html', function() {
return gulp
.src('src/redirect.html')
.pipe(template({majorVersion}))
.pipe(rename('redirect')) // Removes .html extension
.pipe(gulp.dest(`dist/cdn/v${majorVersion}`));
});
/**
* Build all tasks for CDN publishing.
*/
gulp.task('build:cdn', gulp.parallel('build:cdn:js', 'build:cdn:html'));
/**
* Build all tasks for CDN and npm publishing.
*
* dist/
* ├── cdn
* │ ├── v2 // Only major versions of redirect are exposed
* │ │ ├── redirect // HTML file
* │ │ └── redirect.js // Referenced by both the new and old HTML files
* │ └── 2.2.0 // Full semver versions are exposed for SDK releases
* │ └── sdk.js
* ├── npm
* │ └── sdk.js
* └── umd
* └── sdk.js
*/
gulp.task('build', gulp.parallel('build:cdn', 'build:npm'));
// Setup AWS publisher to the Smartcar CDN.
const publisher = awspublish.create({
region: 'us-west-2',
params: {Bucket: 'smartcar-production-javascript-sdk'},
});
/**
* Publish HTML to the CDN in the major version folder (e.g. /v2/redirect).
*
* We strip the `.html` extension from our html file so add content-type header
* to identify the file as `text/html`.
*/
gulp.task('publish:cdn:html', function() {
return gulp
.src(`dist/cdn/v${majorVersion}/redirect`)
.pipe(rename({prefix: `v${majorVersion}/`}))
.pipe(publisher.publish({'content-type': 'text/html'}))
.pipe(awspublish.reporter());
});
/**
* Publish JS to the CDN in the major version folder (e.g. /v2/redirect.js).
*/
gulp.task('publish:cdn:redirect', function() {
return gulp
.src(`dist/cdn/v${majorVersion}/*.js`)
.pipe(rename({prefix: `v${majorVersion}/`}))
.pipe(publisher.publish())
.pipe(awspublish.reporter());
});
/**
* Publish SDK to the CDN in the version folder (e.g. /2.2.0/sdk.js).
*/
gulp.task('publish:cdn:sdk', function() {
return gulp
.src(`dist/cdn/${version}/sdk.js`)
.pipe(rename({prefix: `${version}/`}))
.pipe(publisher.publish())
.pipe(awspublish.reporter());
});
/**
* Publish JS to the CDN.
*/
gulp.task(
'publish:cdn:js',
gulp.parallel('publish:cdn:sdk', 'publish:cdn:redirect'),
);
/**
* Publish all files to the CDN.
*/
gulp.task('publish:cdn', gulp.parallel('publish:cdn:js', 'publish:cdn:html'));