Skip to content

Commit 511c955

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

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ module.exports = function() {
110110
}
111111
if(data.html) {
112112
data.html = data.html.trim();
113+
if (data.js) {
114+
data.html += "\n\n<script type=\"module\">\n" + data.js + "\n</script>";
115+
data.js = "";
116+
}
113117
}
114118
if(data) {
115119
if(window.CREATE_CODE_PEN) {
@@ -123,12 +127,15 @@ module.exports = function() {
123127
}
124128
}
125129
if(el && matches.call(el, ".demo_wrapper")) {
126-
var htmlCode = el.querySelector("[data-for=html] code");
127-
var htmlText = htmlCode ? htmlCode.textContent.trim() : "";
128-
129130
var jsCode = el.querySelector("[data-for=js] code");
130131
var jsText = jsCode ? jsCode.textContent.trim() : "";
131132

133+
var htmlCode = el.querySelector("[data-for=html] code");
134+
var htmlText = htmlCode ? htmlCode.textContent.trim() : "";
135+
if (jsText) {
136+
htmlText += "\n\n<script type=\"module\">\n" + jsText + "\n</script>";
137+
}
138+
132139
var cssText = getStylesFromIframe( el.querySelector("iframe") );
133140

134141
var codePen = {

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var open = function(url, callback, done) {
2222
};
2323

2424
describe("bit-docs-html-codepen-link", function() {
25-
it.only("basics works", function(done) {
25+
it("basics works", function(done) {
2626
this.timeout(60000);
2727

2828
var docMap = Promise.resolve({
@@ -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)