Load Config
Autoload Config for PostHTML
Install
npm i -D posthtml-load-config copy
Usage
npm i -S|-D posthtml-plugin posthtml-plugin ... copy
Install plugins and save them to your package.json
package.json
Create a posthtml section in package.json .
Root
|– client
|– public
|
|- package.json copy
{
"posthtml" : {
"parser" : "posthtml-sugarml" ,
"from" : "/path/to/src/file.sml" ,
"to" : "/path/to/dest/file.html" ,
"plugins" : {
"posthtml-plugin" : {}
}
}
} copy
.posthtmlrc
Create a .posthtmlrc file.
Root
|– client
|– public
|
|-.posthtmlrc
|- package.json copy
{
"parser" : "posthtml-sugarml" ,
"from" : "/path/to/src/file.sml" ,
"to" : "/path/to/dest/file.html" ,
"plugins" : {
"posthtml-plugin" : {}
}
} copy
posthtml.config.js
Create a posthtml.config.js file.
Root
|– client
|– public
|
|- posthtml.config.js
|- package.json copy
module . exports = (ctx) => {
return {
parser: ctx.ext === '.sml' ? 'posthtml-sugarml' : false ,
from: ctx.from,
to: ctx.to,
plugins: {
'posthtml-plugin' : ctx.plugin
}
}
} copy
Plugins can be loaded either using an {Object} or an {Array} in config.plugins.
{Object}
module . exports = (ctx) => {
return {
... options,
plugins: {
'posthtml-plugin' : ctx.plugin
}
}
} copy
{Array}
module . exports = (ctx) => {
return {
... options,
plugins: [
require ( 'posthtml-plugin' )(ctx.plugin)
]
}
} copy
:warning: When using an Array, make sure to require() them.
Options
parser :
parser : 'posthtml-sugarml' copy
from :
from : 'path/to/src/file.sml' copy
to :
to : 'path/to/dest/file.html' copy
render :
render : 'posthtml-jsx' copy
Plugins
Options
{} || null : Plugin loads with defaults.
'posthtml-plugin' : {} || null copy
:warning: {} must be an empty object
[Object] : Plugin loads with given options.
'posthtml-plugin' : { option: '' , option: '' } copy
false : Plugin will not be loaded.
'posthtml-plugin' : false copy
Order
Plugin order is determined by declaration in the plugins section.
{
plugins : {
'posthtml-plugin' : {}, // plugins[0]
'posthtml-plugin' : {}, // plugins[1]
'posthtml-plugin' : {} // plugins[2]
}
} copy
Context
When using a function (posthtml.config.js), it is possible to pass context to posthtml-load-config, which will be evaluated while loading your config. By default ctx.env (process.env.NODE_ENV) and ctx.cwd (process.cwd()) are available.
Examples
posthtml.config.js
module . exports = (ctx) => ({
parser: ctx.ext === '.sml' ? 'posthtml-sugarml' : false ,
from: ctx.from,
to: ctx.to,
plugins: {
posthtml-include: {},
posthtml-expressions: { locals: ctx.locals },
htmlnano: ctx.env === 'production' ? {} : false
}
}) copy
"scripts" : {
"build" : "NODE_ENV=production node posthtml" ,
"start" : "NODE_ENV=development node posthtml"
} copy
import { readFileSync } = require( 'fs' )
const posthtml = require( 'posthtml' )
const posthtmlrc = require( 'posthtml-load-config' )
const sml = readFileSync( 'index.sml' , 'utf8' )
const ctx = { ext: '.sml' }
posthtmlrc(ctx).then(({ plugins, options }) => {
posthtml(plugins)
.process(sml, options)
.then((result) => console.log(result.html))
}) copy
npm i -D gulp-posthtml copy
package.json
"scripts" : {
"build" : "NODE_ENV=production gulp" ,
"start" : "NODE_ENV=development gulp"
} copy
gulpfile.js
import { task, src, dest } from 'gulp'
import plugins from 'gulp-load-plugins'
task ( 'pug' , () => {
const ctx = { locals: {} }
return src ( 'src/*.pug' )
. pipe ( posthtml (ctx))
. pipe ( rename ({ ext: '.html' }))
. pipe ( dest ( 'dest' ))
})
task ( 'sml' , () => {
return src ( 'src/*.sml' )
. pipe (
npm i -D html-loader posthtml-loader copy
package.json
"scripts" : {
"build" : "NODE_ENV=production webpack" ,
"start" : "NODE_ENV=development webpack-dev-server"
} copy
webpack.config.js
module . exports = (env) => {
module : {
rules : [
{
test: /\.html$/
use: [
'html-loader' ,
'posthtml-loader'
]
}
]
}
} copy
Maintainer
Contributors