Skip to content

Instantly share code, notes, and snippets.

@dioncodes
Last active July 3, 2020 12:39
Show Gist options
  • Save dioncodes/149eb5793efa8e8d577a9a55b8276fd6 to your computer and use it in GitHub Desktop.
Save dioncodes/149eb5793efa8e8d577a9a55b8276fd6 to your computer and use it in GitHub Desktop.
npm + gulp for scss and js minifying
var gulp = require('gulp');
var sass = require('gulp-sass');
// var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');
var babel = require('gulp-babel');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
function style(path, dest) {
return (
gulp.src(path)
// .pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'compressed' }))
.on("error", sass.logError)
.pipe(postcss([ require('autoprefixer') ]))
// .pipe(sourcemaps.write())
.pipe(rename((path) => {
path.extname = '.min.css'
}))
.pipe(gulp.dest(dest))
);
}
function script(path, dest) {
return (
gulp.src(path)
.pipe(babel({ presets: ['@babel/env']}))
.pipe(uglify())
.pipe(rename((path) => {
path.extname = '.min.js'
}))
.pipe(gulp.dest(dest))
);
}
exports.style = style;
exports.script = script;
function watch() {
gulp.watch('src/scss/*.scss', () => { return style('src/scss/*.scss', 'public/css') })
gulp.watch('src/js/*.js', () => { return script('src/js/*.js', '/public/js') });
}
exports.watch = watch
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "public/index.php",
"scripts": {
"watch-sources": "gulp watch"
},
"author": "Dion Purushotham <mail@dion.codes>",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"babel-preset-minify": "^0.5.1",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^7.0.1",
"gulp-babel": "^8.0.0",
"gulp-postcss": "^8.0.0",
"gulp-rename": "^2.0.0",
"gulp-sass": "^4.0.0",
"gulp-uglify": "^3.0.2"
},
"dependencies": {}
}
  1. copy both files to the project's main directory (if no npm is existing yet, otherwise require dev dependencies in existing package.json)
  2. adjust the paths in gulpfile.js to match your source and destination folders (duplicate calls in watch() method for multiple directories)
  3. watch files with gulp watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment