const gulp = require('gulp');
const babel = require('gulp-babel');
const clean = require('gulp-clean');
const sass = require('gulp-sass');
const concatCss = require('gulp-concat-css');
const path = require('path');
const shell = require('gulp-shell');
const pkg = require('./package.json');
const generateImportTransformer = require('./scripts/generate-import-transformer/generateImportTransformer');
const generateDocs = require('./scripts/generate-docs/generateDocs');

const jsSource = [
    'src/**/*.{js,jsx}',
    '!src/**/*.{stories,spec,test}.{js,jsx}',
];
const cssSource = 'src/**/*.{css,scss}';

/** ======================
 ========= CLEAN =========
 ====================== */

gulp.task('clean', () =>
    gulp
        .src(['dist/', 'split-css/', 'lib/'], { allowEmpty: true })
        .pipe(clean())
);

/** ======================
 ========== ESM ==========
 ====================== */

const esmDestination = path.dirname(pkg.module);

gulp.task('transpile-esm', () =>
    gulp
        .src(jsSource)
        .pipe(
            babel({
                presets: [['./babelPreset.js', { cssImports: 'rename' }]],
            })
        )
        .pipe(gulp.dest(esmDestination))
);

gulp.task('compile-scss-esm', () =>
    gulp.src(cssSource).pipe(sass()).pipe(gulp.dest(esmDestination))
);

gulp.task('build-esm', gulp.parallel('transpile-esm', 'compile-scss-esm'));

/** ======================
 ========== CJS ==========
 ====================== */

const cjsDestination = path.dirname(pkg.main);

gulp.task('transpile-cjs', () =>
    gulp
        .src(jsSource)
        .pipe(
            babel({
                presets: [
                    ['./babelPreset.js', { cjs: true, cssImports: 'rename' }],
                ],
            })
        )
        .pipe(gulp.dest(cjsDestination))
);

gulp.task('compile-scss-cjs', () =>
    gulp.src(cssSource).pipe(sass()).pipe(gulp.dest(cjsDestination))
);

gulp.task('build-cjs', gulp.parallel('transpile-cjs', 'compile-scss-cjs'));

/** ======================
 ===== CJS CSS-SPLIT =====
 ====================== */

gulp.task('transpile-cjs-split-css', () =>
    gulp
        .src(jsSource)
        .pipe(
            babel({
                presets: [
                    ['./babelPreset.js', { cjs: true, cssImports: 'remove' }],
                ],
            })
        )
        .pipe(gulp.dest('dist/cjs-split-css/'))
);

gulp.task('bundle-css', () =>
    gulp
        .src(cssSource)
        .pipe(sass())
        .pipe(concatCss('styles.css'))
        .pipe(gulp.dest('dist/'))
);

gulp.task(
    'build-cjs-split-css',
    gulp.parallel('transpile-cjs-split-css', 'bundle-css')
);

/** ======================
 ========== UMD ==========
 ====================== */

gulp.task('build-umd', shell.task('rollup -c', { quiet: true }));

/** ======================
 ===== GENERATE DOCS =====
 ====================== */

gulp.task('generate-docs', generateDocs);

/** ======================
 = resolveAbsoluteImport =
 ====================== */

gulp.task('compile-resolve-import', generateImportTransformer);

/** ======================
 ========= BUILD =========
 ====================== */

gulp.task(
    'build',
    gulp.series(
        'clean',
        gulp.parallel(
            gulp.series('build-esm', 'compile-resolve-import'),
            'build-cjs',
            'build-cjs-split-css',
            'build-umd',
            'generate-docs'
        )
    )
);

@@ -1370,24 +1370,566 @@
 ss-esm'));%0A%0A
+/** ======================%0A ====== ESM TO lib/ ======%0A ====================== */%0A%0Agulp.task('transpile-esm-to-lib', () =%3E%0A    gulp%0A        .src(jsSource)%0A        .pipe(%0A            babel(%7B%0A                presets: %5B%5B'./babelPreset.js', %7B cssImports: 'rename' %7D%5D%5D,%0A            %7D)%0A        )%0A        .pipe(gulp.dest('lib/'))%0A);%0A%0Agulp.task('compile-scss-esm-to-lib', () =%3E%0A    gulp.src(cssSource).pipe(sass()).pipe(gulp.dest('lib/'))%0A);%0A%0Agulp.task(%0A    'build-esm-to-lib',%0A    gulp.parallel('transpile-esm-to-lib', 'compile-scss-esm-to-lib')%0A);%0A%0A
 /** ========
@@ -3832,16 +3832,48 @@
 port'),%0A
+            'build-esm-to-lib',%0A
         

Add back lib/ build because too many packages imported from there