Skip to content

Commit bf164f3

Browse files
committed
Add script to sync GitHub Pages assets from dist to docs/assets
- Created a new script `sync-pages-assets.js` that copies specified asset files from the `dist` directory to the `docs/assets` directory. - Ensures the target directory exists and throws an error if any source file is missing.
1 parent 1224f74 commit bf164f3

9 files changed

Lines changed: 47 additions & 11 deletions

File tree

demo/app.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/components/play.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const html = code => `<!DOCTYPE html>
1818
img { width: 100%; }
1919
</style>
2020
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
21-
<script src="dist/apprun-dev-tools.js"></script>
22-
<script src="dist/apprun-html.js"></script>
21+
<script src="docs/assets/apprun-dev-tools.js"></script>
22+
<script src="docs/assets/apprun-html.js"></script>
2323
</head>
2424
<body>
2525
<script>
@@ -44,6 +44,7 @@ const editor = (e) => {
4444
const editor = document.createElement('apprun-code');
4545
editor.style.height = '80vh';
4646
editor.setAttribute('code-width', '60%');
47+
editor.setAttribute('apprun-html-src', 'docs/assets/apprun-html.js');
4748
e.appendChild(editor)
4849
};
4950

@@ -94,4 +95,4 @@ export class PlayComponent extends Component {
9495
}
9596
}
9697

97-
export default (element) => new PlayComponent().mount(element);
98+
export default (element) => new PlayComponent().mount(element);

docs/assets/apprun-dev-tools.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/apprun-dev-tools.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/apprun-html.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/apprun-html.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
},
5959
"scripts": {
6060
"build": "tsc -p src && rollup -c && webpack --mode production",
61+
"postbuild": "node scripts/sync-pages-assets.js",
6162
"prepack": "npm run build",
6263
"lint": "eslint .",
6364
"test": "jest",

scripts/sync-pages-assets.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const root = path.resolve(__dirname, '..');
5+
const assetsDir = path.join(root, 'docs', 'assets');
6+
const assets = [
7+
'apprun-html.js',
8+
'apprun-html.js.map',
9+
'apprun-dev-tools.js',
10+
'apprun-dev-tools.js.map',
11+
];
12+
13+
fs.mkdirSync(assetsDir, { recursive: true });
14+
15+
for (const asset of assets) {
16+
const source = path.join(root, 'dist', asset);
17+
const target = path.join(assetsDir, asset);
18+
19+
if (!fs.existsSync(source)) {
20+
throw new Error(`Cannot sync GitHub Pages asset. Missing ${source}`);
21+
}
22+
23+
fs.copyFileSync(source, target);
24+
}

src/apprun-code.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const encodeHTML = code => {
3939
.replace(/'/g, '&#039;');
4040
}
4141

42-
const code_html = code => `<!DOCTYPE html>
42+
const encodeAttribute = value => `${value}`.replace(/&/g, '&amp;').replace(/"/g, '&quot;');
43+
44+
const code_html = (code, apprun_html_src = 'dist/apprun-html.js') => `<!DOCTYPE html>
4345
<html lang="en">
4446
<head>
4547
<meta charset="UTF-8">
@@ -53,7 +55,7 @@ const code_html = code => `<!DOCTYPE html>
5355
}
5456
</style>
5557
<script src="https://cdn.jsdelivr.net/npm/typescript@latest"></script>
56-
<script src="dist/apprun-html.js"></script>
58+
<script src="${encodeAttribute(apprun_html_src)}"></script>
5759
</head>
5860
<body>
5961
<pre id="code" style="display:none">${encodeHTML(code)}</pre>
@@ -123,6 +125,7 @@ class Play extends Component {
123125
const code_id = props['code-id'];
124126
const hide_code = props['hide-code'];
125127
const code_width = props['code-width'];
128+
const apprun_html_src = props['apprun-html-src'];
126129

127130
let code_area;
128131
if (code_id) {
@@ -138,11 +141,11 @@ class Play extends Component {
138141

139142
if (code_area) code_area.style.display = 'none';
140143

141-
return { code, hide_code, code_width };
144+
return { code, hide_code, code_width, apprun_html_src };
142145
}
143146

144147

145-
rendered = ({ code, hide_code, code_width }) => {
148+
rendered = ({ code, hide_code, code_width, apprun_html_src }) => {
146149
const element = this.element as HTMLElement;
147150

148151
const textarea = element.querySelector(".apprun-play .editor") as any;
@@ -159,7 +162,7 @@ class Play extends Component {
159162
if (code.indexOf('<html') >= 0)
160163
doc.write(code);
161164
else
162-
doc.write(code_html(code));
165+
doc.write(code_html(code, apprun_html_src));
163166
doc.close();
164167
}
165168

0 commit comments

Comments
 (0)