Last active
August 11, 2017 18:14
-
-
Save CITguy/94d1d33a5e36901e402a2cf8fbd27fd3 to your computer and use it in GitHub Desktop.
Using Browsersync + Hexo to run a local development server
This file contains hidden or 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
#!/usr/bin/env node | |
'use strict'; | |
const Hexo = require('hexo'); | |
const YAML = require('js-yaml'); | |
const fs = require('fs'); | |
const cwd = process.cwd(); | |
exports.hexo = new Hexo(cwd); | |
exports.config = YAML.safeLoad( | |
fs.readFileSync(`${cwd}/_config.yml`, 'utf8') | |
); |
This file contains hidden or 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
#!/usr/bin/env node | |
'use strict'; | |
const browserSync = require('browser-sync').create(); | |
const { hexo, config } = require('./hexo'); | |
function initServer () { | |
const serverRoutes = {}; | |
// account for Hexo "root" configuration | |
serverRoutes[`${config.root}`] = config.public_dir; | |
browserSync.init({ | |
files: [ // (See 'watchEvents') | |
`${config.public_dir}/**/*`, | |
{ | |
match: [ | |
'source/**/*', | |
'themes/**/*' | |
], | |
fn: (evt, file) => { | |
// force regeneration of hexo assets on change of source or theme | |
hexo.call('generate', { force: true }); | |
} | |
} | |
], | |
logLevel: 'debug', | |
open: false, | |
reloadOnRestart: true, | |
reloadDebounce: 250, // prevent calling numerous reloads on forced hexo generate | |
server: { | |
baseDir: config.public_dir, | |
routes: serverRoutes | |
}, | |
watchEvents: ['change'] | |
}); | |
}//initServer() | |
hexo.init().then(() => { | |
hexo.call('clean') | |
.then(() => { | |
// MUST have empty obj as 2nd arg or the call will fail | |
return hexo.call('generate', {}); | |
}) | |
.then(initServer) | |
.catch((err) => { | |
hexo.exit(err); | |
}); | |
});//hexo.init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment