Skip to content

Commit

Permalink
(fix) deal with multiple script tags (#193)
Browse files Browse the repository at this point in the history
All script tags, no matter at what level, are listed within the root children.
To get the top level scripts, filter out all those that are part of children's children.
Those have another type ('Element' with name 'script').

Fixes #143
  • Loading branch information
dummdidumm committed Jun 15, 2020
1 parent 705469f commit da2f54d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
55 changes: 41 additions & 14 deletions packages/svelte2tsx/src/svelte2tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,43 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
}
};

let scriptTag: Node = null;
let moduleScriptTag: Node = null;
const handleScriptTag = (node: Node) => {
if (
node.attributes &&
node.attributes.find(
(a) => a.name == 'context' && a.value.length == 1 && a.value[0].raw == 'module',
)
) {
moduleScriptTag = node;
} else {
scriptTag = node;
// All script tags, no matter at what level, are listed within the root children.
// To get the top level scripts, filter out all those that are part of children's children.
// Those have another type ('Element' with name 'script').
const scriptTags = (<Node[]>htmlxAst.children).filter((child) => child.type === 'Script');
let topLevelScripts = scriptTags;
const handleScriptTag = (node: Node, parent: Node) => {
if (parent !== htmlxAst && node.name === 'script') {
topLevelScripts = topLevelScripts.filter(
(tag) => tag.start !== node.start || tag.end !== node.end,
);
}
};
const getTopLevelScriptTags = () => {
let scriptTag: Node = null;
let moduleScriptTag: Node = null;
// should be 2 at most, one each, so using forEach is safe
topLevelScripts.forEach((tag) => {
if (
tag.attributes &&
tag.attributes.find(
(a) => a.name == 'context' && a.value.length == 1 && a.value[0].raw == 'module',
)
) {
moduleScriptTag = tag;
} else {
scriptTag = tag;
}
});
return { scriptTag, moduleScriptTag };
};
const blankOtherScriptTags = () => {
scriptTags
.filter((tag) => !topLevelScripts.includes(tag))
.forEach((tag) => {
str.remove(tag.start, tag.end);
});
};

const slots = new Map<string, Map<string, string>>();
const handleSlot = (node: Node) => {
Expand Down Expand Up @@ -245,8 +268,8 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
case 'Style':
handleStyleTag(node);
break;
case 'Script':
handleScriptTag(node);
case 'Element':
handleScriptTag(node, parent);
break;
case 'BlockStatement':
enterBlockStatement();
Expand Down Expand Up @@ -290,6 +313,10 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {

convertHtmlxToJsx(str, htmlxAst, onHtmlxWalk, onHtmlxLeave);

// resolve scripts
const { scriptTag, moduleScriptTag } = getTopLevelScriptTags();
blankOtherScriptTags();

//resolve stores
pendingStoreResolutions.map(resolveStore);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<></>;function render() {

let b = 'top level';
;
<><div>

</div>



<sveltehead>
<link rel="stylesheet" href="/lib/jodit.es2018.min.css" />

</sveltehead></>
return { props: {}, slots: {} }}

export default class {
$$prop_def = __sveltets_partial(render().props)
$$slot_def = render().slots
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div>
<script>let a = 'not top level';</script>
</div>

<script>
let b = 'top level';
</script>

<svelte:head>
<link rel="stylesheet" href="/lib/jodit.es2018.min.css" />
<script src="/lib/jodit.es2018.min.js">
</script>
</svelte:head>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<></>;import Test from './Test.svelte';
function render() {



let a = 'b';
;
<><Test b="6" />
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Test b="6" />
<script>
import Test from './Test.svelte';
import Test from './Test.svelte';
let a = 'b';
</script>

0 comments on commit da2f54d

Please sign in to comment.