syndicate-browser-compiler.js
This commit is contained in:
parent
f0026d9688
commit
6ded2c2050
|
@ -12,7 +12,7 @@
|
||||||
"url": "https://git.syndicate-lang.org/syndicate-lang/syndicate-js"
|
"url": "https://git.syndicate-lang.org/syndicate-lang/syndicate-js"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "yarn compile && yarn rollup",
|
"prepare": "yarn compile && yarn rollup && cp syndicate-browser-compiler.js dist/",
|
||||||
"compile": "../../node_modules/.bin/tsc",
|
"compile": "../../node_modules/.bin/tsc",
|
||||||
"compile:watch": "../../node_modules/.bin/tsc -w",
|
"compile:watch": "../../node_modules/.bin/tsc -w",
|
||||||
"rollup": "../../node_modules/.bin/rollup -c",
|
"rollup": "../../node_modules/.bin/rollup -c",
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
/// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
||||||
|
|
||||||
|
const __SYNDICATE__ = Syndicate;
|
||||||
|
|
||||||
|
(() => {
|
||||||
|
async function translateScripts() {
|
||||||
|
|
||||||
|
const syndicateScripts =
|
||||||
|
Array.from(document.getElementsByTagName('script'))
|
||||||
|
.filter(s => s.type === 'text/javascript+syndicate');
|
||||||
|
|
||||||
|
for (const script of syndicateScripts) {
|
||||||
|
const sourceUrl = script.src || script.getAttribute('data-src') || false;
|
||||||
|
const sourceCode = sourceUrl ? await (await fetch(sourceUrl)).text() : script.innerHTML;
|
||||||
|
|
||||||
|
const compilationResult = SyndicateCompiler.compile({
|
||||||
|
name: sourceUrl || 'anonymous-script-tag',
|
||||||
|
source: sourceCode,
|
||||||
|
module: 'none',
|
||||||
|
runtime: 'Syndicate',
|
||||||
|
emitError: console.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
const sourceMap = { ... compilationResult.map };
|
||||||
|
sourceMap.sourcesContent = [sourceCode];
|
||||||
|
const formattedSourceMap = '\n//# sourceMappingURL=data:application/json;base64,' +
|
||||||
|
Syndicate.Bytes.from(JSON.stringify(sourceMap)).toBase64();
|
||||||
|
const finalOutput = compilationResult.text + formattedSourceMap;
|
||||||
|
|
||||||
|
const replacement = document.createElement('script');
|
||||||
|
replacement.text = finalOutput;
|
||||||
|
script.parentNode.replaceChild(replacement, script);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('DOMContentLoaded', translateScripts);
|
||||||
|
})();
|
Loading…
Reference in New Issue