-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
executable file
·365 lines (338 loc) · 8.51 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import fs from 'fs';
import gulp from 'gulp';
import { merge } from 'event-stream';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import preprocessify from 'preprocessify';
import gulpif from 'gulp-if';
const $ = require('gulp-load-plugins')();
// Get the vars that are past in the gulp run
const production = process.env.NODE_ENV === 'production';
const target = process.env.TARGET || 'chrome';
const environment = process.env.NODE_ENV || 'development';
// Get from the consts the specific JSON file. So these configs are now in these consts
const generic = JSON.parse(fs.readFileSync(`./config/${environment}.json`));
const specific = JSON.parse(fs.readFileSync(`./config/${target}.json`));
const context = Object.assign({}, generic, specific);
const manifest = {
// Add this to the manifest file So the livereload scritpt is also included in the development
dev: {
env: 'dev',
background: {
scripts: ['scripts/livereload.js', 'scripts/background/index.js'],
},
},
firefox: {
applications: {
gecko: {
id: 'layernotes@uncinc.nl',
strict_min_version: '48.0',
},
},
},
chrome: {
shortName: '__MSG_appDescription__',
update_url: 'https://github.com/uncinc/layer-notes/releases.atom',
},
};
// this are the sizes of the icons
const pluginIconConfig = [
{
width: 16,
rename: {
suffix: '-16',
},
},
{
width: 19,
rename: {
suffix: '-19',
},
},
{
width: 38,
rename: {
suffix: '-38',
},
},
{
width: 48,
rename: {
suffix: '-48',
},
},
{
width: 96,
rename: {
suffix: '-96',
},
},
{
width: 128,
rename: {
suffix: '-128',
},
},
];
// Tasks
gulp.task('clean', () => {
return pipe(`./build/${target}`, $.clean());
});
// Build the exention
gulp.task('build', (cb) => {
$.runSequence(
'clean',
'styles',
'icons',
'sprites',
'ext',
'copy-fonts',
'copy-react',
'copy-react-dom',
cb,
);
});
gulp.task('watch', ['build'], () => {
// start live reload
$.livereload.listen();
gulp.watch(['./src/icons/**/*']).on('change', () => {
$.runSequence('icons', $.livereload.reload);
});
gulp.watch(['./src/scripts/**/*']).on('change', () => {
// $.runSequence('lint.js');
$.runSequence('js', $.livereload.reload);
});
gulp.watch(['./src/styles/**/*']).on('change', () => {
$.runSequence('styles', $.livereload.reload);
});
});
// The defualt task
gulp.task('default', ['build']);
// Merge the vars and the manifest file
gulp.task('ext', ['manifest', 'js'], () => {
return mergeAll(target);
});
// ----------------
// IMAGES
// ________________
gulp.task('icons', () => {
return gulp
.src('./src/icons/*.{jpg,png}')
.pipe(
$.responsive(
{
'*': pluginIconConfig
},
{
quality: 95,
progressive: true,
compressionLevel: 6,
withMetadata: false
}
)
)
.pipe(gulp.dest(`./build/${target}/icons`));
});
// ----------------
// sprites
// Create a sprite form al the SVG's in the folder
// ________________
gulp.task('sprites', () => {
return gulp
.src('./src/images/sprite/*.svg')
.pipe(
$.svgSprites({
cssFile: '../../../src/styles/modules/_sprites.scss', // Place the file here
// Add the exention css file path to the path of the svg in the generated css file
svgPath: `${context.cssFilePath}images/%f`,
pngPath: `${context.cssFilePath}images/%f`,
common: 'ln-icon', // this is the main class
selector: 'ln-%f', // Add the name space 'ln-' in the generated icon classes
svg: {
sprite: 'sprite.svg' //t his is the name of the svg
},
baseSize: 16, // the base size in px
preview: false // Do not generate a previeuw html
})
)
.pipe(gulp.dest(`./build/${target}/images`));
});
// -----------------
// COMMON
// -----------------
gulp.task('js', () => {
return buildJS(target);
});
gulp.task('lint.js', () => {
// generate a zip
return (
gulp
.src([
'./src/scripts/background/*.js',
'./src/scripts/components/**/*.js',
'./src/scripts/config/*.js',
'./src/scripts/utils/*.js'
])
.pipe($.eslint())
// $.eslint.format() outputs the lint results to the console.
// Alternatively use $.eslint.formatEach() (see Docs).
.pipe($.eslint.format())
);
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
// .pipe($.eslint.failAfterError());
});
gulp.task('styles', () => {
return gulp
.src([`src/styles/index.${target}.scss`])
.pipe($.plumber())
.pipe(
$.sass
.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
})
.on('error', $.sass.logError)
)
.pipe($.concat('index.css'))
.pipe(gulp.dest(`build/${target}/styles`));
});
// change the manifest
gulp.task('manifest', () => {
return gulp
.src('./manifest.json') // generate the manifest file
.pipe(
gulpif(
!production,
$.mergeJson({
fileName: 'manifest.json',
jsonSpace: ' '.repeat(2),
endObj: manifest.dev
})
)
)
.pipe(
gulpif(
target === 'firefox',
$.mergeJson({
fileName: 'manifest.json',
jsonSpace: ' '.repeat(2),
endObj: manifest.firefox
})
)
)
.pipe(gulp.dest(`./build/${target}`));
});
// -----------------
// COPY REACT
// -----------------
// Copy react.js and react-dom.js to assets/js/src/vendor
// only if the copy in node_modules is 'newer'
gulp.task('copy-react', () => {
return gulp
.src('node_modules/react/cjs/react.development.js')
.pipe($.newer('./src/scripts/vendor/react.min.js'))
.pipe(gulp.dest('./src/scripts/vendor/'));
});
gulp.task('copy-react-dom', () => {
return gulp
.src('node_modules/react-dom/dist/react-dom.min.js')
.pipe($.newer('./src/scripts/vendor/react-dom.min.js'))
.pipe(gulp.dest('./src/scripts/vendor/'));
});
// copy the fonts to the right folder
gulp.task('copy-fonts', () => {
return gulp
.src('./src/fonts/**/*.{eot,svg,ttf,woff,woff2}')
.pipe(gulp.dest(`./build/${target}/fonts`));
});
// -----------------
// DIST
// -----------------
gulp.task('dist', (cb) => {
// generate a zip
$.runSequence('build', 'zip', cb);
});
gulp.task('zip', () => {
return pipe(`./build/${target}/**/*`, $.zip(`${target}.zip`), './dist');
});
// Helpers
function pipe(src, ...transforms) {
return transforms.reduce((stream, transform) => {
const isDest = typeof transform === 'string';
return stream.pipe(isDest ? gulp.dest(transform) : transform);
}, gulp.src(src));
}
function mergeAll(dest) {
return merge(
// pipe('./src/icons/**/*', `./build/${dest}/icons`),
pipe(['./src/_locales/**/*'], `./build/${dest}/_locales`),
pipe([`./src/images/${target}/**/*`], `./build/${dest}/images`),
pipe(['./src/images/shared/**/*'], `./build/${dest}/images`),
pipe(['./src/**/*.html'], `./build/${dest}`)
);
}
function buildJS(target) {
const files = [
{
source: 'background/index.js',
export: 'background/index.js'
},
{
source: 'contentscript/index.js',
export: 'contentscript/index.js'
},
{
source: 'options.js',
export: 'options.js'
},
{
source: 'livereload.js',
export: 'livereload.js'
}
];
let tasks = files.map((file) => {
return browserify({
entries: `src/scripts/${file.source}`,
debug: !production,
})
.transform('babelify', {
presets: ['es2015', 'stage-2', 'react'],
plugins: [
'transform-class-properties',
'transform-es2015-modules-commonjs',
],
})
.transform(preprocessify, {
includeExtensions: ['.js', '.jsx'],
context,
})
.bundle()
.pipe(source(file.source))
.pipe(buffer())
.pipe(
gulpif(
!production,
$.sourcemaps.init({
loadMaps: true,
}),
),
)
.pipe(gulpif(!production, $.sourcemaps.write('./')))
.pipe(
gulpif(
production,
$.uglify({
mangle: false,
output: {
ascii_only: true,
},
}),
),
)
.pipe(gulp.dest(`build/${target}/scripts`));
});
return merge.apply(null, tasks);
}