Skip to content
Draft
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
44 changes: 32 additions & 12 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,22 @@ const onEndPlugin = {
/** The name of the virtual `entry-points` module. */
const SHARED_ENTRYPOINT = "entry-points";

/** The property name under which `upload-lib`'s namespace is exposed on `entry-points`. */
const UPLOAD_LIB_EXPORT = "uploadLib";

/** The relative source path of the `upload-lib` module that we re-export from `entry-points`. */
const UPLOAD_LIB_SRC = "./src/upload-lib";

/**
* This plugin finds all source files that contain Action entry points.
* It then generates the virtual `entry-points` module which imports all identified files,
* and re-exports their `runWrapper` functions with suitable aliases.
* A tiny stub file is emitted for each Action entrypoint. Each stub imports the shared bundle
* and calls the respective entry point.
* This plugin finds all source files that contain Action entry points. It then generates the
* virtual `entry-points` module which imports all identified files, and re-exports their
* `runWrapper` functions with suitable aliases.
*
* The virtual module additionally re-exports `upload-lib` under the `uploadLib` namespace so that
* external consumers can access it via the small `lib/upload-lib.js` stub emitted below.
*
* A tiny stub file is emitted for each Action entrypoint, and one for `upload-lib`. Each stub
* imports the shared bundle and calls/re-exports from the respective entry point.
*
* @type {esbuild.Plugin}
*/
Expand Down Expand Up @@ -136,15 +146,19 @@ const entryPointsPlugin = {
)
.join("\n\n");

// Also re-export the `upload-lib` namespace so that external consumers can reach it
// via the `lib/upload-lib.js` stub without us having to bundle a second copy.
const uploadLibReExport = `export * as ${UPLOAD_LIB_EXPORT} from "${UPLOAD_LIB_SRC}";`;

return {
contents: `"use strict";\n${imports}\n\n${wrappers}\n`,
contents: `"use strict";\n${imports}\n\n${wrappers}\n\n${uploadLibReExport}\n`,
resolveDir: ".",
loader: "ts",
};
});

// Emit entry point stubs for each Action using the entry template.
build.onEnd(async (result) => {
build.onEnd(async () => {
// Read the entry point template.
const templatePath = "action-entry.js.tpl";
const template = await readFile(join(SRC_DIR, templatePath), "utf-8");
Expand All @@ -163,16 +177,22 @@ const entryPointsPlugin = {
template.replaceAll("__ACTION__", action.pascalCaseName),
);
}

// Write a small stub for `upload-lib` that re-exports it from the shared bundle.
// External callers (e.g. internal testing environments) `require("./lib/upload-lib")`
// and expect the same shape as before, so we expose the namespace as `module.exports`.
await writeFile(
join(OUT_DIR, "upload-lib.js"),
`// Automatically generated stub re-exporting '${UPLOAD_LIB_SRC}.ts' from '${SHARED_ENTRYPOINT}.js'.\n\n` +
`"use strict";\n\n` +
`module.exports = require("./${SHARED_ENTRYPOINT}").${UPLOAD_LIB_EXPORT};\n`,
);
});
},
};

const context = await esbuild.context({
// Include upload-lib.ts as an entry point for use in testing environments.
entryPoints: [
{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT },
join(SRC_DIR, "upload-lib.ts"),
],
entryPoints: [{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT }],
bundle: true,
format: "cjs",
outdir: OUT_DIR,
Expand Down
27 changes: 25 additions & 2 deletions lib/entry-points.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading