Skip to content

Commit 625813c

Browse files
authored
refactor(demo): build demo using rollup (microformats#242)
1 parent 3ddfc9c commit 625813c

11 files changed

+253
-5652
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"plugin:import/typescript",
77
"prettier"
88
],
9-
"ignorePatterns": ["node_modules/", "dist/", "public/"],
9+
"ignorePatterns": ["node_modules/", "dist/", "public/", "**/*.html"],
1010
"settings": {
1111
"import/resolver": {
1212
"node": { "extensions": [".ts"] }

.github/workflows/build_and_test.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ jobs:
2828
run: yarn test
2929
- name: Build package
3030
run: yarn build
31-
- name: Build demo
32-
run: yarn demo:build
3331
- name: Upload build artifacts
3432
uses: actions/upload-artifact@v3
3533
with:

.github/workflows/publish.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ jobs:
2727
run: yarn publish --non-interactive
2828
env:
2929
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
30-
- name: Build demo pages
31-
run: yarn demo:build
3230
- name: Deploy pages
3331
uses: peaceiris/actions-gh-pages@v3
3432
with:

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ dist
104104
.tern-port
105105

106106
public
107-
demo/index.html
107+
demo/dist
108+
109+
.DS_Store

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
dist/
22
.nyc_output/
3-
.cache
3+
.cache

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ We're more than happy to help with any TypeScript, linting or test problems, or
3939

4040
### Testing your changes
4141

42-
You can test your changes using the interactive demo. Just run `yarn demo` and visit `http://localhost:1234` to parse a real-world example.
42+
You can test your changes using the interactive demo. Just run `yarn build` and `yarn demo` and visit `http://localhost:8080` to parse a real-world example.
4343

4444
## Microformats specifications
4545

demo/demo.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line import/no-unresolved
22
import { mf2 } from "../dist/index.mjs";
3+
import "./demo.css";
34

45
const setResult = (result) => {
56
const escaped = JSON.stringify(result, null, 2)

demo/index.tpl.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<title>{{ name }} demo</title>
5-
<link rel="stylesheet" href="./demo.css" />
4+
<title>{{ title }}</title>
65
<link
76
href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;700&display=swap"
87
rel="stylesheet"
98
/>
9+
{{ links }}
1010
<meta name="viewport" content="width=device-width, initial-scale=1" />
1111
<meta name="description" content="{{ description }}" />
1212
<meta name="keywords" content="{{ keywords }}" />
13+
{{ metas }}
1314
</head>
1415
<body>
1516
<nav>
@@ -99,6 +100,6 @@ <h2>Output</h2>
99100
<a href="{{ licenseUrl }}">{{ license }} Licensed</a>
100101
</div>
101102
</footer>
102-
<script src="./demo.js" type="module"></script>
103+
{{ scripts }}
103104
</body>
104105
</html>

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
],
1919
"scripts": {
2020
"prepare": "husky install",
21-
"demo:prebuild": "yarn posthtml -o demo/index.html -c posthtml.config.cjs demo/index.tpl.html",
22-
"demo": "yarn demo:prebuild && parcel serve demo/index.html",
23-
"demo:build": "yarn demo:prebuild && parcel build demo/index.html --out-dir public --public-url ./",
2421
"prebuild": "rm -rf ./dist",
2522
"build": "rollup -c",
23+
"demo": "http-server public",
2624
"lint": "eslint --cache './**/*.{ts,js}'",
2725
"prettier:list": "prettier '**/*.{ts,json,md,html}' --list-different",
2826
"prettier:fix": "prettier '**/*.{ts,json,md,html}' --write",
@@ -45,6 +43,7 @@
4543
},
4644
"devDependencies": {
4745
"@rollup/plugin-commonjs": "^25.0.4",
46+
"@rollup/plugin-html": "^1.0.2",
4847
"@rollup/plugin-node-resolve": "^15.2.1",
4948
"@rollup/plugin-terser": "^0.4.3",
5049
"@rollup/plugin-typescript": "^11.1.3",
@@ -61,16 +60,15 @@
6160
"eslint-config-prettier": "^8.1.0",
6261
"eslint-plugin-import": "^2.26.0",
6362
"glob": "^10.3.4",
63+
"http-server": "^14.1.1",
6464
"husky": ">=4",
6565
"lint-staged": ">=10",
6666
"microformat-tests": "https://github.com/microformats/tests",
6767
"mocha": "^10.0.0",
68-
"parcel-bundler": "^1.12.5",
69-
"posthtml-cli": "^0.10.0",
70-
"posthtml-expressions": "^1.9.0",
7168
"prettier": "^3.0.3",
7269
"rollup": "^3.25.0",
7370
"rollup-plugin-dts": "^6.0.2",
71+
"rollup-plugin-import-css": "^3.3.3",
7472
"source-map-support": "^0.5.19",
7573
"ts-node": "^10.8.0",
7674
"typescript": "^4.0.2"

rollup.config.js

+69
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import commonjs from "@rollup/plugin-commonjs";
33
import nodeResolve from "@rollup/plugin-node-resolve";
44
import terser from "@rollup/plugin-terser";
55
import { dts } from "rollup-plugin-dts";
6+
import html, { makeHtmlAttributes } from "@rollup/plugin-html";
7+
import { readFileSync } from "fs";
8+
import css from "rollup-plugin-import-css";
9+
10+
import pkg from "./package.json" assert { type: "json" };
11+
const { name, description, version, repository, license, keywords } = pkg;
612

713
export default [
814
{
@@ -43,4 +49,67 @@ export default [
4349
output: [{ file: "dist/index.d.ts", format: "es" }],
4450
plugins: [dts()],
4551
},
52+
{
53+
input: "./demo/demo.js",
54+
output: {
55+
dir: "./public",
56+
format: "esm",
57+
entryFileNames: "assets/bundle-[hash].js",
58+
},
59+
plugins: [
60+
nodeResolve(),
61+
commonjs({
62+
ignoreGlobal: true,
63+
}),
64+
css(),
65+
html({
66+
title: `${name} demo`,
67+
template: ({ attributes, files, meta, publicPath, title }) => {
68+
const repo = repository.replace("git+", "").replace(".git", "");
69+
const scripts = (files.js || [])
70+
.map(({ fileName }) => {
71+
const attrs = makeHtmlAttributes(attributes.script);
72+
return `<script src="${publicPath}${fileName}"${attrs}></script>`;
73+
})
74+
.join("\n");
75+
76+
const links = (files.css || [])
77+
.map(({ fileName }) => {
78+
const attrs = makeHtmlAttributes(attributes.link);
79+
return `<link href="${publicPath}${fileName}" rel="stylesheet"${attrs}>`;
80+
})
81+
.join("\n");
82+
83+
const metas = meta
84+
.map((input) => {
85+
const attrs = makeHtmlAttributes(input);
86+
return `<meta${attrs}>`;
87+
})
88+
.join("\n");
89+
90+
const replacements = {
91+
scripts,
92+
links,
93+
title,
94+
name,
95+
description,
96+
metas,
97+
version: `v${version}`,
98+
repo,
99+
releases: `${repo}/releases`,
100+
licenseUrl: `${repo}/blob/main/LICENSE`,
101+
npm: `https://npmjs.org/package/${name}`,
102+
license,
103+
keywords: keywords.join(", "),
104+
};
105+
106+
let template = readFileSync("./demo/index.tpl.html", "utf-8");
107+
Object.entries(replacements).forEach(([key, value]) => {
108+
template = template.replaceAll(`{{ ${key} }}`, value);
109+
});
110+
return template;
111+
},
112+
}),
113+
],
114+
},
46115
];

0 commit comments

Comments
 (0)