Skip to content

Commit

Permalink
fix: support all posthtml options, close #124
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Aug 21, 2020
1 parent fe9e332 commit fd79d32
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ module: {
|:--:|:--:|:-----:|:----------|
|**[`config`](#config)**|`{Object}`|`undefined`|PostHTML Config|
|**[`parser`](#parser)**|`{String/Function}`|`undefined`|PostHTML Parser|
|**[`render`](#parser)**|`{String/Function}`|`undefined`|PostHTML Render|
|**[`skipParse`](#skipParse)**|`{Boolean}`|`false`|PostHTML Options SkipParse|
|**[`render`](#render)**|`{String/Function}`|`undefined`|PostHTML Render|
|**[`plugins`](#plugins)**|`{Array/Function}`|`[]`|PostHTML Plugins|
|**[`sync`](#sync)**|`{boolean}`|`false`|PostHTML Options Sync|
|**[`directives`](#directives)**|`{Array<Object>}`|`[]`|PostHTML Options custom [Directives](https://github.com/posthtml/posthtml-parser#directives)|

### `Config`

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


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

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

### `skipParse`

If you want to use disable parsing, you can pass it in under the `skipParse` key in the loader options

#### `{Boolean}`

**webpack.config.js**
```js
{
loader: 'posthtml-loader',
options: {
skipParse: false
}
}
```

### `Render`

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

### `Sync`

Enables sync mode, plugins will run synchronously, throws an error when used with async plugins

#### `{Boolean}`

**webpack.config.js**
```js
{
loader: 'posthtml-loader',
options: {
sync: true
}
}
```

### `Directives`

If you want to use a custom directives, you can pass it in under the `directives` key in the loader options

#### `{Array}`

**webpack.config.js**
```js
{
loader: 'posthtml-loader',
options: {
directives: [{name: '?php', start: '<', end: '>'}]
}
}
```

<h2 align="center">Maintainer</h2>

<table>
Expand Down
23 changes: 8 additions & 15 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
'use strict'

module.exports = function parseOptions (params) {
if (typeof params.plugins === 'function') {
params.plugins = params.plugins.call(this, this)
}

let plugins

if (typeof params.plugins === 'undefined') plugins = []
else if (Array.isArray(params.plugins)) plugins = params.plugins
else plugins = [params.plugins]
let {plugins, ...options} = params;

const options = {}

if (typeof params !== 'undefined') {
options.parser = params.parser
// options.render = params.render
if (typeof plugins === 'function') {
plugins = plugins.call(this, this)
}

return Promise.resolve({ options: options, plugins: plugins })
if (typeof plugins === 'undefined') plugins = []
else if (Array.isArray(plugins)) plugins = plugins
else plugins = [plugins]

return Promise.resolve({ options, plugins })
}
30 changes: 30 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
{
"type": "object",
"properties": {
"sync": {
"type": "boolean"
},
"directives": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"start": {
"type": "string"
},
"end": {
"type": "string"
}
}
}
},
"skipParse": {
"type": "boolean"
},
"ident": {
"type": "string"
},
Expand All @@ -23,6 +46,13 @@
{ "instanceof": "Function" }
]
},
"render": {
"oneOf": [
{ "type": "string" },
{ "type": "object" },
{ "instanceof": "Function" }
]
},
"plugins": {
"oneOf": [
{ "type": "array" },
Expand Down

0 comments on commit fd79d32

Please sign in to comment.