Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: ensure delimiter added when scripts merged across async writers #1880

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/purple-cameras-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"marko": patch
"@marko/compiler": patch
"@marko/translator-default": patch
---

Fix an issue where merging scripts (via the out.script api) was not properly inserting delimeters when scripts are added in different async writers.
8 changes: 7 additions & 1 deletion packages/marko/src/runtime/html/StringWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ StringWriter.prototype = {

merge: function (otherWriter) {
this._content += otherWriter._content;
this._scripts += otherWriter._scripts;

if (otherWriter._scripts) {
this._scripts = this._scripts
? this._scripts + ";" + otherWriter._scripts
: otherWriter._scripts;
}

if (otherWriter._data) {
if (this._data) {
for (const key in otherWriter._data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { setImmediate } from "timers";
static function tick() {
return new Promise(resolve => setImmediate(() => setImmediate(resolve)));
}

<!-- Should merge scripts in the same writer.-->
$ out.script("console.log('hello')");
$ out.script("console.log('world')");

<!-- Should merge scripts in different writers.-->
<await(tick())>
<@then>
$ out.script("console.log('again')");
</>
</await>

$ out.script("console.log('and again')");
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var expect = require("chai").expect;

module.exports = function (helpers, done) {
var template = require("./template.marko").default;

template.render({}, function (err, html) {
expect(html.toString()).to.include(
"console.log('hello');console.log('world');console.log('again');console.log('and again')"
);
done();
});
};