ES6 modules in the browser
This commit is contained in:
parent
5ffc647997
commit
c6eef34736
|
@ -8,17 +8,21 @@ const __SYNDICATE__ = Syndicate;
|
||||||
|
|
||||||
const syndicateScripts =
|
const syndicateScripts =
|
||||||
Array.from(document.getElementsByTagName('script'))
|
Array.from(document.getElementsByTagName('script'))
|
||||||
.filter(s => s.type === 'text/javascript+syndicate');
|
.filter(s => (s.type === 'text/javascript+syndicate' ||
|
||||||
|
s.type === 'syndicate' ||
|
||||||
|
s.type === 'module+syndicate'));
|
||||||
|
|
||||||
for (const script of syndicateScripts) {
|
for (const script of syndicateScripts) {
|
||||||
|
const isModule = script.type === 'module+syndicate';
|
||||||
|
|
||||||
const sourceUrl = script.src || script.getAttribute('data-src') || false;
|
const sourceUrl = script.src || script.getAttribute('data-src') || false;
|
||||||
const sourceCode = sourceUrl ? await (await fetch(sourceUrl)).text() : script.innerHTML;
|
const sourceCode = sourceUrl ? await (await fetch(sourceUrl)).text() : script.innerHTML;
|
||||||
|
|
||||||
const compilationResult = SyndicateCompiler.compile({
|
const compilationResult = SyndicateCompiler.compile({
|
||||||
name: sourceUrl || 'anonymous-script-tag',
|
name: sourceUrl || 'anonymous-script-tag',
|
||||||
source: sourceCode,
|
source: sourceCode,
|
||||||
module: 'none',
|
module: isModule ? 'es6' : 'none',
|
||||||
runtime: 'Syndicate',
|
runtime: isModule ? void 0 : 'Syndicate',
|
||||||
emitError: console.error,
|
emitError: console.error,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,8 +32,11 @@ const __SYNDICATE__ = Syndicate;
|
||||||
Syndicate.Bytes.from(JSON.stringify(sourceMap)).toBase64();
|
Syndicate.Bytes.from(JSON.stringify(sourceMap)).toBase64();
|
||||||
const finalOutput = compilationResult.text + formattedSourceMap;
|
const finalOutput = compilationResult.text + formattedSourceMap;
|
||||||
|
|
||||||
|
// console.log(finalOutput);
|
||||||
|
|
||||||
const replacement = document.createElement('script');
|
const replacement = document.createElement('script');
|
||||||
replacement.text = finalOutput;
|
replacement.text = finalOutput;
|
||||||
|
if (isModule) replacement.type = 'module';
|
||||||
script.parentNode.replaceChild(replacement, script);
|
script.parentNode.replaceChild(replacement, script);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue