https://github.com/posthtml/posthtml-load-config
Autoload Config for PostHTML
https://github.com/posthtml/posthtml-load-config
Last synced: 10 months ago
JSON representation
Autoload Config for PostHTML
- Host: GitHub
- URL: https://github.com/posthtml/posthtml-load-config
- Owner: posthtml
- License: other
- Created: 2016-06-01T16:23:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-08-28T09:24:04.000Z (over 5 years ago)
- Last Synced: 2024-10-29T21:06:06.537Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![tests][tests]][tests-url]
[![coverage][cover]][cover-url]
[![code style][style]][style-url]
[![chat][chat]][chat-url]
Install
```bash
npm i -D posthtml-load-config
```
Usage
```
npm i -S|-D posthtml-plugin posthtml-plugin ...
```
Install plugins and save them to your ***package.json***
### `package.json`
Create a **`posthtml`** section in **`package.json`**.
```
Root
|– client
|– public
|
|- package.json
```
```json
{
"posthtml": {
"parser": "posthtml-sugarml",
"from": "/path/to/src/file.sml",
"to": "/path/to/dest/file.html",
"plugins": {
"posthtml-plugin": {}
}
}
}
```
### `.posthtmlrc`
Create a **`.posthtmlrc`** file.
```
Root
|– client
|– public
|
|-.posthtmlrc
|- package.json
```
```json
{
"parser": "posthtml-sugarml",
"from": "/path/to/src/file.sml",
"to": "/path/to/dest/file.html",
"plugins": {
"posthtml-plugin": {}
}
}
```
### `posthtml.config.js`
Create a **`posthtml.config.js`** file.
```
Root
|– client
|– public
|
|- posthtml.config.js
|- package.json
```
```js
module.exports = (ctx) => {
return {
parser: ctx.ext === '.sml' ? 'posthtml-sugarml' : false,
from: ctx.from,
to: ctx.to,
plugins: {
'posthtml-plugin': ctx.plugin
}
}
}
```
Plugins can be loaded either using an `{Object}` or an `{Array}` in `config.plugins`.
##### `{Object}`
```js
module.exports = (ctx) => {
return {
...options,
plugins: {
'posthtml-plugin': ctx.plugin
}
}
}
```
##### `{Array}`
```js
module.exports = (ctx) => {
return {
...options,
plugins: [
require('posthtml-plugin')(ctx.plugin)
]
}
}
```
> :warning: When using an Array, make sure to `require()` them.
Options
**`parser`**:
```js
parser: 'posthtml-sugarml'
```
**`from`**:
```js
from: 'path/to/src/file.sml'
```
**`to`**:
```js
to: 'path/to/dest/file.html'
```
**`render`**:
```js
render: 'posthtml-jsx'
```
Plugins
### Options
**`{} || null`**: Plugin loads with defaults.
```js
'posthtml-plugin': {} || null
```
> :warning: `{}` must be an **empty** object
**`[Object]`**: Plugin loads with given options.
```js
'posthtml-plugin': { option: '', option: '' }
```
**`false`**: Plugin will not be loaded.
```js
'posthtml-plugin': false
```
### Order
Plugin **order** is determined by declaration in the plugins section.
```js
{
plugins: {
'posthtml-plugin': {}, // plugins[0]
'posthtml-plugin': {}, // plugins[1]
'posthtml-plugin': {} // plugins[2]
}
}
```
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**
```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
}
})
```
### 
```json
"scripts": {
"build": "NODE_ENV=production node posthtml",
"start": "NODE_ENV=development node posthtml"
}
```
```js
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))
})
```
### 
```bash
npm i -D gulp-posthtml
```
**package.json**
```json
"scripts": {
"build": "NODE_ENV=production gulp",
"start": "NODE_ENV=development gulp"
}
```
**gulpfile.js**
```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(posthtml())
.pipe(rename({ ext: '.html' }))
.pipe(dest('dest'))
})
task('html', () => {
return src('src/*.html')
.pipe(posthtml())
.pipe(dest('dest'))
})
```
### 
```bash
npm i -D html-loader posthtml-loader
```
**package.json**
```json
"scripts": {
"build": "NODE_ENV=production webpack",
"start": "NODE_ENV=development webpack-dev-server"
}
```
**webpack.config.js**
```js
module.exports = (env) => {
module: {
rules: [
{
test: /\.html$/
use: [
'html-loader',
'posthtml-loader'
]
}
]
}
}
```
Maintainer
Contributors
[npm]: https://img.shields.io/npm/v/posthtml-load-config.svg
[npm-url]: https://npmjs.com/package/posthtml-load-config
[node]: https://img.shields.io/node/v/posthtml-load-plugins.svg
[node-url]: https://nodejs.org/
[deps]: https://david-dm.org/posthtml/posthtml-load-config.svg
[deps-url]: https://david-dm.org/posthtml/posthtml-load-config
[style]: https://img.shields.io/badge/code%20style-standard-yellow.svg
[style-url]: http://standardjs.com/
[tests]: http://img.shields.io/travis/posthtml/posthtml-load-config.svg?branch=master
[tests-url]: https://travis-ci.org/posthtml/posthtml-load-config?branch=master
[cover]: https://coveralls.io/repos/github/posthtml/posthtml-load-config/badge.svg?branch=master
[cover-url]: https://coveralls.io/github/posthtml/posthtml-load-config?branch=master
[chat]: https://badges.gitter.im/posthtml/posthtml.svg
[chat-url]: https://gitter.im/posthtml/posthtml?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"