Can you help with gulp rendering my css and js during production and development current html files is on the root folder. the src folder includes scss and js files and directory the problem is how I am going to set up my <script src=""></script> that it will in both production and development
To handle CSS and JavaScript rendering in both production and development environments using Gulp, you can follow these steps:
Let's assume your folder structure is something like this:
root/
│
├── src/
│ ├── scss/
│ │ └── styles.scss
│ ├── js/
│ │ └── scripts.js
│ └── index.html
├── dist/ (for production)
│ ├── css/
│ │ └── styles.min.css
│ ├── js/
│ │ └── scripts.min.js
└── gulpfile.js
You already have tasks for SCSS compilation, JS minification, and file watching. We'll make slight adjustments to them to ensure they work for both environments.
Here’s an example setup for your gulpfile.js
:
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const browserSync = require('browser-sync').create();
// Paths
const paths = {
scss: './src/scss/**/*.scss',
js: './src/js/**/*.js',
html: './*.html'
};
// SCSS to CSS
function styleTask() {
return gulp.src(paths.scss)
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'))
.pipe(cleanCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./dist/css'))
.pipe(browserSync.stream());
}
// Minify JS
function jsTask() {
return gulp.src(paths.js)
.pipe(gulp.dest('./dist/js'))
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./dist/js'))
.pipe(browserSync.stream());
}
// HTML task
function htmlTask() {
return gulp.src(paths.html)
.pipe(gulp.dest('./dist'))
.pipe(browserSync.stream());
}
// BrowserSync
function browsersyncServe(cb) {
browserSync.init({
server: {
baseDir: './'
}
});
cb();
}
function browsersyncReload(cb) {
browserSync.reload();
cb();
}
// Watch files
function watchTask() {
gulp.watch(paths.scss, styleTask);
gulp.watch(paths.js, jsTask);
gulp.watch(paths.html, browsersyncReload);
}
// Default Gulp task
exports.default = gulp.series(
gulp.parallel(styleTask, jsTask, htmlTask),
browsersyncServe,
watchTask
);
In your HTML file (index.html
), set up the paths depending on the environment:
When working in development, you'll likely want to include the unminified versions of your CSS and JS:
<link rel="stylesheet" href="dist/css/styles.css">
<script src="dist/js/scripts.js"></script>
In production, you'll want to include the minified versions:
<link rel="stylesheet" href="dist/css/styles.min.css">
<script src="dist/js/scripts.min.js"></script>
To automate this, you can use environment variables in Gulp to differentiate between production and development.
First, install gulp-if
and yargs
to help manage environments:
npm install gulp-if yargs --save-dev
Now, modify your gulpfile.js
:
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const browserSync = require('browser-sync').create();
const gulpIf = require('gulp-if');
const yargs = require('yargs');
const argv = yargs.argv;
const production = !!argv.production; // Check if --production flag is used
// Paths
const paths = {
scss: './src/scss/**/*.scss',
js: './src/js/**/*.js',
html: './*.html'
};
// SCSS to CSS
function styleTask() {
return gulp.src(paths.scss)
.pipe(sass().on('error', sass.logError))
.pipe(gulpIf(production, cleanCSS())) // Minify CSS in production
.pipe(gulpIf(production, rename({ suffix: '.min' })))
.pipe(gulp.dest('./dist/css'))
.pipe(browserSync.stream());
}
// Minify JS
function jsTask() {
return gulp.src(paths.js)
.pipe(gulpIf(production, uglify())) // Minify JS in production
.pipe(gulpIf(production, rename({ suffix: '.min' })))
.pipe(gulp.dest('./dist/js'))
.pipe(browserSync.stream());
}
// BrowserSync
function browsersyncServe(cb) {
browserSync.init({
server: {
baseDir: './'
}
});
cb();
}
function browsersyncReload(cb) {
browserSync.reload();
cb();
}
// Watch files
function watchTask() {
gulp.watch(paths.scss, styleTask);
gulp.watch(paths.js, jsTask);
gulp.watch(paths.html, browsersyncReload);
}
// Default Gulp task
exports.default = gulp.series(
gulp.parallel(styleTask, jsTask),
gulpIf(!production, browsersyncServe),
gulpIf(!production, watchTask)
);
// Build task for production
exports.build = gulp.series(
gulp.parallel(styleTask, jsTask)
);
Now, when you run Gulp without flags, it assumes development mode:
gulp
And for production:
gulp --production
This setup allows you to have the correct CSS and JS files linked depending on the environment you’re working in. During development, it will use the unminified versions, and in production, it will automatically switch to the minified versions.