const { dest, series, src } = require('gulp'); const del = require('del'); const replace = require('gulp-replace'); const webpack = require('webpack-stream'); const PACKAGE = require('./package.json'); const FOLDER_SOURCE = './source' const FOLDER_BUILD = './build'; let mode = 'development'; function clean(cb) { del(FOLDER_BUILD); cb(); } const eventsLoaderOutputPath = PACKAGE.name + '/front/events-loader'; const eventsLoaderInputPath = FOLDER_SOURCE + '/' + PACKAGE.name + '/front/events-loader.js'; function bundleFrontend() { return src(FOLDER_SOURCE + '/' + PACKAGE.name + '/front/events-loader.js') .pipe(webpack({ mode, entry: { [eventsLoaderOutputPath]: eventsLoaderInputPath, }, output: { filename: '[name].js', }, })) .pipe(dest(FOLDER_BUILD)); } function copyBackend() { return src([ FOLDER_SOURCE + '/**/*.php', FOLDER_SOURCE + '/**/*.txt' ]) .pipe(dest(FOLDER_BUILD)); } function injectMetadata() { return src([ FOLDER_BUILD + '/' + eventsLoaderOutputPath + '.js', FOLDER_BUILD + '/' + PACKAGE.name + '/' + PACKAGE.name + '.php', FOLDER_BUILD + '/' + PACKAGE.name + '/includes/constants.php', FOLDER_BUILD + '/' + PACKAGE.name + '/readme.txt' ], { base: './' }) .pipe(replace('', PACKAGE.author.name)) .pipe(replace('', PACKAGE.author.url)) .pipe(replace('', PACKAGE.description)) .pipe(replace('', PACKAGE.funding.url)) .pipe(replace('', PACKAGE.license)) .pipe(replace('', PACKAGE.additionalDetails.wordpressMinimumVersion)) .pipe(replace('', PACKAGE.name)) .pipe(replace('', PACKAGE.additionalDetails.niceName)) .pipe(replace('', PACKAGE.additionalDetails.phpMinimumVersion)) .pipe(replace('', PACKAGE.additionalDetails.wordpressTestedUpToVersion)) .pipe(replace('', PACKAGE.version)) .pipe(dest('.')); } exports.front = bundleFrontend; exports.copy = copyBackend; exports.inject = injectMetadata; const build = series(clean, bundleFrontend, copyBackend, injectMetadata); const buildDev = series((cb) => { mode = 'development'; cb(); }, build); const buildProd = series((cb) => { mode = 'production'; cb(); }, build); exports.clean = clean; exports.dev = buildDev; exports.default = buildDev; exports.prod = buildProd;