-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathssr.cjs
More file actions
51 lines (45 loc) · 2.02 KB
/
ssr.cjs
File metadata and controls
51 lines (45 loc) · 2.02 KB
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
const { readFileSync, writeFileSync } = require('fs');
const { join } = require('path');
const fixExports = require('./exports.cjs');
const init = join(__dirname, '..', 'esm', 'init-ssr.js');
const uhtml = readFileSync(init).toString();
const content = [
'const document = content ? new DOMParser().parseFromString(content, ...rest) : new Document;',
'const { constructor: DocumentFragment } = document.createDocumentFragment();',
'document[__chunks__] = true;',
];
writeFileSync(init + '_', `
// ⚠️ WARNING - THIS FILE IS AN ARTIFACT - DO NOT EDIT
import Document from './dom/document.js';
import DOMParser from './dom/dom-parser.js';
import { __chunks__ } from './dom/symbols.js';
import { value } from './dom/symbols.js';
import Comment from './dom/comment.js';
Comment.prototype.toString = function toString() {
const content = this[value];
switch (content) {
case '<>':
case '</>':
return '';
default:
return /^\\[\\d+\\]$/.test(content) ? '' : \`<!--\${content}-->\`;
}
};
/**
* @param {Document} document
* @returns {import("./keyed.js")}
*/
export default (content, ...rest) => ${
// tested via integration
fixExports(
uhtml
.replace(/const create(HTML|SVG) = create\(parse\((false|true), false\)\)/g, 'const create$1 = create(parse($2, true))')
.replace(`svg || ('ownerSVGElement' in element)`, `/* c8 ignore start */ svg || ('ownerSVGElement' in element) /* c8 ignore stop */`)
.replace(/diffFragment = \(([\S\s]+?)return /, 'diffFragment = /* c8 ignore start */($1/* c8 ignore stop */return ')
.replace(/udomdiff = \(([\S\s]+?)return /, 'udomdiff = /* c8 ignore start */($1/* c8 ignore stop */return ')
.replace(/^(\s+)replaceWith\(([^}]+?)\}/m, '$1/* c8 ignore start */\n$1replaceWith($2}\n$1/* c8 ignore stop */')
.replace(/^(\s+)(["'])use strict\2;/m, (_, tab, quote) => `${tab}${quote}use strict${quote};\n\n${tab}${content.join(`\n${tab}`)}`)
.replace(/^(\s+)(return exports;)/m, '$1exports.document = document;\n$1$2')
.replace(/^[^(]+/, '')
)
}`);