let gulp = require('gulp');
let sourcemaps = require('gulp-sourcemaps');
let sass = require('gulp-sass');
let rollup = require('rollup');
let rollupAppConfig = require('./rollup.config.app.js');
let rollupTestConfig = require('./rollup.config.test.js');
let workboxBuild = require('workbox-build');

gulp.task('assets', () => {
  return gulp.src('app/assets/**/*')
    .pipe(gulp.dest('public'));
});
gulp.task('assets:watch', () => {
  return gulp.watch('app/assets/**/*', gulp.series('assets', 'sw'));
});

gulp.task('sass', () => {
  return gulp.src('app/styles/index.scss')
    .pipe(sourcemaps.init())
    .pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('public/styles'));
});
gulp.task('sass:watch', () => {
  return gulp.watch('app/styles/**/*.scss', gulp.series('sass', 'sw'));
});

gulp.task('rollup:app', () => {
  return rollup.rollup(rollupAppConfig).then((bundle) => {
    return bundle.write(rollupAppConfig.output);
  });
});
gulp.task('rollup:test', () => {
  return rollup.rollup(rollupTestConfig).then((bundle) => {
    return bundle.write(rollupTestConfig.output);
  });
});
gulp.task('rollup:watch', () => {
  return gulp.watch(
    ['app/scripts/**/*.js', 'test/**/*.js'],
    gulp.series('rollup', 'sw')
  );
});
gulp.task('rollup', gulp.parallel(
  'rollup:app',
  'rollup:test'
));


gulp.task('sw', () => {
  return workboxBuild.injectManifest({
    globDirectory: 'public',
    globPatterns: [
      '**\/*.{html,js,css,png}'
    ],
    swSrc: 'app/scripts/service-worker.js',
    swDest: 'public/service-worker.js'
   }).then(({ warnings }) => {
    warnings.forEach(console.warn);
  });
});

gulp.task('build', gulp.series(
  gulp.parallel(
    'assets',
    'sass',
    'rollup'
  ),
  'sw'
));
gulp.task('build:watch', gulp.series(
  'build',
  gulp.parallel(
    'assets:watch',
    'sass:watch',
    'rollup:watch'
  )
));

@@ -1506,13 +1506,8 @@
 /*.%7B
-html,
 js,c
@@ -1520,16 +1520,446 @@
 %7D'%0A    %5D
+,%0A    // Precaching index.html using templatedUrls fixes a %22Response served by%0A    // service worker has redirections%22 error on iOS 12; see%0A    // %3Chttps://github.com/v8/v8.dev/issues/4%3E and%0A    // %3Chttps://github.com/v8/v8.dev/pull/7%3E%0A    templatedUrls: %7B%0A      // '.' must be used instead of '/' because the app is not served from the%0A      // root of the domain (but rather, from a subdirectory)%0A      '.': %5B'index.html'%5D%0A    %7D
 ,%0A    sw

Fix redirect error on iOS mobile Safari