Skip to content

Commit 85a9219

Browse files
committed
Make code work again without ESM
1 parent 4c80922 commit 85a9219

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

code-input.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,13 @@ var codeInput = {
224224
*/
225225
templates: {
226226
// (Source code for class templates after var codeInput = ... so they can extend the codeInput.Template class)
227-
// ESM-SUPPORT-START-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
228-
/**
229-
* @deprecated Please use `new codeInput.templates.Prism(...)`
230-
*/
231-
prism(prism, plugins = []) { // Dependency: Prism.js (https://prismjs.com/)
232-
return new codeInput.templates.Prism(prism, plugins);
233-
},
234-
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
235227
/**
236228
* @deprecated Please use `new codeInput.templates.Prism(...)`
237229
*/
238230
prism(prism, plugins = []) { // Dependency: Prism.js (https://prismjs.com/)
239231
return new codeInput.templates.Prism(prism, plugins);
240232
},
241233

242-
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
243-
/**
244-
* @deprecated Please use `new codeInput.templates.Hljs(...)`
245-
*/
246-
hljs(hljs, plugins = []) { // Dependency: Highlight.js (https://highlightjs.org/)
247-
return new codeInput.templates.Hljs(hljs, plugins)
248-
},
249-
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
250234
/**
251235
* @deprecated Please use `new codeInput.templates.Hljs(...)`
252236
*/
@@ -1060,6 +1044,7 @@ var codeInput = {
10601044
{
10611045
// Templates are defined here after the codeInput variable is set, because they reference it by extending codeInput.Template.
10621046

1047+
// ESM-SUPPORT-START-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
10631048
/**
10641049
* A template that uses Prism.js syntax highlighting (https://prismjs.com/).
10651050
*/
@@ -1080,8 +1065,10 @@ var codeInput = {
10801065
);
10811066
}
10821067
};
1068+
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
10831069
codeInput.templates.Prism = Prism;
10841070

1071+
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
10851072
/**
10861073
* A template that uses highlight.js syntax highlighting (https://highlightjs.org/).
10871074
*/
@@ -1105,6 +1092,7 @@ var codeInput = {
11051092
);
11061093
}
11071094
};
1095+
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
11081096
codeInput.templates.Hljs = Hljs;
11091097
}
11101098

esm/generate.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
9292
const templateMjs = await open("templates/"+templateName+".mjs", "w")
9393
await templateMjs.writeFile(AUTOGENERATED_NOTICE);
9494
// Imports
95-
await templateMjs.writeFile("import { Template } from \"../code-input.mjs\";\n")
96-
// Template syntax is to be stored in an object; do so temporarily.
97-
await templateMjs.writeFile("const templates = {\n");
95+
await templateMjs.writeFile("import { Template } from \"../code-input.mjs\";\n");
9896
// Code after start and before end of this template, making use of the imported Template, not codeInput.Template
9997
let copyingCode = false;
10098
let templateClassName = null;
@@ -104,7 +102,7 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
104102
}
105103
if(copyingCode) {
106104
if(templateClassName === null) {
107-
const templateClassNameThisLine = line.match(/[A-Za-z]+(?=: class extends codeInput.Template)/);
105+
const templateClassNameThisLine = line.match(/(?<=class )[A-Za-z]+(?= extends codeInput.Template)/);
108106
if(templateClassNameThisLine !== null && templateClassNameThisLine.length > 0) {
109107
templateClassName = templateClassNameThisLine;
110108
}
@@ -115,9 +113,8 @@ const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outsi
115113
copyingCode = true; // After is code to copy - this line missed out
116114
}
117115
}
118-
await templateMjs.writeFile("};\n");
119116
// Export, assuming the name of the function is the same as the name of the file.
120-
await templateMjs.writeFile("export default templates."+templateClassName+";\n");
117+
await templateMjs.writeFile("export default "+templateClassName+";\n");
121118
await codeInputJs.close();
122119
await templateMjs.close();
123120
});

esm/generate.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ mkdir -p templates
4040
echo "" >> templates/$0.mjs;
4141
# Imports
4242
echo "import { Template } from \"../code-input.mjs\";" >> templates/$0.mjs
43-
# Template syntax is to be stored in an object; do so temporarily.
44-
echo "const templates = {" >> templates/$0.mjs
43+
4544
# Code after start and before end of this template, making use of the imported Template, not codeInput.Template
4645
head -$(($(sed -n "/ESM-SUPPORT-END-TEMPLATE-$0/=" ../code-input.js | head -1) - 1)) ../code-input.js | tail --line=+$(($(sed -n "/ESM-SUPPORT-START-TEMPLATE-$0/=" ../code-input.js | head -1) + 1)) | sed "s/codeInput\.Template/Template/g" >> templates/$0.mjs
47-
echo "};" >> templates/$0.mjs
4846
49-
TEMPLATE_CLASS_NAME="$(grep -Eo "[a-zA-Z]+: class extends Template" templates/$0.mjs | head -1 | sed "s/: class extends Template//")";
47+
TEMPLATE_CLASS_NAME="$(grep -Eo "class [a-zA-Z]+ extends Template" templates/$0.mjs | head -1 | sed "s/class //" | sed "s/ extends Template//")";
5048
# Export.
51-
echo "export default templates.$TEMPLATE_CLASS_NAME;" >> templates/$0.mjs
49+
echo "export default $TEMPLATE_CLASS_NAME;" >> templates/$0.mjs
5250
5351
# $0 is the template name, $1 is the autogenerated notice
5452
' "%" "$AUTOGENERATED_NOTICE"

0 commit comments

Comments
 (0)