Skip to content

Commit 9f76428

Browse files
committed
Docs: initial usage documentation
1 parent ea33eab commit 9f76428

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,144 @@ To install, simply run:
1111
npm install --save inline-html-template-plugin
1212
```
1313

14+
## The Problem
15+
16+
Sometimes, you need to inject javascript into an HTML file. Webpack handles this beautifully using HtmlWebpackPlugin. HtmlWebpackPlugin can also do a number of other dynamic things to generate an HTML file that's complete and ready for production.
17+
18+
In some cases, particularly with Web Components, you might need to inject HTML into your javascript. In the case of Web Components, your CSS will need to be inlined, and you'll want the CSS to be dynamically generated based on all the CSS imported throughout your app.
19+
20+
## The Solution
21+
22+
The InlineHTMLTemplatePlugin allows you to inline your finalized HTML as a template into any Javascript file. In Web Components, this means that you can use the completed HTML as a template, and your entire Web Component will be packaged into a single javascript file.
23+
1424
## Usage
1525

26+
A case for Web Components, as described above, might look like the following:
27+
28+
### webpack.config.js
29+
30+
```javascript
31+
const HtmlWebpackPlugin = require('html-webpack-plugin');
32+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
33+
const HTMLInlineCSSWebpackPlugin = require('html-inline-css-webpack-plugin')
34+
.default;
35+
const InlineHTMLTemplatePlugin = require('inline-html-template-plugin').default;
36+
37+
module.exports = {
38+
mode: 'development',
39+
entry: {
40+
component: __dirname + '/src/component.js'
41+
},
42+
module: {
43+
rules: [
44+
{
45+
test: /\.js$/,
46+
exclude: /(node_modules)/,
47+
use: {
48+
loader: 'babel-loader',
49+
options: {
50+
presets: ['@babel/preset-env'],
51+
plugins: [
52+
'@babel/plugin-proposal-object-rest-spread',
53+
'@babel/plugin-transform-runtime',
54+
'@babel/plugin-proposal-class-properties'
55+
]
56+
}
57+
}
58+
},
59+
{
60+
test: /\.css$/i,
61+
use: [
62+
{
63+
loader: MiniCssExtractPlugin.loader,
64+
options: {
65+
sourceMap: false
66+
}
67+
},
68+
{
69+
loader: 'css-loader',
70+
options: {
71+
modules: true
72+
}
73+
}
74+
]
75+
}
76+
]
77+
},
78+
plugins: [
79+
new HtmlWebpackPlugin({
80+
template: 'src/component-template.html',
81+
filename: 'component-template.html',
82+
inject: false
83+
}),
84+
new MiniCssExtractPlugin(),
85+
new HTMLInlineCSSWebpackPlugin({
86+
replace: {
87+
target: '<!-- inline css -->',
88+
removeTarget: true
89+
}
90+
}),
91+
new InlineHTMLTemplatePlugin()
92+
]
93+
};
94+
```
95+
96+
The key portion is this:
97+
98+
```javascript
99+
plugins: [
100+
// load the template; give it a filename
101+
new HtmlWebpackPlugin({
102+
template: 'src/component-template.html',
103+
filename: 'component-template.html',
104+
// to avoid a circular reference, do not inject javascript
105+
inject: false
106+
}),
107+
// extract the css into a file
108+
new MiniCssExtractPlugin(),
109+
// inline that CSS into your HTML
110+
new HTMLInlineCSSWebpackPlugin({
111+
replace: {
112+
target: '<!-- inline css -->',
113+
removeTarget: true
114+
}
115+
}),
116+
// inject the finalized HTML as a string into your javascript
117+
new InlineHTMLTemplatePlugin()
118+
];
119+
```
120+
121+
### component-template.html
122+
123+
Your html might look like this:
124+
125+
```html
126+
<div id="app-container">
127+
<!-- inline css -->
128+
<div id="mount"></div>
129+
</div>
130+
```
131+
132+
### component.js
133+
134+
Your component javascript might look something like this, where `myApp` is a javascript solution for initializing an application on a DOM node.
135+
136+
```javascript
137+
import myApp from './myApp';
138+
const loadedTemplate = '/* InlineHTML: component-template.html */';
139+
140+
class WebpackComponent extends HTMLElement {
141+
connectedCallback() {
142+
this.attachShadow({ open: true });
143+
this.shadowRoot.innerHTML = loadedTemplate;
144+
myApp.initialize(this.shadowRoot.querySelector('#mount'));
145+
}
146+
}
147+
customElements.define('webpack-component', WebpackComponent);
148+
```
149+
150+
In this javascript, the InlineHtmlTemplatePlugin looks for `"/* InlineHTML: component-template.html */"`, parses the filename from this string, and replaces the string with your finalized HTML Template.
151+
16152
## Contributing
17153

18154
This package uses `semantic-release`. Changes will be compiled into a changelog and the package versioned, tagged and published automatically.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "inline-html-template-plugin",
33
"version": "0.0.1",
4-
"description": "",
4+
"description": "A Webpack plugin for inlining fully rendered HTML as a template string.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [
@@ -18,8 +18,16 @@
1818
"url": "git+https://github.com/WTW-IM/inline-html-template-plugin.git"
1919
},
2020
"keywords": [
21-
"HTMLElement",
22-
"Web Components"
21+
"Web Components",
22+
"webpack",
23+
"webpack4",
24+
"webpack-plugin",
25+
"inline",
26+
"internal",
27+
"embedded",
28+
"document",
29+
"html",
30+
"markup"
2331
],
2432
"author": "Steve Matney",
2533
"license": "ISC",

0 commit comments

Comments
 (0)