-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
106 lines (84 loc) · 3.21 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { readFileSync, writeFileSync } from 'fs';
import { dirname } from 'path';
import mkpath from 'mkpath';
import JSZip from 'jszip';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
import { marked } from 'marked';
import { transformSync } from '@babel/core';
import { minify } from 'terser'
function readFile(dst) {
return readFileSync(dst, 'utf8');
}
function writeFile(dst, content) {
mkpath(dirname(dst));
return writeFileSync(dst, content, 'utf8');
}
function replaceAll(str, obj) {
for (let key in obj) {
str = str.split(key).join(obj[key]);
}
return str;
}
function getBookmarklet() {
const script = readFile('dist/bookmarklet.min.js');
return `javascript:(function(){${encodeURIComponent(script)}}())`;
}
function getPackageConfig() {
return JSON.parse(readFile('./package.json'));
}
function prepareJSVariable(str) {
str = str.replace(/\s+/g, ' ');
str = str.replace(/'/g, '\\\'');
return str;
}
async function generateReadme() {
const markdown = readFile('src/readme.md');
const updatedMarkdown = replaceAll(markdown, { '{{bookmarklet}}': getBookmarklet() });
writeFile('./readme.md', updatedMarkdown);
const html = readFile('src/readme.html');
const updatedHtml = replaceAll(html, { '{{content}}': marked(updatedMarkdown) });
writeFile('./docs/index.html', updatedHtml);
}
async function buildChromeExtension() {
const zip = new JSZip();
const config = getPackageConfig();
let manifest = readFile('src/chrome/manifest.json');
manifest = replaceAll(manifest, {
'{{name}}': 'Accessibility Heading Outliner',
'{{description}}': 'See the heading outline like a screenreader.',
'{{homepage}}': 'https://hinderlingvolkart.github.io/h123/',
'{{version}}': config.version
});
const backgroundJS = readFile('src/chrome/background.js');
const bookmarkletJS = readFile('dist/bookmarklet.js');
zip.file('manifest.json', manifest);
zip.file('background.js', backgroundJS);
zip.file('bookmarklet.js', bookmarkletJS);
[16, 48, 128].forEach(size => {
const icon = readFileSync(`./src/chrome/icon-${size}.png`);
zip.file(`icon-${size}.png`, icon, { base64: false, binary: true });
});
const content = await zip.generateAsync({ type: 'nodebuffer' });
writeFileSync('./dist/AccessibilityOutlinerChrome.zip', content);
}
async function compile() {
const html = readFile('src/bookmarklet.ui.html');
const css = readFile('src/bookmarklet.ui.css');
const cssResult = await postcss([autoprefixer]).process(css, { from: undefined });
const updatedHtml = html.replace('{{css}}', cssResult.css);
let bookmarkletJS = readFile('src/bookmarklet.js');
bookmarkletJS = bookmarkletJS.replace('{{ui}}', prepareJSVariable(updatedHtml));
const transformedJS = transformSync(bookmarkletJS, { presets: ['@babel/preset-env'] }).code;
writeFile('./dist/bookmarklet.js', transformedJS);
const minifiedJS = (await minify(transformedJS)).code;
writeFile('./dist/bookmarklet.min.js', minifiedJS);
}
async function build() {
await compile();
await buildChromeExtension();
await generateReadme();
}
(async () => {
await build();
})();