Skip to content

Commit bc175ad

Browse files
author
Chasen Le Hara
committed
Put the JS into the HTML as <script type="module">
Related to canjs/canjs#4763
1 parent f92e92c commit bc175ad

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var languageHTML = /language-(\w+)/;
55
function cleanCodePenData(data) {
66
if(docObject.codepen) {
77
docObject.codepen.forEach(function(replacement){
8+
if(data.html) {
9+
data.html = data.html.split(replacement[0]).join(replacement[1]);
10+
}
811
if(data.js) {
912
data.js = data.js.split(replacement[0]).join(replacement[1]);
1013
}
@@ -115,6 +118,10 @@ module.exports = function() {
115118
}
116119
if(data.html) {
117120
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+
}
118125
}
119126
if(data) {
120127
cleanCodePenData(data);
@@ -134,6 +141,10 @@ module.exports = function() {
134141

135142
var jsCode = el.querySelector("[data-for=js] code");
136143
var jsText = jsCode ? jsCode.textContent.trim() : "";
144+
if (jsText) {
145+
htmlText += "\n<script type=\"module\">\n" + jsText + "\n</script>";
146+
jsText = "";
147+
}
137148

138149
var cssText = getStylesFromIframe( el.querySelector("iframe") );
139150

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ describe("bit-docs-html-codepen-link", function() {
6060
codePen.click();
6161
});
6262
assert.deepEqual(createCallData, [{
63-
html: '<my-app></my-app>',
64-
js: 'import { Component } from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nComponent',
63+
html: '<my-app></my-app>\n\n<script type="module">\nimport { Component } from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nComponent\n</script>',
64+
js: '',
6565
js_module: true,
6666
editors: '1011',
6767
css: 'my-app {color: "green";}'

0 commit comments

Comments
 (0)