@unbound-app/rollup-plugin-hermes 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -28,10 +28,10 @@ export type BundleEntry = {
28
28
  export type HermesPlugin = {
29
29
  name: string;
30
30
  generateBundle(this: HermesContext, options: OutputOptions, bundle: Record<string, BundleEntry>, isWrite: boolean): Promise<void>;
31
- buildEnd(this: HermesContext): Promise<void>;
32
31
  };
33
32
  /**
34
33
  * @description Compiles the bundle's output file to Hermes bytecode, once per held hermesc version, as `<name>.<version>.bundle` beside it.
34
+ * Each build uses its own temporary folder, so builds running side by side never delete each other's files.
35
35
  * @param options A fixed hermesc directory, or which of npm's bytecode versions to hold; plus extra hermesc flags.
36
36
  * @returns The rollup plugin, which rolldown also accepts.
37
37
  */
package/dist/index.mjs CHANGED
@@ -1,14 +1,13 @@
1
1
  import { hermescBinary, resolveHermesTargets } from "./manifest.mjs";
2
- import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
+ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
3
3
  import { spawn } from "node:child_process";
4
4
  import { randomBytes } from "node:crypto";
5
5
  import { existsSync } from "node:fs";
6
6
  import { tmpdir } from "node:os";
7
- import { join } from "node:path";
7
+ import { dirname, join } from "node:path";
8
8
  // src/index.ts
9
- const temp = join(tmpdir(), "hermesc");
10
- function tempFile(extension) {
11
- return join(temp, `${randomBytes(8).readUint32LE(0)}${extension}`);
9
+ function tempFile(directory, extension) {
10
+ return join(directory, `${randomBytes(8).readUint32LE(0)}${extension}`);
12
11
  }
13
12
  async function compile(ctx, dir, js, flags) {
14
13
  const binary = hermescBinary(dir);
@@ -16,7 +15,7 @@ async function compile(ctx, dir, js, flags) {
16
15
  ctx.warn(`hermesc is unsupported on this platform or missing at ${binary}, skipping it.`);
17
16
  return;
18
17
  }
19
- const bytecode = tempFile(".bundle");
18
+ const bytecode = tempFile(dirname(js), ".bundle");
20
19
  const args = [
21
20
  "--emit-binary",
22
21
  "--out",
@@ -58,8 +57,40 @@ async function compile(ctx, dir, js, flags) {
58
57
  map
59
58
  };
60
59
  }
60
+ async function compileTargets(ctx, targets, flags, { directory, file, name, outputFile, code }) {
61
+ const js = tempFile(directory, ".js");
62
+ await writeFile(js, code, "utf-8");
63
+ for (const target of targets) {
64
+ if (!target.dir) {
65
+ ctx.warn(`hermesc for bytecode ${target.version} could not be downloaded, skipping it.`);
66
+ continue;
67
+ }
68
+ const fileName = target.version === null ? `${name}.bundle` : `${name}.${target.version}.bundle`;
69
+ ctx.info(`${outputFile} -> ${outputFile.replace(file, fileName)}...`);
70
+ const compiled = await compile(ctx, target.dir, js, flags);
71
+ if (!compiled) continue;
72
+ ctx.emitFile({
73
+ type: "asset",
74
+ fileName,
75
+ source: compiled.bytecode
76
+ });
77
+ if (compiled.map) {
78
+ const map = {
79
+ ...JSON.parse(compiled.map),
80
+ sources: [file]
81
+ };
82
+ ctx.emitFile({
83
+ type: "asset",
84
+ fileName: `${fileName}.map`,
85
+ source: JSON.stringify(map)
86
+ });
87
+ }
88
+ ctx.info(`( ͡° ͜ʖ ͡°) Bytecode compiled to ${fileName}`);
89
+ }
90
+ }
61
91
  /**
62
92
  * @description Compiles the bundle's output file to Hermes bytecode, once per held hermesc version, as `<name>.<version>.bundle` beside it.
93
+ * Each build uses its own temporary folder, so builds running side by side never delete each other's files.
63
94
  * @param options A fixed hermesc directory, or which of npm's bytecode versions to hold; plus extra hermesc flags.
64
95
  * @returns The rollup plugin, which rolldown also accepts.
65
96
  */
@@ -68,52 +99,31 @@ function hermes(options = {}) {
68
99
  return {
69
100
  name: "hermesc",
70
101
  async generateBundle(output, bundle) {
71
- const file = output.file?.split("/").pop();
72
- if (!output.file || !file) return;
102
+ const outputFile = output.file;
103
+ const file = outputFile?.split("/").pop();
104
+ if (!outputFile || !file) return;
73
105
  const chunk = bundle[file];
74
106
  if (typeof chunk?.code !== "string") return;
75
- const name = file.split(".")[0];
107
+ const name = file.split(".")[0] ?? file;
76
108
  targets ??= options.hermesc ? [{
77
109
  version: null,
78
110
  dir: options.hermesc
79
111
  }] : await resolveHermesTargets(this, options);
80
- if (!existsSync(temp)) await mkdir(temp);
81
- const js = tempFile(".js");
82
- await writeFile(js, chunk.code, "utf-8");
83
- for (const target of targets) {
84
- if (!target.dir) {
85
- this.warn(`hermesc for bytecode ${target.version} could not be downloaded, skipping it.`);
86
- continue;
87
- }
88
- const fileName = target.version === null ? `${name}.bundle` : `${name}.${target.version}.bundle`;
89
- this.info(`${output.file} -> ${output.file.replace(file, fileName)}...`);
90
- const compiled = await compile(this, target.dir, js, options.flags ?? []);
91
- if (!compiled) continue;
92
- this.emitFile({
93
- type: "asset",
94
- fileName,
95
- source: compiled.bytecode
112
+ const directory = await mkdtemp(join(tmpdir(), "hermesc-"));
113
+ try {
114
+ await compileTargets(this, targets, options.flags ?? [], {
115
+ directory,
116
+ file,
117
+ name,
118
+ outputFile,
119
+ code: chunk.code
120
+ });
121
+ } finally {
122
+ await rm(directory, {
123
+ recursive: true,
124
+ force: true
96
125
  });
97
- if (compiled.map) {
98
- const map = {
99
- ...JSON.parse(compiled.map),
100
- sources: [file]
101
- };
102
- this.emitFile({
103
- type: "asset",
104
- fileName: `${fileName}.map`,
105
- source: JSON.stringify(map)
106
- });
107
- }
108
- this.info(`( ͡° ͜ʖ ͡°) Bytecode compiled to ${fileName}`);
109
126
  }
110
- await rm(js, { force: true });
111
- },
112
- async buildEnd() {
113
- await rm(temp, {
114
- recursive: true,
115
- force: true
116
- });
117
127
  }
118
128
  };
119
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unbound-app/rollup-plugin-hermes",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Compiles rollup and rolldown bundles to Hermes bytecode with every hermesc version Discord may run.",
5
5
  "author": {
6
6
  "name": "Mario P.",