Skip to content

Commit 1b84c1d

Browse files
author
Chasen Le Hara
committed
Fix some issues with <script type="module">
- Only move the JS if `import` is in the JS - Fix issue with moving the JS when there was no HTML
1 parent 998af69 commit 1b84c1d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ module.exports = function() {
118118
}
119119
if(data.html) {
120120
data.html = data.html.trim();
121-
if (data.js) {
122-
data.html += "\n\n<script type=\"module\">\n" + data.js + "\n</script>";
123-
data.js = "";
124-
}
121+
}
122+
if (data.js && data.js.indexOf('import') > -1) {
123+
var jsAsModule = "<script type=\"module\">\n" + data.js + "\n</script>";
124+
data.html = data.html ? data.html + "\n\n" + jsAsModule : jsAsModule;
125+
data.js = "";
125126
}
126127
if(data) {
127128
cleanCodePenData(data);

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ describe("bit-docs-html-codepen-link", function() {
6767
css: 'my-app {color: "green";}'
6868
},
6969
{
70-
js: 'import {DefineMap} from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nconsole.log( myCounter.count ) //-> 1',
70+
html: '<script type="module">\nimport {DefineMap} from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nconsole.log( myCounter.count ) //-> 1\n</script>',
71+
js: '',
7172
js_module: true,
7273
editors: '0011'
7374
}

0 commit comments

Comments
 (0)