Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
feat(output): add minification option
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed May 22, 2017
1 parent e6ef155 commit fa94bdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"commander": "^2.9.0",
"contentful": "^4.1.2",
"glob": "^7.1.1",
"html-minifier": "^3.5.1",
"moment": "^2.18.1",
"showdown": "^1.6.4"
},
Expand Down
19 changes: 16 additions & 3 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'
import { template } from 'lodash'
import path from 'path'
import moment from 'moment'
import { minify } from 'html-minifier'

const markdownConverter = new Converter({
simplifiedAutoLink: true,
Expand Down Expand Up @@ -131,7 +132,18 @@ const buildPage = (build, config, collections, blocks, content, identifier, temp
}

// Build page
const pageTemplate = buildTemplate(readFileSync(template, 'utf8'), templateData)
let pageTemplate = buildTemplate(readFileSync(template, 'utf8'), templateData)
if (config.minify) {
pageTemplate = minify(pageTemplate, {
removeAttributeQuotes: true,
decodeEntities: true,
removeComments: true,
removeEmptyAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true,
collapseInlineTagWhitespace: true
})
}

// Build pages
if (isIndex) {
Expand All @@ -146,7 +158,7 @@ const buildPage = (build, config, collections, blocks, content, identifier, temp
return templateData
}

export const buildSite = (contentFile, templateDir, version, locale, environment = 'production') => {
export const buildSite = (contentFile, templateDir, version, locale, environment = 'production', minify = true) => {
const content = JSON.parse(readFileSync(contentFile, 'utf-8'))

// Find includes
Expand All @@ -160,7 +172,8 @@ export const buildSite = (contentFile, templateDir, version, locale, environment
environment,
version,
time: Date.now(),
locale
locale,
minify
}

// Build the config
Expand Down
3 changes: 2 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ program
.option('-l, --locale <locale>', 'contentful locale')
.option('-t, --templates <templates>', 'template directory')
.option('-e, --environment [environment]', 'environment', 'production')
.action(options => buildSite(options.content, options.templates, options.version, options.locale, options.environment))
.option('-m, --minify', 'minify output', false)
.action(options => buildSite(options.content, options.templates, options.version, options.locale, options.environment, options.minify))

program.parse(process.argv)

0 comments on commit fa94bdd

Please sign in to comment.