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
|
const fs = require('fs'), path = require('path'), crypto = require('crypto');
const site = process.argv[2];
if (!site) { console.error('usage: node scripts/publish.js <path-to-jekyll-site>'); process.exit(1); }
const dist = path.join(__dirname, '..', 'dist');
const dest = path.join(site, 'assets', 'cm');
const jsFiles = fs.readdirSync(dist).filter(f => f.endsWith('.js')).sort();
// version = hash of the bundle contents, so it only changes when the code does
const h = crypto.createHash('sha1');
for (const f of jsFiles) h.update(fs.readFileSync(path.join(dist, f)));
const v = h.digest('hex').slice(0, 10);
// replace old files so stale chunks don't pile up
fs.rmSync(dest, { recursive: true, force: true });
fs.mkdirSync(dest, { recursive: true });
for (const f of fs.readdirSync(dist)) fs.copyFileSync(path.join(dist, f), path.join(dest, f));
const entries = {
'@codemirror/state': 'state',
'@codemirror/view': 'view',
'@codemirror/commands': 'commands',
'@codemirror/language': 'language',
'@uiw/codemirror-theme-github': 'theme',
};
const imports = Object.entries(entries)
.map(([spec, name]) => ` "${spec}": "{{ '/assets/cm/${name}.js' | relative_url }}?v=${v}"`)
.join(',\n');
const preload = jsFiles.filter(f => f.startsWith('chunk-'))
.map(f => `<link rel="modulepreload" href="{{ '/assets/cm/${f}' | relative_url }}">`)
.join('\n');
const html = `${preload}\n<script type="importmap">\n{\n "imports": {\n${imports}\n }\n}\n</script>\n`;
fs.mkdirSync(path.join(site, '_includes'), { recursive: true });
fs.writeFileSync(path.join(site, '_includes', 'cm-importmap.html'), html);
console.log('published, version', v);
|