html-bundle 6.0.1 → 6.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -10
- package/dist/bundle.mjs +2 -2
- package/dist/utils.mjs +23 -2
- package/example.gif +0 -0
- package/package.json +4 -3
- package/src/bundle.mts +4 -2
- package/src/utils.mts +23 -2
- package/output.JPG +0 -0
package/README.md
CHANGED
|
@@ -16,6 +16,10 @@ A (primarily) zero-config bundler for HTML files. The idea is to use HTML as Sin
|
|
|
16
16
|
- 🚋 Watcher on PostCSS and Tailwind CSS and TS Config
|
|
17
17
|
- 🛡️ Almost no need to restart
|
|
18
18
|
|
|
19
|
+
## Demo
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
19
23
|
## Installation and Usage
|
|
20
24
|
|
|
21
25
|
```properties
|
|
@@ -43,7 +47,7 @@ $ npm run build
|
|
|
43
47
|
|
|
44
48
|
`--hmr`: boots up a static server and enables Hot Module Replacement. **This generates a development build and works best when not triggered from the main index.html**<br>
|
|
45
49
|
`--secure`: creates a secure HTTP2 over HTTPS instance. This requires the files `localhost.pem` and `localhost-key.pem` in the root folder. You can generate them with [mkcert](https://github.com/FiloSottile/mkcert) for instance.<br>
|
|
46
|
-
`--
|
|
50
|
+
`--isCritical`: uses critical to extract and inline critical-path CSS to HTML.<br>
|
|
47
51
|
`--handler`: path to your custom handler. Here, you can handle all non-supported files. You can get the filename via `process.argv[2]`.
|
|
48
52
|
|
|
49
53
|
## Optional Config
|
|
@@ -61,7 +65,7 @@ Generate the config in the root and call it "bundle.config.js"
|
|
|
61
65
|
|
|
62
66
|
## Concept
|
|
63
67
|
|
|
64
|
-
The bundler always globs all HTML, CSS and TS/JS files from the `src` (config) directory and processes them to the `build` (config) directory. PostCSS is being used for CSS files and inline styles, html-minifier-terser for HTML and esbuild to bundle, minify, etc. for inline and referenced TS/JS. Server-sent events and [hydro-js](https://github.com/Krutsch/hydro-js) are used for HMR.
|
|
68
|
+
The bundler always globs all HTML, CSS and TS/JS files from the `src` (config) directory and processes them to the `build` (config) directory. PostCSS is being used for CSS files and inline styles, html-minifier-terser for HTML and esbuild to bundle, minify, etc. for inline and referenced TS/JS. Server-sent events and [hydro-js](https://github.com/Krutsch/hydro-js) are used for HMR. In order to trigger SPA Routers, the popstate event is being triggered after HMR Operations.
|
|
65
69
|
|
|
66
70
|
## Example hydro-js
|
|
67
71
|
|
|
@@ -100,10 +104,6 @@ Set `"jsxFactory": "h"` in `tsconfig.json` for JSX.
|
|
|
100
104
|
</html>
|
|
101
105
|
```
|
|
102
106
|
|
|
103
|
-
### Output
|
|
104
|
-
|
|
105
|
-

|
|
106
|
-
|
|
107
107
|
## Example Vue.js@next
|
|
108
108
|
|
|
109
109
|
Set `"jsxFactory": "h"` in `tsconfig.json`.
|
|
@@ -173,7 +173,3 @@ Set `"jsxFactory": "h"` in `tsconfig.json`.
|
|
|
173
173
|
</body>
|
|
174
174
|
</html>
|
|
175
175
|
```
|
|
176
|
-
|
|
177
|
-
### Demo
|
|
178
|
-
|
|
179
|
-

|
package/dist/bundle.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { getTagName, findElements } from "@web/parse5-utils";
|
|
|
14
14
|
import awaitSpawn from "await-spawn";
|
|
15
15
|
import { fileCopy, createDefaultServer, getPostCSSConfig, getBuildPath, createDir, bundleConfig, serverSentEvents, addHMRCode, } from "./utils.mjs";
|
|
16
16
|
const isHMR = process.argv.includes("--hmr") || bundleConfig.hmr;
|
|
17
|
-
const isCritical = process.argv.includes("--
|
|
17
|
+
const isCritical = process.argv.includes("--isCritical") || bundleConfig.isCritical;
|
|
18
18
|
const isSecure = process.argv.includes("--secure") || bundleConfig.secure; // uses CSP for critical too
|
|
19
19
|
const handlerFile = process.argv.includes("--handler")
|
|
20
20
|
? process.argv[process.argv.indexOf("--handler") + 1]
|
|
@@ -204,11 +204,11 @@ async function minifyCode() {
|
|
|
204
204
|
missingPkg = true;
|
|
205
205
|
const packageNameRegex = /(?<=").*(?=")/;
|
|
206
206
|
const [pkgName] = error.text.match(packageNameRegex);
|
|
207
|
-
console.log(`📦 Package ${pkgName} was installed for you`);
|
|
208
207
|
await awaitSpawn(process.platform === "win32" ? "npm.cmd" : "npm", [
|
|
209
208
|
"install",
|
|
210
209
|
pkgName,
|
|
211
210
|
]);
|
|
211
|
+
console.log(`📦 Package ${pkgName} was installed for you`);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
if (missingPkg) {
|
package/dist/utils.mjs
CHANGED
|
@@ -109,6 +109,7 @@ function randomText() {
|
|
|
109
109
|
function getHMRCode(file, id, src) {
|
|
110
110
|
return `import { render, html, $, $$, setShouldSetReactivity } from "hydro-js";
|
|
111
111
|
window.isHMR = true;
|
|
112
|
+
window.lastCalled = new Map();
|
|
112
113
|
if (!window.eventsource${id}) {
|
|
113
114
|
window.eventsource${id} = new EventSource("/hmr");
|
|
114
115
|
window.eventsource${id}.addEventListener('error', (e) => {
|
|
@@ -156,15 +157,35 @@ function getHMRCode(file, id, src) {
|
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
}
|
|
160
|
+
|
|
161
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
162
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
163
|
+
})
|
|
164
|
+
if (dataObj.html.includes("<script")) updateElem("script");
|
|
165
|
+
|
|
159
166
|
|
|
160
167
|
if (dataObj.file === \`${src}/index.html\`) {
|
|
161
168
|
dispatchEvent(new Event("popstate"));
|
|
162
169
|
}
|
|
163
170
|
} else if (dataObj.file.endsWith(".css")) {
|
|
164
|
-
|
|
171
|
+
const now = performance.now();
|
|
172
|
+
if (!window.lastCalled.has(dataObj.file) || now - window.lastCalled.get(dataObj.file) > 100) {
|
|
173
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
174
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
175
|
+
})
|
|
176
|
+
window.lastCalled.set(dataObj.file, now)
|
|
177
|
+
}
|
|
165
178
|
} else if (dataObj.file.endsWith(".js")) {
|
|
166
|
-
|
|
179
|
+
const now = performance.now();
|
|
180
|
+
if (!window.lastCalled.has(dataObj.file) || now - window.lastCalled.get(dataObj.file) > 100) {
|
|
181
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
182
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
183
|
+
})
|
|
184
|
+
updateElem("script");
|
|
185
|
+
window.lastCalled.set(dataObj.file, now)
|
|
186
|
+
}
|
|
167
187
|
}
|
|
188
|
+
|
|
168
189
|
|
|
169
190
|
function updateElem(type) {
|
|
170
191
|
const hmrId = "${id}";
|
package/example.gif
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-bundle",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.5",
|
|
4
4
|
"description": "A very simple bundler for HTML SFC",
|
|
5
5
|
"bin": "./dist/bundle.mjs",
|
|
6
6
|
"scripts": {
|
|
@@ -36,11 +36,12 @@
|
|
|
36
36
|
"chokidar": "^3.5.2",
|
|
37
37
|
"critical": "^4.0.1",
|
|
38
38
|
"cssnano": "^5.0.14",
|
|
39
|
-
"esbuild": "^0.14.
|
|
40
|
-
"fastify": "^3.25.
|
|
39
|
+
"esbuild": "^0.14.9",
|
|
40
|
+
"fastify": "^3.25.3",
|
|
41
41
|
"fastify-static": "^4.5.0",
|
|
42
42
|
"glob": "^7.2.0",
|
|
43
43
|
"html-minifier-terser": "^6.1.0",
|
|
44
|
+
"hydro-js": "^1.5.3",
|
|
44
45
|
"parse5": "^6.0.1",
|
|
45
46
|
"postcss": "^8.4.5",
|
|
46
47
|
"postcss-load-config": "^3.1.0"
|
package/src/bundle.mts
CHANGED
|
@@ -27,7 +27,8 @@ import {
|
|
|
27
27
|
} from "./utils.mjs";
|
|
28
28
|
|
|
29
29
|
const isHMR = process.argv.includes("--hmr") || bundleConfig.hmr;
|
|
30
|
-
const isCritical =
|
|
30
|
+
const isCritical =
|
|
31
|
+
process.argv.includes("--isCritical") || bundleConfig.isCritical;
|
|
31
32
|
const isSecure = process.argv.includes("--secure") || bundleConfig.secure; // uses CSP for critical too
|
|
32
33
|
const handlerFile = process.argv.includes("--handler")
|
|
33
34
|
? process.argv[process.argv.indexOf("--handler") + 1]
|
|
@@ -243,12 +244,13 @@ async function minifyCode(): Promise<unknown> {
|
|
|
243
244
|
missingPkg = true;
|
|
244
245
|
const packageNameRegex = /(?<=").*(?=")/;
|
|
245
246
|
const [pkgName] = error.text.match(packageNameRegex);
|
|
246
|
-
console.log(`📦 Package ${pkgName} was installed for you`);
|
|
247
247
|
|
|
248
248
|
await awaitSpawn(process.platform === "win32" ? "npm.cmd" : "npm", [
|
|
249
249
|
"install",
|
|
250
250
|
pkgName,
|
|
251
251
|
]);
|
|
252
|
+
|
|
253
|
+
console.log(`📦 Package ${pkgName} was installed for you`);
|
|
252
254
|
}
|
|
253
255
|
}
|
|
254
256
|
|
package/src/utils.mts
CHANGED
|
@@ -145,6 +145,7 @@ function randomText() {
|
|
|
145
145
|
function getHMRCode(file: string, id: string, src: string) {
|
|
146
146
|
return `import { render, html, $, $$, setShouldSetReactivity } from "hydro-js";
|
|
147
147
|
window.isHMR = true;
|
|
148
|
+
window.lastCalled = new Map();
|
|
148
149
|
if (!window.eventsource${id}) {
|
|
149
150
|
window.eventsource${id} = new EventSource("/hmr");
|
|
150
151
|
window.eventsource${id}.addEventListener('error', (e) => {
|
|
@@ -192,15 +193,35 @@ function getHMRCode(file: string, id: string, src: string) {
|
|
|
192
193
|
}
|
|
193
194
|
}
|
|
194
195
|
}
|
|
196
|
+
|
|
197
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
198
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
199
|
+
})
|
|
200
|
+
if (dataObj.html.includes("<script")) updateElem("script");
|
|
201
|
+
|
|
195
202
|
|
|
196
203
|
if (dataObj.file === \`${src}/index.html\`) {
|
|
197
204
|
dispatchEvent(new Event("popstate"));
|
|
198
205
|
}
|
|
199
206
|
} else if (dataObj.file.endsWith(".css")) {
|
|
200
|
-
|
|
207
|
+
const now = performance.now();
|
|
208
|
+
if (!window.lastCalled.has(dataObj.file) || now - window.lastCalled.get(dataObj.file) > 100) {
|
|
209
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
210
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
211
|
+
})
|
|
212
|
+
window.lastCalled.set(dataObj.file, now)
|
|
213
|
+
}
|
|
201
214
|
} else if (dataObj.file.endsWith(".js")) {
|
|
202
|
-
|
|
215
|
+
const now = performance.now();
|
|
216
|
+
if (!window.lastCalled.has(dataObj.file) || now - window.lastCalled.get(dataObj.file) > 100) {
|
|
217
|
+
$$('link[rel="stylesheet"][href]').forEach(link => {
|
|
218
|
+
link.setAttribute("href", link.getAttribute("href") + "?v=" + String(Math.random().toFixed(4)).slice(2));
|
|
219
|
+
})
|
|
220
|
+
updateElem("script");
|
|
221
|
+
window.lastCalled.set(dataObj.file, now)
|
|
222
|
+
}
|
|
203
223
|
}
|
|
224
|
+
|
|
204
225
|
|
|
205
226
|
function updateElem(type) {
|
|
206
227
|
const hmrId = "${id}";
|
package/output.JPG
DELETED
|
Binary file
|