Last active
December 8, 2017 16:10
-
-
Save MKRhere/3d289e1aacb7ce5ad7a633ed80003b73 to your computer and use it in GitHub Desktop.
Node Build and Watch Script for static websites using sass, js (no Webpack necessary)
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
/* | |
// Add these to package.json and do `npm install --only=dev` | |
// Add this file to project root, and then do `node build` | |
"devDependencies": { | |
"@babel/core": "^7.0.0-beta.34", | |
"@babel/preset-env": "^7.0.0-beta.34", | |
"babel-cli": "^6.26.0", | |
"babelify": "^8.0.0", | |
"browserify": "^14.5.0", | |
"node-sass": "^4.7.2", | |
"node-watch": "^0.5.5" | |
} | |
*/ | |
const fs = require('fs') | |
const browserify = require('browserify') | |
const babelify = require('babelify') | |
const sass = require('node-sass') | |
const watch = require('node-watch') | |
let jsPath = { | |
get: './js/main.js', | |
put: './public/js/bundle.js' | |
} | |
let cssPath = { | |
get: './sass/style.sass', | |
put: './public/css/style.css' | |
} | |
const jsfy = () => { | |
browserify({ debug: true }) | |
.transform(babelify) | |
.require(jsPath.get, { entry: true }) | |
.bundle() | |
.on('error', function (err) { console.log('Error: ' + err.message) }) | |
.pipe(fs.createWriteStream(jsPath.put)) | |
} | |
const cssfy = () => { | |
sass.render({ | |
file: cssPath.get | |
}, function (err, result) { | |
fs.writeFile(cssPath.put, result.css, (e) => { | |
if (e) console.log(console.log('[ERR] : ' + e)) | |
}) | |
}) | |
} | |
function start() { | |
try { | |
jsfy() | |
cssfy() | |
} catch (e) { | |
console.log('[ERR] : ' + e) | |
} | |
} | |
start() | |
watch(['js', 'sass'], | |
{ | |
recursive: true | |
}, | |
start | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment