/* eslint-disable import/no-commonjs */

const gulp = require('gulp');
const watch = require('gulp-watch');
const plumber = require('gulp-plumber');
const babel = require('gulp-babel');
const del = require('del');

const FRAMEWORK_BABEL_PRESET = `${__dirname}/src/internals/babel/internal-babel-preset`;

// ====== Lib build ======
// Transpile to ES5

function cleanLib() {
  return del(['./lib']);
}

// ['clean-lib']
function copyLib() {
  return gulp
    .src('./src/**/*')
    .pipe(gulp.dest('./lib'));
}

function compileLib() {
  return gulp.src('./src/**/*.js')
    .pipe(plumber())
    .pipe(babel({
      presets: [
        [FRAMEWORK_BABEL_PRESET, { buildEsModules: true }],
      ],
    }))
    .pipe(plumber.stop())
    .pipe(gulp.dest('./lib'));
}

const buildLib = gulp.series(cleanLib, copyLib, compileLib);

// ====== ES  build ======
// Like Lib but doesn't transform import statements.

function cleanEs() {
  return del(['./es']);
}

function copyEs() {
  return gulp
    .src('./src/**/*')
    .pipe(gulp.dest('./es'));
}

function compileEs() {
  return gulp.src('./src/**/*.js')
    .pipe(plumber())
    .pipe(babel({
      presets: [
        [FRAMEWORK_BABEL_PRESET, { buildEsModules: false }],
      ],
    }))
    .pipe(plumber.stop())
    .pipe(gulp.dest('./es'));
}

const buildEs = gulp.series(cleanEs, copyEs, compileEs);

// ====== Common ======

const buildAll = gulp.parallel([buildEs, buildLib]);

gulp.task('build', buildAll);

gulp.task('build:watch', gulp.series(buildAll, () => {
  return watch('./src/**/*', buildAll);
}));

@@ -34,16 +34,26 @@
 njs */%0A%0A
+// @flow%0A%0A
 const gu
@@ -466,34 +466,52 @@
     .src('./src/
-**
+!(gatsby-theme-docz)
 /*')%0A    .pipe(g
@@ -574,34 +574,52 @@
 gulp.src('./src/
-**
+!(gatsby-theme-docz)
 /*.js')%0A    .pip
@@ -1044,18 +1044,36 @@
 ('./src/
-**
+!(gatsby-theme-docz)
 /*')%0A   
@@ -1150,18 +1150,36 @@
 ('./src/
-**
+!(gatsby-theme-docz)
 /*.js')%0A

exclude docz sources from compilation