Skip to content

Commit 29c2a4e

Browse files
authored
Merge pull request #1 from WTW-IM/initial-setup
Initial Setup
2 parents f89076a + 9f76428 commit 29c2a4e

File tree

12 files changed

+13873
-0
lines changed

12 files changed

+13873
-0
lines changed

.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
parserOptions: {
5+
project: ['./tsconfig.json']
6+
},
7+
plugins: ['@typescript-eslint'],
8+
extends: [
9+
'plugin:@typescript-eslint/eslint-recommended',
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
12+
'prettier/@typescript-eslint'
13+
],
14+
rules: {
15+
'no-underscore-dangle': 'off',
16+
'max-len': ['error', { code: 150 }],
17+
'@typescript-eslint/ban-ts-ignore': 0,
18+
'class-methods-use-this': 0
19+
}
20+
};

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
node_modules
5+
/.pnp
6+
.pnp.js
7+
/.cache
8+
dist
9+
10+
# testing
11+
/coverage
12+
13+
# production
14+
/dist
15+
16+
# misc
17+
.DS_Store
18+
.env.local
19+
.env.development.local
20+
.env.test.local
21+
.env.production.local
22+
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: node_js
2+
node_js:
3+
- lts/*
4+
- node
5+
6+
stages:
7+
- test
8+
- name: release
9+
if: branch = master && type != pull_request
10+
11+
jobs:
12+
include:
13+
- stage: release
14+
node_js: node
15+
script: skip
16+
deploy:
17+
provider: script
18+
skip_cleanup: true
19+
script: npx semantic-release

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.

commitlint.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'type-enum': [
5+
2,
6+
'always',
7+
['Fix', 'Update', 'Breaking', 'Docs', 'Build', 'New', 'Upgrade']
8+
],
9+
'type-case': [2, 'always', 'pascal-case']
10+
}
11+
};

0 commit comments

Comments
 (0)