const { src, dest, watch, parallel } = require('gulp'); const ts = require('gulp-typescript'); const fs = require('fs'); const sourcemaps = require('gulp-sourcemaps'); const minimist = require('minimist'); const tsConfig = require('./tsconfig.json'); const webpack = require('webpack-stream'); const webpackConfig = require('./webpack.config'); const argv = minimist(process.argv); const tsProject = ts.createProject('tsconfig.json'); function buildFrontend() { let config = webpackConfig; if (argv.dev) { config = { ...config, ...{ mode: 'development' } }; } if (argv.watch) { config = { ...config, ...{ watch: true } }; } return src(config.entry) .pipe(webpack(config)) .pipe(dest(config.output.path)); } function copyIndexHtml(cb) { let indexHtml = 'index.prod.html'; if (argv.dev) { indexHtml = 'index.dev.html'; } fs.copyFileSync(indexHtml, 'index.html'); cb(); } function transpileTypescript() { return tsProject .src() .pipe(sourcemaps.init()) .pipe(tsProject()) .pipe(sourcemaps.write('.')) .pipe(dest('.')); } function build(cb) { if (argv.watch) { watch(tsConfig.include, { ignoreInitial: false }, transpileTypescript); watch('index.*.html', { ignoreInitial: false }, copyIndexHtml); buildFrontend(); } else { parallel(transpileTypescript, copyIndexHtml, buildFrontend)(cb); } } build.flags = { '--dev': 'builds in development mode', '--watch': 'watches the files', }; exports.build = build;