Skip to content

Commit 866a98e

Browse files
docs(README): add url intro (options.url)
1 parent 8622775 commit 866a98e

File tree

1 file changed

+34
-68
lines changed

1 file changed

+34
-68
lines changed

README.md

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ By default all assets (`<img src="./image.png">`) are transpiled to their own mo
4141

4242
### `Caching`
4343

44-
If your application includes many HTML Components or certain HTML Components are of significant size, we highly recommend to use the [`cache-loader`](https://github.com/webpack-contrib/cache-loader) for persistent caching (faster rebuilds)
44+
If your application includes many HTML Components or certain HTML Components are of significant size, we highly recommend to use the [`cache-loader`](https://github.com/webpack-contrib/cache-loader) for persistent caching (faster initial builds)
4545

4646
**webpack.config.js**
4747
```js
@@ -69,6 +69,8 @@ If your application includes many HTML Components or certain HTML Components are
6969

7070
### `url`
7171

72+
It's possible to completely disable or filter certain URL's from resolving in case these assets shouldn't be handled by `webpack`. Protocol URL's like (`<img src="https://cnd.domain.com/image.png">`) are ignored by default
73+
7274
#### `{Boolean}`
7375

7476
**webpack.config.js**
@@ -161,6 +163,8 @@ If your application includes many HTML Components or certain HTML Components are
161163

162164
### `template`
163165

166+
When set to `true` the loader will export a template `{Function}` instead of a `{String}`. The `locals` param is configurable and defaults to `_`
167+
164168
#### `{Boolean}`
165169

166170
**file.html**
@@ -253,9 +257,10 @@ Set custom [options](https://github.com/posthtml/htmlnano#modules) for minificat
253257

254258
**component.js**
255259
```js
256-
import template from "./component.html";
260+
import template from './component.html';
257261

258262
const component = document.createElement('div')
263+
259264
component.innerHTML = template({ hello: 'Hello World!' })
260265

261266
document.body.appendChild(component);
@@ -265,95 +270,56 @@ if (module.hot) {
265270
// Capture hot update
266271
module.hot.accept('./component.html', () => {
267272
// Replace old content with the hot loaded one
268-
component.innerHTML = template({...locals})
273+
component.innerHTML = template({ ...locals })
269274
})
270275
}
271276
```
272277

273-
### `Extract`
274-
275-
A very common scenario is exporting the HTML into their own `.html` file, to
276-
serve them directly instead of injecting with javascript. This can be achieved
277-
with a combination of following 3 loaders
278+
### `npm Packages (Modules)`
278279

279-
- [file-loader](https://github.com/webpack/file-loader)
280-
- [extract-loader](https://github.com/peerigon/extract-loader)
281-
- html-loader
280+
> :information_source: Any key matching with `resolve.mainFields` is valid and should work. `pkg.template` is just used as an example here.
282281
283-
The `html-loader` will parse the URLs, require the images and everything you
284-
expect. The `extract-loader` will parse the javascript back into a proper HTML
285-
file, ensuring images are required and point to the proper path, and finally the `file-loader` will write the `.html` file for you
286-
287-
**webpack.config.js**
288-
```js
282+
**package.json**
283+
```json
289284
{
290-
test: /\.html$/,
291-
use: [
292-
{
293-
loader: 'file-loader'
294-
options: { name: '[path][name].[ext]'}
295-
},
296-
'extract-loader'
297-
'html-loader'
298-
]
299-
}
300-
```
301-
302-
### `CSS Modules`
303-
304-
**file.css**
305-
```css
306-
.container {
307-
color: red;
285+
"name": "@package",
286+
"version": "1.0.0",
287+
"template": "path/to/component.html"
308288
}
309289
```
310290

311-
**file.html**
312-
```html
313-
<div class=${ _.container }></div>
314-
```
315-
316291
**webpack.config.js**
317292
```js
318-
[
319-
{
320-
test: /\.html$/
321-
use: {
322-
loader: 'html-loader'
323-
options: {
324-
template: true
325-
}
326-
}
327-
},
328-
{
329-
test: /\.css$/
330-
use: [
331-
'style-loader',
332-
'css-loader'
293+
const config = {
294+
module: {
295+
rules: [
333296
{
334-
loader: 'postcss-loader',
335-
options: {
336-
ident: 'postcss',
337-
plugins () {
338-
return [
339-
require('postcss-modules')()
340-
]
341-
}
297+
test: /\.html$/,
298+
use: [ 'html-loader' ],
299+
resolve: {
300+
mainFields: [ 'template' ]
342301
}
343302
}
344303
]
345304
}
346-
]
305+
}
306+
307+
module.exports = config
308+
```
309+
310+
**component.html**
311+
```html
312+
<import src="@package"></import>
313+
<div>...<div>
347314
```
348315

349316
**component.js**
350317
```js
351-
import * as styles from './file.css'
352-
import template from './file.html'
318+
import html from './file.html'
353319

354-
const html = template({ ...styles })
320+
const el = document.createElement('div')
355321

356-
document.body.innerHTML = html
322+
el.innerHTML = html
357323
```
358324

359325
<h2 align="center">Maintainers</h2>

0 commit comments

Comments
 (0)