Created
November 2, 2020 00:07
-
-
Save pedrorvidal/8d71bf33ff27eb0be2acdb0d94fbcf1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import { src, dest, watch, series, parallel } from 'gulp'; | |
const { src, dest, watch, series, parallel } = pkg; | |
import pkg from 'gulp'; | |
import yargs from 'yargs'; | |
import sass from 'gulp-sass'; | |
import cleanCss from 'gulp-clean-css'; | |
import gulpif from 'gulp-if'; | |
import postcss from 'gulp-postcss'; | |
import sourcemaps from 'gulp-sourcemaps'; | |
import autoprefixer from 'autoprefixer'; | |
import imagemin from 'gulp-imagemin'; | |
import del from 'del'; | |
import webpack from 'webpack-stream'; | |
import named from 'vinyl-named'; | |
import browserSync from "browser-sync"; | |
import zip from "gulp-zip"; | |
import info from "./package.json"; | |
import replace from "gulp-replace"; | |
import wpPot from "gulp-wp-pot"; | |
const PRODUCTION = yargs.argv.prod; | |
const server = browserSync.create(); | |
export const serve = done => { | |
server.init({ | |
proxy: "http://tchebusca.local", | |
browser: ["google-chrome"] | |
// browser: ["firefox"] | |
}); | |
done(); | |
}; | |
export const reload = done => { | |
server.reload(); | |
done(); | |
}; | |
export const clean = () => del(['dist']); | |
export const styles = () => { | |
return src( 'src/scss/bundle.scss') | |
.pipe(gulpif(!PRODUCTION, sourcemaps.init())) | |
.pipe(sass().on('error', sass.logError)) | |
.pipe(gulpif(PRODUCTION, postcss([ autoprefixer ]))) | |
.pipe(gulpif(PRODUCTION, cleanCss({compatibility:'ie8'}))) | |
.pipe(gulpif(!PRODUCTION, sourcemaps.write())) | |
.pipe(dest('dist/css')) | |
.pipe(server.stream()); | |
} | |
export const images = () => { | |
return src('src/img/**/*.{jpg,jpeg,png,svg,gif}') | |
.pipe(gulpif(PRODUCTION, imagemin())) | |
.pipe(dest('dist/img')); | |
} | |
export const copy = () => { | |
return src(['src/**/*','!src/{img,js,scss}','!src/{img,js,scss}/**/*']) | |
.pipe(dest('dist')); | |
} | |
export const scripts = () => { | |
// return src(['src/js/bundle.js','src/js/admin.js']) | |
return src('src/js/bundle.js') | |
.pipe(named()) | |
.pipe(webpack({ | |
module: { | |
rules: [ | |
{ | |
test: /\.js$/, | |
use: { | |
loader: 'babel-loader', | |
options: { | |
presets: [] | |
} | |
} | |
} | |
] | |
}, | |
mode: PRODUCTION ? 'production' : 'development', | |
devtool: !PRODUCTION ? 'inline-source-map' : false, | |
output: { | |
filename: '[name].js' | |
}, | |
externals: { | |
jquery: 'jQuery' | |
}, | |
})) | |
.pipe(dest('dist/js')); | |
} | |
export const compress = () => { | |
return src([ | |
"**/*", | |
"!node_modules{,/**}", | |
"!bundled{,/**}", | |
"!src{,/**}", | |
"!.babelrc", | |
"!.gitignore", | |
"!gulpfile.babel.js", | |
"!package.json", | |
"!package-lock.json", | |
]) | |
.pipe( | |
gulpif( | |
file => file.relative.split(".").pop() !== "zip", | |
replace("_themename", info.name) | |
) | |
) | |
.pipe(zip(`${info.name}.zip`)) | |
.pipe(dest('bundled')); | |
}; | |
export const pot = () => { | |
return src("**/*.php") | |
.pipe( | |
wpPot({ | |
domain: "_themename", | |
package: info.name | |
}) | |
) | |
.pipe(dest(`languages/${info.name}.pot`)); | |
}; | |
export const watchForChanges = () => { | |
watch('src/scss/**/*.scss', styles); | |
watch('src/img/**/*.{jpg,jpeg,png,svg,gif}', series(images, reload)); | |
watch(['src/**/*','!src/{img,js,scss}','!src/{img,js,scss}/**/*'], series(copy, reload)); | |
watch('src/js/**/*.js', series(scripts, reload)); | |
watch("**/*.php", reload); | |
} | |
export const dev = series(clean, parallel(styles, images, copy, scripts), serve, watchForChanges); | |
export const build = series(clean, parallel(styles, images, copy, scripts), pot, compress); | |
export default dev; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment