Skip to content

Commit

Permalink
Add cache building support
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Oct 19, 2015
1 parent e8b48f3 commit 8ab0acb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules/
_docpress
/cache
/coverage
20 changes: 20 additions & 0 deletions bin/build
@@ -0,0 +1,20 @@
#!/usr/bin/env node
// Builds the assets for caching in the published version.

var argv = process.argv.slice(2)

if (argv[0] === '--css') {
buildWith(require('../lib/build_css'))
} else if (argv[0] === '--js') {
buildWith(require('../lib/build_js'))
} else {
console.log('usage: build --css|--js')
process.exit(1)
}

function buildWith (render) {
render({ compress: true }, (err, res) => {
if (err) throw err
process.stdout.write(res)
})
}
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -80,7 +80,7 @@ function addCss (files, ms, done) {
*/

function addJs (files, ms, done) {
buildJs((err, contents) => {
buildJs({}, (err, contents) => {
if (err) return done(err)
if (!files['assets/script.js']) {
files['assets/script.js'] = { contents }
Expand Down
11 changes: 8 additions & 3 deletions lib/build_css.js
Expand Up @@ -4,16 +4,17 @@ const join = require('path').join
const readFileSync = require('fs').readFileSync
const dirname = require('path').dirname

const stylus = require('stylus')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')({})

module.exports = function buildCss (options, done) {
buildStylus(join(__dirname, '../data/style.styl'), options, done)
}

function buildStylus (filepath, options, done) {
try {
const stylus = require('stylus')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')({})

let data = readFileSync(filepath, 'utf-8')

let s = stylus(data)
Expand All @@ -22,6 +23,10 @@ function buildStylus (filepath, options, done) {
.set('hoist atrules', true)
.include(join(__dirname, '../node_modules'))

if (options && options.compress) {
s = s.set('compress', true)
}

if (options && options.imports) {
options.imports.forEach((external) => {
s = s
Expand Down
6 changes: 4 additions & 2 deletions lib/build_js.js
Expand Up @@ -2,11 +2,13 @@

const join = require('path').join

module.exports = function buildJs (done) {
module.exports = function buildJs (options, done) {
const fname = join(__dirname, '../data/script.js')
const browserify = require('browserify')
const b = browserify()
b.add(fname)
b.transform(require('uglifyify'), { global: true, sourcemap: false })
if (options && options.compress) {
b.transform(require('uglifyify'), { global: true, sourcemap: false })
}
b.bundle(done)
}
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -46,6 +46,8 @@
},
"scripts": {
"coverage": "istanbul cover _mocha -x '**/fixture/**' -- -R spec",
"build": "mkdir -p cache && ./bin/build --css > cache/style.css && ./bin/build --js > cache/script.js",
"prepublish": "npm run build",
"test": "mocha",
"serve": "ss fixture/onmount/_docpress",
"watch": "mocha -R min --watch"
Expand Down

0 comments on commit 8ab0acb

Please sign in to comment.