alchemy 0.10.0 → 0.10.1

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.
@@ -95,7 +95,8 @@ export interface Bundle extends Resource<"esbuild::Bundle"> {
95
95
  export declare const Bundle: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<Bundle>, id: string, props: BundleProps) => Promise<Bundle>);
96
96
  export declare function bundle(props: BundleProps): Promise<esbuild.BuildResult<{
97
97
  entryPoints: string[];
98
- outfile: string;
98
+ outdir: string | undefined;
99
+ outfile: string | undefined;
99
100
  bundle: true;
100
101
  format: "iife" | "cjs" | "esm" | undefined;
101
102
  target: string | string[] | undefined;
@@ -107,7 +108,6 @@ export declare function bundle(props: BundleProps): Promise<esbuild.BuildResult<
107
108
  write: true;
108
109
  splitting?: boolean;
109
110
  preserveSymlinks?: boolean;
110
- outdir?: string;
111
111
  outbase?: string;
112
112
  packages?: "bundle" | "external";
113
113
  alias?: Record<string, string>;
@@ -23,34 +23,51 @@ export const Bundle = Resource("esbuild::Bundle", {
23
23
  alwaysUpdate: true,
24
24
  }, async function (id, props) {
25
25
  // Determine output path
26
- const outputPath = getOutputPath(props);
26
+ const outDirPath = props.outfile != null ? path.dirname(props.outfile) : props.outdir;
27
+ if (outDirPath == null) {
28
+ throw new Error(`You need to specify either outfile or outdir in your bundle configuration ${JSON.stringify(props)}`);
29
+ }
27
30
  // Ensure output directory exists
28
- await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
31
+ await fs.promises.mkdir(path.dirname(outDirPath), { recursive: true });
29
32
  if (this.phase === "delete") {
30
- await fs.promises.unlink(outputPath).catch(() => { });
31
- // Also clean up sourcemap if it exists
32
- await fs.promises.unlink(outputPath + ".map").catch(() => { });
33
- return this.destroy();
33
+ await fs.promises.rm(outDirPath, { recursive: true });
34
34
  }
35
35
  const result = await bundle(props);
36
+ // Check that bundle created output an file for the given entrypoint
37
+ // Use bundle metada data to retrieve its content
38
+ const bundleOutputPath = Object.entries(result.metafile.outputs).find(([file, output]) => {
39
+ if (output.entryPoint === undefined) {
40
+ return false;
41
+ }
42
+ // resolve to absolute and then relative to ensure consistent result (e.g. ./src/handler.ts instead of src/handler.ts)
43
+ const relativeOutput = path.relative(process.cwd(), path.resolve(output.entryPoint));
44
+ return (relativeOutput ===
45
+ path.relative(process.cwd(), path.resolve(process.cwd(), props.entryPoint)));
46
+ })?.[0];
47
+ if (!bundleOutputPath) {
48
+ throw new Error(`Unable to find a compiled file`);
49
+ }
36
50
  // Calculate hash of the output
37
- const contents = await fs.promises.readFile(outputPath);
38
- const hash = crypto.createHash("sha256").update(contents).digest("hex");
51
+ const bundleOutputContent = await fs.promises.readFile(bundleOutputPath);
52
+ const hash = crypto
53
+ .createHash("sha256")
54
+ .update(bundleOutputContent)
55
+ .digest("hex");
39
56
  // Store metadata in context
40
57
  await this.set("metafile", result.metafile);
41
58
  await this.set("hash", hash);
42
59
  // Return output info
43
60
  return this({
44
- path: outputPath,
61
+ path: bundleOutputPath,
45
62
  hash,
46
63
  });
47
64
  });
48
65
  export async function bundle(props) {
49
- const outputPath = getOutputPath(props);
50
66
  return await esbuild.build({
51
67
  ...props.options,
52
68
  entryPoints: [props.entryPoint],
53
- outfile: outputPath,
69
+ outdir: props.outdir,
70
+ outfile: props.outfile,
54
71
  bundle: true,
55
72
  format: props.format,
56
73
  target: props.target,
@@ -66,7 +83,3 @@ export async function bundle(props) {
66
83
  write: true,
67
84
  });
68
85
  }
69
- function getOutputPath(props) {
70
- return (props.outfile ||
71
- path.join(props.outdir || "dist", path.basename(props.entryPoint, path.extname(props.entryPoint)) + ".js"));
72
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alchemy",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "type": "module",
5
5
  "module": "./lib/index.js",
6
6
  "scripts": {
@@ -119,22 +119,54 @@ export const Bundle = Resource(
119
119
  props: BundleProps
120
120
  ): Promise<Bundle> {
121
121
  // Determine output path
122
- const outputPath = getOutputPath(props);
122
+ const outDirPath =
123
+ props.outfile != null ? path.dirname(props.outfile) : props.outdir;
123
124
 
124
- // Ensure output directory exists
125
- await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
125
+ if (outDirPath == null) {
126
+ throw new Error(
127
+ `You need to specify either outfile or outdir in your bundle configuration ${JSON.stringify(props)}`
128
+ );
129
+ }
126
130
 
131
+ // Ensure output directory exists
132
+ await fs.promises.mkdir(path.dirname(outDirPath), { recursive: true });
127
133
  if (this.phase === "delete") {
128
- await fs.promises.unlink(outputPath).catch(() => {});
129
- // Also clean up sourcemap if it exists
130
- await fs.promises.unlink(outputPath + ".map").catch(() => {});
131
- return this.destroy();
134
+ await fs.promises.rm(outDirPath, { recursive: true });
132
135
  }
133
136
 
134
137
  const result = await bundle(props);
138
+ // Check that bundle created output an file for the given entrypoint
139
+ // Use bundle metada data to retrieve its content
140
+ const bundleOutputPath = Object.entries(result.metafile.outputs).find(
141
+ ([file, output]) => {
142
+ if (output.entryPoint === undefined) {
143
+ return false;
144
+ }
145
+ // resolve to absolute and then relative to ensure consistent result (e.g. ./src/handler.ts instead of src/handler.ts)
146
+ const relativeOutput = path.relative(
147
+ process.cwd(),
148
+ path.resolve(output.entryPoint)
149
+ );
150
+ return (
151
+ relativeOutput ===
152
+ path.relative(
153
+ process.cwd(),
154
+ path.resolve(process.cwd(), props.entryPoint)
155
+ )
156
+ );
157
+ }
158
+ )?.[0];
159
+
160
+ if (!bundleOutputPath) {
161
+ throw new Error(`Unable to find a compiled file`);
162
+ }
163
+
135
164
  // Calculate hash of the output
136
- const contents = await fs.promises.readFile(outputPath);
137
- const hash = crypto.createHash("sha256").update(contents).digest("hex");
165
+ const bundleOutputContent = await fs.promises.readFile(bundleOutputPath);
166
+ const hash = crypto
167
+ .createHash("sha256")
168
+ .update(bundleOutputContent)
169
+ .digest("hex");
138
170
 
139
171
  // Store metadata in context
140
172
  await this.set("metafile", result.metafile);
@@ -142,18 +174,18 @@ export const Bundle = Resource(
142
174
 
143
175
  // Return output info
144
176
  return this({
145
- path: outputPath,
177
+ path: bundleOutputPath,
146
178
  hash,
147
179
  });
148
180
  }
149
181
  );
150
182
 
151
183
  export async function bundle(props: BundleProps) {
152
- const outputPath = getOutputPath(props);
153
184
  return await esbuild.build({
154
185
  ...props.options,
155
186
  entryPoints: [props.entryPoint],
156
- outfile: outputPath,
187
+ outdir: props.outdir,
188
+ outfile: props.outfile,
157
189
  bundle: true,
158
190
  format: props.format,
159
191
  target: props.target,
@@ -169,13 +201,3 @@ export async function bundle(props: BundleProps) {
169
201
  write: true,
170
202
  });
171
203
  }
172
-
173
- function getOutputPath(props: BundleProps) {
174
- return (
175
- props.outfile ||
176
- path.join(
177
- props.outdir || "dist",
178
- path.basename(props.entryPoint, path.extname(props.entryPoint)) + ".js"
179
- )
180
- );
181
- }