|
1 |
| -[![NPM][npm]][npm-url] |
2 |
| -[![Deps][deps]][deps-url] |
3 |
| -[![Tests][build]][build-url] |
4 |
| -[![Coverage][cover]][cover-url] |
5 |
| -[![Standard Code Style][style]][style-url] |
6 |
| -[![Chat][chat]][chat-badge] |
| 1 | +[![npm][npm]][npm-url] |
| 2 | +[![node][node]][node-url] |
| 3 | +[![deps][deps]][deps-url] |
| 4 | +[![tests][tests]][tests-url] |
| 5 | +[![coverage][cover]][cover-url] |
| 6 | +[![code style][style]][style-url] |
| 7 | +[![chat][chat]][chat-url] |
| 8 | + |
| 9 | +<div align="center"> |
| 10 | + <a href="https://github.com/posthtml/posthtml"> |
| 11 | + <img width="220" height="200" title="PosHTML" src="http://posthtml.github.io/posthtml/logo.svg"> |
| 12 | + </a> |
| 13 | + <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg"> |
| 14 | + <h1>PostHTML Loader</h1> |
| 15 | +</div> |
| 16 | + |
| 17 | +<h2 align="center">Install</h2> |
| 18 | + |
| 19 | +```bash |
| 20 | +npm i -D posthtml-loader |
| 21 | +``` |
7 | 22 |
|
8 |
| -# PostHTML Loader <img align="right" width="200" height="220" title="PostHTML logo" src="http://posthtml.github.io/posthtml/logo.svg"> |
| 23 | +<h2 align="center">Usage</h2> |
9 | 24 |
|
10 |
| -## Install |
| 25 | +```js |
| 26 | +import html from './file.html' |
| 27 | +``` |
11 | 28 |
|
12 |
| -```sh |
13 |
| -npm i -D html-loader posthtml-loader |
| 29 | +**webpack.config.js** |
| 30 | +```js |
| 31 | +module: { |
| 32 | + rules: [ |
| 33 | + { |
| 34 | + test: /\.html$/, |
| 35 | + use: [ |
| 36 | + 'html-loader', |
| 37 | + { |
| 38 | + loader: 'posthtml-loader', |
| 39 | + options: { |
| 40 | + ident: 'posthtml', |
| 41 | + parser: 'PostHTML Parser' |
| 42 | + plugins: [ |
| 43 | + /* PostHTML Plugins */ |
| 44 | + require('posthtml-plugin')(options) |
| 45 | + ] |
| 46 | + } |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | + ] |
| 51 | +}, |
14 | 52 | ```
|
15 | 53 |
|
16 |
| -## Usage |
| 54 | +<h2 align="center">Options</h2> |
17 | 55 |
|
18 |
| -The posthtml loader must be used with at least one other loader in order to integrate with webpack correctly. For most use cases, the [html-loader](https://github.com/webpack/html-loader) is recommended. If you want to export the html string directly for use in javascript or webpack plugins, we recommend the [source-loader](https://github.com/static-dev/source-loader). Whichever loader you choose, it should be the first loader, followed by posthtml, as you will see in the examples below. |
| 56 | +|Name|Type|Default|Description| |
| 57 | +|:--:|:--:|:-----:|:----------| |
| 58 | +|**[`config`](#config)**|`{Object}`|`undefined`|PostHTML Config| |
| 59 | +|**[`parser`](#parser)**|`{String/Function}`|`undefined`|PostHTML Parser| |
| 60 | +|**[`plugins`](#plugins)**|`{Array/Function}`|`[]`|PostHTML Plugins| |
19 | 61 |
|
20 |
| -Options can be passed through a `posthtml` option directly on the webpack config object. It accepts an array, an object, or a function that returns an array or object. If it's an array, it should contain plugins. If it's an object, it can contain a `plugins` key, which is an array of plugins and an optional `parser` key which allows you to pass in a custom parser. Any other key will apply to the `pack` querystring parameter, documented below. |
| 62 | +### `Config` |
21 | 63 |
|
22 |
| -Basic configuration example: |
| 64 | +|Name|Type|Default|Description| |
| 65 | +|:--:|:--:|:-----:|:----------| |
| 66 | +|**[`path`](#path)**|`{String}`|`loader.resourcePath`|PostHTML Config Path| |
| 67 | +|**[`ctx`](#context)**|`{Object}`|`{}`|PostHTML Config Context| |
23 | 68 |
|
24 |
| -```js |
25 |
| -// webpack.config.js |
26 |
| -module: { |
27 |
| - loaders: [{ |
28 |
| - test: /\.html$/, |
29 |
| - loader: 'html!posthtml' |
30 |
| - }] |
31 |
| -}, |
32 |
| -posthtml: [/* plugins here */] |
33 |
| -``` |
| 69 | +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 placed 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 |
34 | 70 |
|
35 |
| -## Options |
| 71 | +``` |
| 72 | +|– src |
| 73 | +||– components |
| 74 | +|||– component.html |
| 75 | +|||– posthtml.config.js (components) |
| 76 | +||– index.html |
| 77 | +| |
| 78 | +|– posthtml.config.js (index) |
| 79 | +|– webpack.config.js |
| 80 | +``` |
36 | 81 |
|
37 |
| -### Plugin Packages |
| 82 | +#### `Path` |
38 | 83 |
|
39 |
| -If you need to apply different sets of plugins to different groups of files, you can use a **plugin pack**. Just add `pack=[name]` as a querystring option, and return an object from the `posthtml` config option with a key matching the pack name, and the value being an array of plugins. |
| 84 | +If you normally place all your config files in a separate folder e.g `./config` it is necessary to explicitly set the config path in `webpack.config.js` |
40 | 85 |
|
| 86 | +**webpack.config.js** |
41 | 87 | ```js
|
42 |
| -// webpack.config.js |
43 |
| -module: { |
44 |
| - loaders: [{ |
45 |
| - test: /\\.special\.html$/, |
46 |
| - loader: 'html!posthtml?pack=special' |
47 |
| - }] |
48 |
| -}, |
49 |
| -posthtml: { |
50 |
| - plugins: [/* plugins that apply anything that's not using a pack */], |
51 |
| - special: [ /* plugins specific to the "special" pack */ ], |
| 88 | +{ |
| 89 | + loader: 'posthtml-loader', |
| 90 | + options: { |
| 91 | + config: { |
| 92 | + path: 'path/to/.config/' |
| 93 | + } |
| 94 | + } |
52 | 95 | }
|
53 | 96 | ```
|
54 | 97 |
|
55 |
| -### Custom Parser |
| 98 | +#### `Context` |
56 | 99 |
|
57 |
| -If you want to use a custom parser, you can pass it in under the `parser` key or as query string in the loader. Below is an example with the [sugarml parser](https://github.com/posthtml/sugarml): |
| 100 | +|Name|Type|Default|Description| |
| 101 | +|:--:|:--:|:-----:|:----------| |
| 102 | +|`env`|`{String}`|`'development'`|process.env.NODE_ENV| |
| 103 | +|`file`|`{Object}`|`{ dirname, basename, extname }`|File| |
| 104 | +|`options`|`{Object}`|`{}`|Plugin Options (Context)| |
58 | 105 |
|
| 106 | +[**posthtml.config.js**](https://github.com/posthtml/posthtml-load-config) |
59 | 107 | ```js
|
60 |
| -// webpack.config.js |
61 |
| -const sugarml = require('sugarml') |
| 108 | +module.exports = ({ file, options, env }) => ({ |
| 109 | + parser: 'posthtml-sugarml' |
| 110 | + plugins: { |
| 111 | + 'posthtml-include': options.include |
| 112 | + 'posthtml-content': options.content |
| 113 | + 'htmlnano': env === 'production' ? {} : false |
| 114 | + } |
| 115 | +}) |
| 116 | +``` |
62 | 117 |
|
63 |
| -module: { |
64 |
| - loaders: [{ |
65 |
| - test: /\\.special\.html$/, |
66 |
| - loader: 'html!posthtml' |
67 |
| - }] |
68 |
| -}, |
69 |
| -posthtml: { |
70 |
| - plugins: [/* posthtml plugins */], |
71 |
| - parser: sugarml |
| 118 | +**webpack.config.js** |
| 119 | +```js |
| 120 | +{ |
| 121 | + loader: 'posthtml-loader', |
| 122 | + options: { |
| 123 | + config: { |
| 124 | + ctx: { |
| 125 | + include: {...options} |
| 126 | + content: {...options} |
| 127 | + } |
| 128 | + } |
| 129 | + } |
72 | 130 | }
|
73 | 131 | ```
|
74 | 132 |
|
| 133 | +### `Parser` |
| 134 | + |
| 135 | +If you want to use a custom parser e.g [SugarML](https://github.com/posthtml/sugarml), you can pass it in under the `parser` key in the loader options |
| 136 | + |
| 137 | +#### `{String}` |
| 138 | + |
| 139 | +**webpack.config.js** |
75 | 140 | ```js
|
76 |
| -// webpack.config.js |
77 |
| -const sugarml = require('sugarml') |
| 141 | +{ |
| 142 | + loader: 'posthtml-loader', |
| 143 | + options: { |
| 144 | + parser: 'posthtml-sugarml' |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
78 | 148 |
|
79 |
| -module: { |
80 |
| - loaders: [{ |
81 |
| - test: /\\.special\.html$/, |
82 |
| - loader: 'html!posthtml?parser=sugarml' |
83 |
| - }] |
84 |
| -}, |
85 |
| -posthtml: { |
86 |
| - plugins: [/* posthtml plugins */] |
| 149 | +#### `{Function}` |
| 150 | + |
| 151 | +**webpack.config.js** |
| 152 | +```js |
| 153 | +{ |
| 154 | + loader: 'posthtml-loader', |
| 155 | + options: { |
| 156 | + parser: require('posthtml-sugarml')() |
| 157 | + } |
87 | 158 | }
|
88 | 159 | ```
|
89 | 160 |
|
90 |
| -### Using a Function |
| 161 | +### `Plugins` |
| 162 | + |
| 163 | +Plugins are specified under the `plugins` key in the loader options |
91 | 164 |
|
92 |
| -You can also return a function from the `posthtml` config value, if you need to for any reason. The function passes along the [loader context](https://webpack.github.io/docs/loaders.html#loader-context) as an argument, so you can get information about the file currently being processed from this and pass it to plugins if needed. For example: |
| 165 | +#### `{Array}` |
93 | 166 |
|
| 167 | +**webpack.config.js** |
94 | 168 | ```js
|
95 |
| -// webpack.config.js |
96 |
| -module: { |
97 |
| - loaders: [{ |
98 |
| - test: /\.html$/, |
99 |
| - loader: 'html!posthtml' |
100 |
| - }] |
101 |
| -}, |
102 |
| -posthtml: (ctx) => { |
103 |
| - return [ plugin({ filename: ctx.resourcePath })] |
| 169 | +{ |
| 170 | + loader: 'posthtml-loader', |
| 171 | + options: { |
| 172 | + plugins: [ |
| 173 | + require('posthtml-plugin')() |
| 174 | + ] |
| 175 | + } |
104 | 176 | }
|
105 | 177 | ```
|
106 | 178 |
|
107 |
| -## Maintainers |
| 179 | +#### `{Function}` |
| 180 | + |
| 181 | +**webpack.config.js** |
| 182 | +```js |
| 183 | +{ |
| 184 | + loader: 'posthtml-loader', |
| 185 | + options: { |
| 186 | + plugins (loader) { |
| 187 | + return [ |
| 188 | + require('posthtml-plugin')() |
| 189 | + ] |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +<h2 align="center">Maintainer</h2> |
108 | 196 |
|
109 | 197 | <table>
|
110 | 198 | <tbody>
|
111 | 199 | <tr>
|
112 | 200 | <td align="center">
|
113 | 201 | <img width="150 height="150"
|
114 |
| - src="https://avatars.githubusercontent.com/u/5419992?v=3&s=150"> |
| 202 | + src="https://github.com/michael-ciniawsky.png?v=3&s=150"> |
115 | 203 | <br />
|
116 | 204 | <a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
|
117 | 205 | </td>
|
| 206 | + </tr> |
| 207 | + <tbody> |
| 208 | +</table> |
| 209 | + |
| 210 | +<h2 align="center">Contributors</h2> |
| 211 | + |
| 212 | +<table> |
| 213 | + <tbody> |
| 214 | + <tr> |
118 | 215 | <td align="center">
|
119 |
| - <img width="150 height="150" |
120 |
| - src="https://avatars.githubusercontent.com/u/556932?v=3&s=150"> |
121 |
| - <br /> |
122 |
| - <a href="https://github.com/jescalan">Jeff Escalante</a> |
123 |
| - </td> |
124 |
| - <td align="center"> |
125 |
| - <img width="150" height="150" src="https://avatars.githubusercontent.com/u/2789192?v=3&s=150"> |
| 216 | + <img width="150" height="150" src="https://github.com/Gitscrum.png?v=3&s=150"> |
126 | 217 | <br />
|
127 | 218 | <a href="https://github.com/Gitscrum">Ivan Demidov</a>
|
128 | 219 | </td>
|
129 | 220 | </tr>
|
130 | 221 | <tbody>
|
131 | 222 | </table>
|
132 | 223 |
|
133 |
| -## Contributing |
134 |
| - |
135 |
| -See [PostHTML Guidelines](https://github.com/posthtml/posthtml/tree/master/docs) and [CONTRIBUTING](CONTRIBUTING.md). |
136 |
| - |
137 |
| -## LICENSE |
138 |
| - |
139 |
| -[MIT](LICENSE) |
140 | 224 |
|
141 | 225 | [npm]: https://img.shields.io/npm/v/posthtml-loader.svg
|
142 | 226 | [npm-url]: https://npmjs.com/package/posthtml-loader
|
143 | 227 |
|
| 228 | +[node]: https://img.shields.io/node/v/posthtml-loader.svg |
| 229 | +[node-url]: https://nodejs.org/ |
| 230 | + |
144 | 231 | [deps]: https://david-dm.org/posthtml/posthtml-loader.svg
|
145 | 232 | [deps-url]: https://david-dm.org/posthtml/posthtml-loader
|
146 | 233 |
|
147 |
| -[build]: http://img.shields.io/travis/posthtml/posthtml-loader.svg |
148 |
| -[build-url]: https://travis-ci.org/posthtml/posthtml-loader |
| 234 | +[tests]: http://img.shields.io/travis/posthtml/posthtml-loader.svg |
| 235 | +[tests-url]: https://travis-ci.org/posthtml/posthtml-loader |
149 | 236 |
|
150 |
| -[cover]: https://coveralls.io/repos/github/posthtml/posthtml-loader/badge.svg?branch=master |
151 |
| -[cover-url]: https://coveralls.io/github/posthtml/posthtml-loader?branch=master |
| 237 | +[cover]: https://coveralls.io/repos/github/posthtml/posthtml-loader/badge.svg |
| 238 | +[cover-url]: https://coveralls.io/github/posthtml/posthtml-loader |
152 | 239 |
|
153 | 240 | [style]: https://img.shields.io/badge/code%20style-standard-yellow.svg
|
154 | 241 | [style-url]: http://standardjs.com/
|
155 | 242 |
|
156 | 243 | [chat]: https://badges.gitter.im/posthtml/posthtml.svg
|
157 |
| -[chat-badge]: https://gitter.im/posthtml/posthtml?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" |
| 244 | +[chat-url]: https://gitter.im/posthtml/posthtml |
0 commit comments