Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: keep injected JS wrapper valid when the file ends in a line comment

Luís Miguel91e2f0ef9f89
diff --git a/bin/utils/combine.ts b/bin/utils/combine.ts
index 835a8d37..e2924f2e 100644
--- a/bin/utils/combine.ts
+++ b/bin/utils/combine.ts
@@ -14,10 +14,14 @@ export default async function combineFiles(files: string[], output: string) {
}
const fileContent = await fs.readFile(file);
+ // Keep the closing `});` on its own line. If the injected file ends in a
+ // line comment without a trailing newline, appending ` });` on the same
+ // line would comment it out and break the wrapper (mirrors the .css
+ // branch above, which already closes on a separate line).
return (
- "window.addEventListener('DOMContentLoaded', (_event) => { " +
+ "window.addEventListener('DOMContentLoaded', (_event) => {\n" +
fileContent +
- ' });'
+ '\n});'
);
}),
);
diff --git a/dist/cli.js b/dist/cli.js
index 3d2d4297..06cae53b 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -435,9 +435,13 @@ async function combineFiles(files, output) {
});`;
}
const fileContent = await fs$1.readFile(file);
- return ("window.addEventListener('DOMContentLoaded', (_event) => { " +
+ // Keep the closing `});` on its own line. If the injected file ends in a
+ // line comment without a trailing newline, appending ` });` on the same
+ // line would comment it out and break the wrapper (mirrors the .css
+ // branch above, which already closes on a separate line).
+ return ("window.addEventListener('DOMContentLoaded', (_event) => {\n" +
fileContent +
- ' });');
+ '\n});');
}));
await fs$1.writeFile(output, contents.join('\n'));
return files;
diff --git a/tests/unit/combine.test.ts b/tests/unit/combine.test.ts
new file mode 100644
index 00000000..dea21473
--- /dev/null
+++ b/tests/unit/combine.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it, beforeAll, afterAll } from 'vitest';
+import fs from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import combineFiles from '../../bin/utils/combine.js';
+
+describe('combineFiles', () => {
+ let dir: string;
+
+ beforeAll(async () => {
+ dir = await fs.mkdtemp(path.join(os.tmpdir(), 'pake-combine-'));
+ });
+
+ afterAll(async () => {
+ await fs.rm(dir, { recursive: true, force: true });
+ });
+
+ it('keeps the wrapper valid when a .js file ends in a line comment without a trailing newline', async () => {
+ const jsFile = path.join(dir, 'inject.js');
+ const out = path.join(dir, 'out-js.js');
+ // No trailing newline after the line comment: the closing `});` must not be
+ // absorbed into the comment.
+ await fs.writeFile(
+ jsFile,
+ "console.log('pake');\n//# sourceMappingURL=inject.js.map",
+ );
+
+ await combineFiles([jsFile], out);
+ const result = await fs.readFile(out, 'utf-8');
+
+ // Compiles only if the arrow function and its call are both closed.
+ expect(() => new Function(result)).not.toThrow();
+ expect(result).toContain("addEventListener('DOMContentLoaded'");
+ });
+
+ it('wraps .css files as a style injection with the content JSON-encoded', async () => {
+ const cssFile = path.join(dir, 'inject.css');
+ const out = path.join(dir, 'out-css.js');
+ await fs.writeFile(cssFile, 'body { color: red; }');
+
+ await combineFiles([cssFile], out);
+ const result = await fs.readFile(out, 'utf-8');
+
+ expect(() => new Function(result)).not.toThrow();
+ expect(result).toContain('document.head.appendChild');
+ expect(result).toContain(JSON.stringify('body { color: red; }'));
+ });
+
+ it('returns the input file list', async () => {
+ const jsFile = path.join(dir, 'ret.js');
+ const out = path.join(dir, 'out-ret.js');
+ await fs.writeFile(jsFile, 'void 0;');
+
+ const returned = await combineFiles([jsFile], out);
+
+ expect(returned).toEqual([jsFile]);
+ });
+});