Refactoring to support inlined scripts

This commit is contained in:
2023-05-02 03:46:47 +02:00
parent e3a022d420
commit 4006f3954e
12 changed files with 563 additions and 231 deletions

View File

@@ -1,4 +1,2 @@
export const test = ()=>{
return `I'm "annoying" ${"in case we need to test \`string\` escaping."}. Hence this files \'tries\' to include all allowed forms of 'it'`;
}
console.log(test());
export const b = ()=>'batman';
console.log(b());

View File

@@ -0,0 +1,12 @@
<html>
<head>
</head>
<body>
<script type="module">
import {b} from "./batman.js";
document.body.appendChild(
document.createTextNode(`Inline script including ${b()}`)
);
</script>
</body>
</html>

View File

@@ -10,27 +10,23 @@ Generated by [AVA](https://avajs.dev).
[
{
code: `const test = ()=>{
return \`I'm "annoying" ${"in case we need to test \\\`string\\\` escaping."}. Hence this files \\'tries\\' to include all allowed forms of 'it'\`;␊
};␊
console.log(test());␊
code: `const b = ()=>'batman';
console.log(b());␊
export { test };␊
//# sourceMappingURL=batman-9dbe0e1d.js.map␊
export { b };␊
//# sourceMappingURL=batman-c7fa228c.js.map␊
`,
fileName: 'batman-9dbe0e1d.js',
fileName: 'batman-c7fa228c.js',
map: SourceMap {
file: 'batman-9dbe0e1d.js',
mappings: 'AAAY,MAAC,IAAI,GAAG,IAAI;AACxB,IAAI,OAAO,CAAC,eAAe,EAAE,8CAA8C,CAAC,iEAAiE,CAAC,CAAC;AAC/I,EAAC;AACD,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;;;;',
file: 'batman-c7fa228c.js',
mappings: 'AAAY,MAAC,CAAC,GAAG,IAAI,SAAS;AAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;;;;',
names: [],
sources: [
'../batman.js',
],
sourcesContent: [
`export const test = ()=>{
return \`I'm "annoying" ${"in case we need to test \\\`string\\\` escaping."}. Hence this files \\'tries\\' to include all allowed forms of 'it'\`;␊
}␊
console.log(test());␊
`export const b = ()=>'batman';
console.log(b());␊
`,
],
version: 3,
@@ -39,9 +35,9 @@ Generated by [AVA](https://avajs.dev).
},
{
code: undefined,
fileName: 'batman-9dbe0e1d.js.map',
fileName: 'batman-c7fa228c.js.map',
map: undefined,
source: '{"version":3,"file":"batman-9dbe0e1d.js","sources":["../batman.js"],"sourcesContent":["export const test = ()=>{\\n return `I\'m \\"annoying\\" ${\\"in case we need to test \\\\`string\\\\` escaping.\\"}. Hence this files \\\\\'tries\\\\\' to include all allowed forms of \'it\'`;\\n}\\nconsole.log(test());\\n"],"names":[],"mappings":"AAAY,MAAC,IAAI,GAAG,IAAI;AACxB,IAAI,OAAO,CAAC,eAAe,EAAE,8CAA8C,CAAC,iEAAiE,CAAC,CAAC;AAC/I,EAAC;AACD,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;;;;"}',
source: '{"version":3,"file":"batman-c7fa228c.js","sources":["../batman.js"],"sourcesContent":["export const b = ()=>\'batman\';\\nconsole.log(b());\\n"],"names":[],"mappings":"AAAY,MAAC,CAAC,GAAG,IAAI,SAAS;AAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;;;;"}',
},
{
code: undefined,
@@ -50,7 +46,39 @@ Generated by [AVA](https://avajs.dev).
source: `<html><head>␊
</head>␊
<body>␊
<script src="batman-9dbe0e1d.js" type="module"></script>␊
<script src="batman-c7fa228c.js" type="module"></script>␊
</body></html>`,
},
]
## inline-script
> Snapshot 1
[
{
code: undefined,
fileName: 'script.html.body.script-e3b82208.js.map',
map: undefined,
source: '{"version":3,"file":"script.html.body.script-e3b82208.js","sources":["../batman.js","../script.html.body.script.js"],"sourcesContent":["export const b = ()=>\'batman\';\\nconsole.log(b());\\n","\\n import {b} from \\"./batman.js\\";\\n document.body.appendChild(\\n document.createTextNode(`Inline script including ${b()}`)\\n );\\n "],"names":[],"mappings":"AAAO,MAAM,CAAC,GAAG,IAAI,QAAQ,CAAC;AAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;;ACCJ,QAAQ,CAAC,IAAI,CAAC,WAAW;AACrC,gBAAgB,QAAQ,CAAC,cAAc,CAAC,CAAC,wBAAwB,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AACzE,aAAa"}',
},
{
code: undefined,
fileName: 'script.html',
map: undefined,
source: `<html><head>␊
</head>␊
<body>␊
<script type="module">const b = ()=>'batman';␊
console.log(b());␊
document.body.appendChild(␊
document.createTextNode(\`Inline script including ${b()}\`)␊
);␊
//# sourceMappingURL=script.html.body.script-e3b82208.js.map␊
</script>␊
</body></html>`,

Binary file not shown.

View File

@@ -30,6 +30,19 @@ test.serial('simple', async (t) => {
t.snapshot(code);
});
test.serial('inline-script', async (t) => {
const bundle = await rollup({
input: 'script.html',
plugins: [
html({
}),
]
});
const code = await getCode(bundle, output, true);
debugPrintOutput('inline-script',code);
t.snapshot(code);
});
// TODO various parameters
// - format: cjs, iifi, ...
// - sourcemap: inline, false, (and the various exotic sourcemap options)