aboutsummaryrefslogtreecommitdiffstats
path: root/gulpfile.babel.ts
blob: 5d351071507b6a7b6f9c8051392579d137c9ccf4 (plain) (blame)
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
import gulp from 'gulp';
import gulpIf from 'gulp-if';
import babel from 'gulp-babel';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import csso from 'gulp-csso';
import terser from 'gulp-terser';
import htmlMin from 'gulp-htmlmin';
import connect from 'gulp-connect';
import sassVariables from 'gulp-sass-variables';
import { removeSync, outputJson } from 'fs-extra';
import kebabCase from 'kebab-case';
import hexRgb from 'hex-rgb';
import ts from 'gulp-typescript';

import * as buildInfo from 'preval-build-info';
import config from './package.json';

import rawStyleConfig from './scripts/theme/default/legacy';

import 'dotenv/config';

const sass = gulpSass(dartSass);

const isDevBuild = process.env.NODE_ENV === 'development';

const getTargetEnv = isDevBuild ? 'development' : 'production';

const tsProject = ts.createProject('tsconfig.json');

const styleConfig = Object.keys(rawStyleConfig).map(key => {
  const isHex = /^#[\da-f]{6}$/i.test(rawStyleConfig[key]);
  return {
    [`$raw_${kebabCase(key)}`]: isHex
      ? hexRgb(rawStyleConfig[key], { format: 'array' }).splice(0, 3).join(',')
      : rawStyleConfig[key],
  };
});

const paths = {
  src: 'src',
  dest: 'build',
  tmp: '.tmp',
  dist: 'out',
  package: `out/${config.version}`,
  buildInfoDestFile: 'build/buildInfo.json',
  recipes: {
    src: 'recipes/archives/*.tar.gz',
    dest: 'build/recipes/',
  },
  recipeInfo: {
    src: 'recipes/*.json',
    dest: 'build/recipes/',
  },
  html: {
    src: 'src/**/*.html',
    dest: 'build/',
    watch: 'src/**/*.html',
  },
  styles: {
    src: [
      'src/styles/main.scss',
      'src/styles/vertical.scss',
      'src/styles/animations.scss',
    ],
    dest: 'build/styles',
    watch: 'src/styles/**/*.scss',
  },
  javascript: {
    src: ['src/**/*.js', 'src/**/*.jsx'],
    dest: 'build/',
    watch: ['src/**/*.js', 'src/**/*.jsx'],
  },
  typescript: {
    src: ['src/**/*.ts', 'src/**/*.tsx'],
    dest: 'build/',
    watch: ['src/**/*.ts', 'src/**/*.tsx'],
  },
};

const clean: (done: any) => void = done => {
  removeSync(paths.tmp);
  removeSync(paths.dest);
  removeSync(paths.dist);

  done();
};

function mvSrc() {
  return gulp
    .src(
      [
        `${paths.src}/*`,
        `${paths.src}/*/**`,
        `!${paths.javascript.watch[0]}`,
        `!${paths.typescript.watch[0]}`,
        `!${paths.src}/styles/**`,
      ],
      { since: gulp.lastRun(mvSrc) },
    )
    .pipe(gulp.dest(paths.dest));
}

function mvPackageJson() {
  return gulp.src(['./package.json']).pipe(gulp.dest(paths.dest));
}

function BuildInfo() {
  const buildInfoData = {
    timestamp: buildInfo.timestamp,
    gitHashShort: buildInfo.gitHashShort,
    gitBranch: buildInfo.gitBranch,
  };
  return outputJson(paths.buildInfoDestFile, buildInfoData);
}

function html() {
  return gulp
    .src(paths.html.src, { since: gulp.lastRun(html) })
    .pipe(
      gulpIf(
        !isDevBuild,
        htmlMin({
          // Only minify in production to speed up dev builds
          collapseWhitespace: true,
          removeComments: true,
        }),
      ),
    )
    .pipe(gulp.dest(paths.html.dest))
    .pipe(connect.reload());
}

function styles(): NodeJS.ReadWriteStream {
  return gulp
    .src(paths.styles.src)
    .pipe(
      sassVariables(
        Object.assign(
          {
            $env: getTargetEnv,
          },
          ...styleConfig,
        ),
      ),
    )
    .pipe(
      sass({
        includePaths: ['./node_modules', '../node_modules'],
      }).on('error', sass.logError),
    )
    .pipe(
      gulpIf(
        !isDevBuild,
        csso({
          // Only minify in production to speed up dev builds
          restructure: false, // Don't restructure CSS, otherwise it will break the styles
        }),
      ),
    )
    .pipe(gulp.dest(paths.styles.dest))
    .pipe(connect.reload());
}

function processJavascript() {
  return gulp
    .src(paths.javascript.src, { since: gulp.lastRun(processJavascript) })
    .pipe(
      babel({
        comments: false,
      }),
    )
    .pipe(gulpIf(!isDevBuild, terser())) // Only uglify in production to speed up dev builds
    .pipe(gulp.dest(paths.javascript.dest))
    .pipe(connect.reload());
}

function processTypescript() {
  return gulp
    .src(paths.typescript.src, { since: gulp.lastRun(processTypescript) })
    .pipe(tsProject())
    .js.pipe(
      babel({
        comments: false,
      }),
    )
    .pipe(gulpIf(!isDevBuild, terser())) // Only uglify in production to speed up dev builds
    .pipe(gulp.dest(paths.typescript.dest))
    .pipe(connect.reload());
}

function watch() {
  gulp.watch(paths.styles.watch, styles);

  gulp.watch([paths.src], mvSrc);

  gulp.watch(paths.javascript.watch, processJavascript);
  gulp.watch(paths.typescript.watch, processTypescript);
}

function webserver() {
  connect.server({
    root: paths.dest,
    livereload: true,
  });
}

function recipes() {
  return gulp
    .src(paths.recipes.src, { since: gulp.lastRun(recipes) })
    .pipe(gulp.dest(paths.recipes.dest));
}

function recipeInfo() {
  return gulp
    .src(paths.recipeInfo.src, { since: gulp.lastRun(recipeInfo) })
    .pipe(gulp.dest(paths.recipeInfo.dest));
}

export const build = gulp.series(
  clean,
  gulp.parallel(mvSrc, mvPackageJson, BuildInfo),
  gulp.parallel(
    html,
    processJavascript,
    processTypescript,
    styles,
    recipes,
    recipeInfo,
  ),
);

export const dev = gulp.series(build, gulp.parallel(webserver, watch));