Skip to content

Commit fd79d32

Browse files
committed
fix: support all posthtml options, close #124
1 parent fe9e332 commit fd79d32

File tree

3 files changed

+91
-16
lines changed

3 files changed

+91
-16
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ module: {
5757
|:--:|:--:|:-----:|:----------|
5858
|**[`config`](#config)**|`{Object}`|`undefined`|PostHTML Config|
5959
|**[`parser`](#parser)**|`{String/Function}`|`undefined`|PostHTML Parser|
60-
|**[`render`](#parser)**|`{String/Function}`|`undefined`|PostHTML Render|
60+
|**[`skipParse`](#skipParse)**|`{Boolean}`|`false`|PostHTML Options SkipParse|
61+
|**[`render`](#render)**|`{String/Function}`|`undefined`|PostHTML Render|
6162
|**[`plugins`](#plugins)**|`{Array/Function}`|`[]`|PostHTML Plugins|
63+
|**[`sync`](#sync)**|`{boolean}`|`false`|PostHTML Options Sync|
64+
|**[`directives`](#directives)**|`{Array<Object>}`|`[]`|PostHTML Options custom [Directives](https://github.com/posthtml/posthtml-parser#directives)|
6265

6366
### `Config`
6467

@@ -67,6 +70,7 @@ module: {
6770
|**[`path`](#path)**|`{String}`|`loader.resourcePath`|PostHTML Config Path|
6871
|**[`ctx`](#context)**|`{Object}`|`{}`|PostHTML Config Context|
6972

73+
7074
If you want to use are shareable config file instead of inline options in your `webpack.config.js` create a `posthtml.config.js` file and place it somewhere down the file tree in your project. The nearest config relative to `dirname(file)` currently processed by the loader applies. This enables **Config Cascading**. Despite some edge cases the config file will be loaded automatically and **no** additional setup is required. If you don't intend to use Config Cascading, it's recommended to place `posthtml.config.js` in the **root** `./` of your project
7175

7276
```
@@ -158,6 +162,22 @@ If you want to use a custom parser e.g [SugarML](https://github.com/posthtml/sug
158162
}
159163
```
160164

165+
### `skipParse`
166+
167+
If you want to use disable parsing, you can pass it in under the `skipParse` key in the loader options
168+
169+
#### `{Boolean}`
170+
171+
**webpack.config.js**
172+
```js
173+
{
174+
loader: 'posthtml-loader',
175+
options: {
176+
skipParse: false
177+
}
178+
}
179+
```
180+
161181
### `Render`
162182

163183
If you want to use a custom render, you can pass it in under the `render` key in the loader options
@@ -220,6 +240,38 @@ Plugins are specified under the `plugins` key in the loader options
220240
}
221241
```
222242

243+
### `Sync`
244+
245+
Enables sync mode, plugins will run synchronously, throws an error when used with async plugins
246+
247+
#### `{Boolean}`
248+
249+
**webpack.config.js**
250+
```js
251+
{
252+
loader: 'posthtml-loader',
253+
options: {
254+
sync: true
255+
}
256+
}
257+
```
258+
259+
### `Directives`
260+
261+
If you want to use a custom directives, you can pass it in under the `directives` key in the loader options
262+
263+
#### `{Array}`
264+
265+
**webpack.config.js**
266+
```js
267+
{
268+
loader: 'posthtml-loader',
269+
options: {
270+
directives: [{name: '?php', start: '<', end: '>'}]
271+
}
272+
}
273+
```
274+
223275
<h2 align="center">Maintainer</h2>
224276

225277
<table>

lib/options.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
'use strict'
22

33
module.exports = function parseOptions (params) {
4-
if (typeof params.plugins === 'function') {
5-
params.plugins = params.plugins.call(this, this)
6-
}
7-
8-
let plugins
9-
10-
if (typeof params.plugins === 'undefined') plugins = []
11-
else if (Array.isArray(params.plugins)) plugins = params.plugins
12-
else plugins = [params.plugins]
4+
let {plugins, ...options} = params;
135

14-
const options = {}
15-
16-
if (typeof params !== 'undefined') {
17-
options.parser = params.parser
18-
// options.render = params.render
6+
if (typeof plugins === 'function') {
7+
plugins = plugins.call(this, this)
198
}
209

21-
return Promise.resolve({ options: options, plugins: plugins })
10+
if (typeof plugins === 'undefined') plugins = []
11+
else if (Array.isArray(plugins)) plugins = plugins
12+
else plugins = [plugins]
13+
14+
return Promise.resolve({ options, plugins })
2215
}

lib/options.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
{
22
"type": "object",
33
"properties": {
4+
"sync": {
5+
"type": "boolean"
6+
},
7+
"directives": {
8+
"type": "array",
9+
"items": {
10+
"type": "object",
11+
"properties": {
12+
"name": {
13+
"type": "string"
14+
},
15+
"start": {
16+
"type": "string"
17+
},
18+
"end": {
19+
"type": "string"
20+
}
21+
}
22+
}
23+
},
24+
"skipParse": {
25+
"type": "boolean"
26+
},
427
"ident": {
528
"type": "string"
629
},
@@ -23,6 +46,13 @@
2346
{ "instanceof": "Function" }
2447
]
2548
},
49+
"render": {
50+
"oneOf": [
51+
{ "type": "string" },
52+
{ "type": "object" },
53+
{ "instanceof": "Function" }
54+
]
55+
},
2656
"plugins": {
2757
"oneOf": [
2858
{ "type": "array" },

0 commit comments

Comments
 (0)