Skip to content

Commit fb11431

Browse files
committed
refactor
1 parent af1401b commit fb11431

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed
Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as fs from "node:fs/promises";
21
import * as path from "node:path";
32

4-
import { rolldown } from "rolldown";
3+
import * as rolldown from "rolldown";
54

65
import type { ResolvedUserscriptConfig } from "~/config/schema";
76

@@ -17,35 +16,29 @@ async function buildUserscript(
1716

1817
const header = serializeMetaHeader(config.header);
1918

20-
const bundle = await rolldown({ input: config.entryPoint, tsconfig: true });
21-
const result = await bundle.generate({
22-
format: "iife",
23-
sourcemap: false,
24-
minify: "dce-only",
19+
const outDir = config.outDir;
20+
const outFile = path.join(outDir, USERSCRIPT_OUTPUT_FILE_NAME);
21+
22+
const result = await rolldown.build({
23+
input: config.entryPoint,
24+
tsconfig: true,
25+
plugins: [config.plugins],
26+
output: {
27+
format: "iife",
28+
sourcemap: false,
29+
minify: "dce-only",
30+
postBanner: `${header}\n`,
31+
cleanDir: config.clean,
32+
file: outFile,
33+
},
34+
write: options?.write,
2535
});
2636

2737
if (result.output.length !== 1) {
2838
throw new Error(`❌ Unexpected userscript build output`);
2939
}
3040

31-
const bundledCode = result.output[0].code;
32-
const fullCode = `${header}\n\n${bundledCode}`;
33-
34-
if (!options?.write) {
35-
return fullCode;
36-
}
37-
38-
const outDir = config.outDir;
39-
if (config.clean) {
40-
console.log("\n🧹 Cleaning output directory");
41-
await fs.rm(outDir, { recursive: true, force: true });
42-
}
43-
await fs.mkdir(outDir, { recursive: true });
44-
const outFile = path.join(outDir, USERSCRIPT_OUTPUT_FILE_NAME);
45-
await fs.writeFile(outFile, fullCode, "utf-8");
46-
console.log("\n🎉 Build process complete!");
47-
48-
return fullCode;
41+
return result.output[0].code;
4942
}
5043

5144
export { buildUserscript };

packages/usts/src/core/build/meta-header.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import type { UserscriptMetaHeaderConfig } from "~/config/schema";
22

3-
const headerStart = "// ==UserScript==" as const;
4-
const headerEnd = "// ==/UserScript==" as const;
5-
63
function getHeaderLine(
74
key: string,
85
val: string | boolean | readonly [string, string],
@@ -34,6 +31,9 @@ export function serializeMetaHeader(
3431
const headerLines = headerConfigEntries.flatMap(([kwy, val]) =>
3532
getHeaderLines(kwy, val),
3633
);
34+
35+
const headerStart = "// ==UserScript==";
36+
const headerEnd = "// ==/UserScript==";
3737
const serializedHeaderLines = headerLines.join("\n");
3838
const serializedHeader =
3939
`${headerStart}\n${serializedHeaderLines}\n${headerEnd}` as const;

packages/usts/tests/__snapshots__/basic.test.ts.snap

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Bun Snapshot v1, https://bun.sh/docs/test/snapshots
22

3-
exports[`Basic userscript Correctly bundles a basic userscript 1`] = `
3+
exports[`Basic userscript correctly bundles a basic userscript 1`] = `
44
"// ==UserScript==
55
// @name Userscript name
66
// @namespace fixtures
@@ -14,3 +14,19 @@ exports[`Basic userscript Correctly bundles a basic userscript 1`] = `
1414
})();
1515
"
1616
`;
17+
18+
exports[`Basic userscript correctly bundles a basic userscript with more options 1`] = `
19+
"// ==UserScript==
20+
// @name Userscript name
21+
// @namespace fixtures
22+
// @match https://example.com/*
23+
// @version 1.0.0
24+
// @description Userscript description
25+
// @resource myStyle https://example.com/myStyle.css
26+
// ==/UserScript==
27+
28+
(function() {
29+
console.log("Hello world!");
30+
})();
31+
"
32+
`;

packages/usts/tests/basic.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ import { buildUserscript } from "../src/core/build/build";
44
import { resolveFixture } from "./helpers/resolve";
55

66
describe("Basic userscript", () => {
7-
it("Correctly bundles a basic userscript", async () => {
7+
it("correctly bundles a basic userscript", async () => {
88
const fixture = resolveFixture("fixtures/basic/index.ts");
99
const result = await buildUserscript(fixture);
1010
expect(result).toMatchSnapshot();
1111
});
12+
13+
it("correctly bundles a basic userscript with more options", async () => {
14+
const fixture = resolveFixture("fixtures/basic/index.ts", {
15+
resource: {
16+
myStyle: "https://example.com/myStyle.css",
17+
},
18+
});
19+
const result = await buildUserscript(fixture);
20+
expect(result).toMatchSnapshot();
21+
});
1222
});

0 commit comments

Comments
 (0)