UNPKG

1.92 kBJavaScriptView Raw
1module.exports = function createTask(crafty, bundle, StreamHandler) {
2 return cb => {
3 // Init
4 const stream = new StreamHandler(
5 bundle.source,
6 crafty.config.destination_js +
7 (bundle.directory ? `/${bundle.directory}` : ""),
8 cb
9 );
10
11 // Avoid compressing if it's already at the latest version
12 if (crafty.isWatching()) {
13 const newer = require("gulp-newer");
14 stream.add(newer(crafty.config.destination_js + bundle.destination));
15 }
16
17 // Linting
18 const {
19 toTempFile
20 } = require("@swissquote/crafty-preset-eslint/src/eslintConfigurator");
21 const eslint = require("gulp-eslint7");
22 stream.add(eslint(toTempFile(crafty.config.eslint))).add(eslint.format());
23
24 // Fail the build if we have linting
25 // errors and we build directly
26 if (!crafty.isWatching()) {
27 stream.add(
28 eslint.results(results => {
29 const count = results.errorCount;
30 if (count) {
31 const message = `ESLint failed with ${count}${
32 count === 1 ? " error" : " errors"
33 }`;
34 cb(new crafty.Information(message));
35 }
36 })
37 );
38 }
39
40 const babel = require("gulp-babel");
41 const babelConfigurator = require("@swissquote/babel-preset-swissquote/configurator-gulp");
42 const babelOptions = babelConfigurator(crafty, bundle);
43
44 stream.add(babel(babelOptions));
45
46 // Process
47 const sourcemaps = require("gulp-sourcemaps");
48 stream.add(sourcemaps.init({ loadMaps: true }));
49
50 if (bundle.concat) {
51 const concat = require("gulp-concat");
52 stream.add(concat(bundle.destination));
53 }
54
55 if (crafty.getEnvironment() === "production") {
56 const terser = require("gulp-terser");
57 stream.add(terser({ ...crafty.config.terser, sourceMap: {} }));
58 }
59
60 stream.add(sourcemaps.write("./"));
61
62 // Save
63 return stream.generate();
64 };
65};