Skip to content

Commit

Permalink
Modify tasks to use process.env.NODE_ENV convention instead of a cust…
Browse files Browse the repository at this point in the history
…om boolean. (#23)

This allows #19 to use the env context in custom PostCSS config's.
  • Loading branch information
gijsroge authored and MartijnCuppens committed Oct 4, 2019
1 parent ce919da commit a285c28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
20 changes: 13 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function watchFiles() {

// eslint-disable-next-line func-names
gulp.watch(src, function css() {
return cssCompile({src, dest, minified: false, browserSync});
return cssCompile({src, dest, browserSync});
});
cssCompile({src, dest, minified: false});
cssCompile({src, dest});
});

config.js.forEach(js => {
Expand All @@ -37,9 +37,9 @@ function watchFiles() {

// eslint-disable-next-line func-names
gulp.watch(src, function js() {
return jsCompile({src, dest, minified: false, browserSync});
return jsCompile({src, dest, browserSync});
});
jsCompile({src, dest, minified: false});
jsCompile({src, dest});
});

config['js-concat'].forEach(js => {
Expand All @@ -49,9 +49,9 @@ function watchFiles() {

// eslint-disable-next-line func-names
gulp.watch(src, function concat() {
return jsConcat({src, dest, name, minified: false, browserSync});
return jsConcat({src, dest, name, browserSync});
});
jsConcat({src, dest, name, minified: false});
jsConcat({src, dest, name});
});

config.img.forEach(img => {
Expand Down Expand Up @@ -91,8 +91,13 @@ function watchFiles() {
}
}

function setProduction(cb) {
process.env.NODE_ENV = 'production';
cb();
}

// Define complex tasks
const build = gulp.series(clean, copy, gulp.parallel(css, js, img));
const build = gulp.series(setProduction, clean, copy, gulp.parallel(css, js, img));
const watch = gulp.series(clean, copy, watchFiles);

// Export tasks
Expand All @@ -103,5 +108,6 @@ exports.js = js;
exports['js-concat'] = jsConcat;
exports['svg-sprite'] = svgSprite;
exports.clean = clean;
exports.setProduction = setProduction;
exports.build = build;
exports.watch = watch;
10 changes: 5 additions & 5 deletions lib/gulp/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ function css(cb) {
cb();
}

async function getPostCSSConfiguration(minified) {
async function getPostCSSConfiguration() {
try {
// Get PostCSS config from user
return await loadPostCSSConfig();
} catch (error) {
// Catch no config found from postcss-load-config in order to set default
// values for our PostCSS instance.
if (minified) {
if (process.env.NODE_ENV === 'production') {
return {plugins: [rfs(), autoprefixer(), cssnano()]};
}

return {plugins: [rfs(), autoprefixer()]};
}
}

async function cssCompile({src, dest, minified = true, browserSync = false}) {
const {plugins, options = {}} = await getPostCSSConfiguration(minified);
async function cssCompile({src, dest, browserSync = false}) {
const {plugins, options = {}} = await getPostCSSConfiguration();
let stream = gulp
.src(src);

Expand All @@ -42,7 +42,7 @@ async function cssCompile({src, dest, minified = true, browserSync = false}) {
stream = stream.pipe(plumber());
}

if (minified) {
if (process.env.NODE_ENV === 'production') {
stream = stream
.pipe(sass({outputStyle: 'compressed'}))
.pipe(postcss(plugins, options));
Expand Down
8 changes: 4 additions & 4 deletions lib/gulp/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function js(cb) {
cb();
}

function jsCompile({src, dest, minified = true, browserSync = false}) {
function jsCompile({src, dest, browserSync = false}) {
let stream = gulp
.src(src);

Expand All @@ -30,7 +30,7 @@ function jsCompile({src, dest, minified = true, browserSync = false}) {
presets: [presetEnv]
}));

if (minified) {
if (process.env.NODE_ENV === 'production') {
stream = stream.pipe(terser());
}

Expand All @@ -39,7 +39,7 @@ function jsCompile({src, dest, minified = true, browserSync = false}) {
return browserSync === false ? stream : stream.pipe(browserSync.stream({match: '**/*.js'}));
}

function jsConcat({src, dest, name, minified = true, browserSync = false}) {
function jsConcat({src, dest, name, browserSync = false}) {
let stream = gulp.src(src);

// Prevent errors from aborting task when files are being watched
Expand All @@ -53,7 +53,7 @@ function jsConcat({src, dest, name, minified = true, browserSync = false}) {
}))
.pipe(concat(name));

if (minified) {
if (process.env.NODE_ENV === 'production') {
stream = stream.pipe(terser());
}

Expand Down

0 comments on commit a285c28

Please sign in to comment.