Skip to content

Commit f4d0a7a

Browse files
authored
Merge pull request #3912 from github/henrymercer/smaller-upload-lib
Action size: Reduce duplication between `upload-lib` and `entry-points`
2 parents f62fbc9 + 2a73406 commit f4d0a7a

4 files changed

Lines changed: 80 additions & 93755 deletions

File tree

build.mjs

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,22 @@ const onEndPlugin = {
6565
/** The name of the virtual `entry-points` module. */
6666
const SHARED_ENTRYPOINT = "entry-points";
6767

68+
/** The property name under which `upload-lib`'s namespace is exposed in `entry-points`. */
69+
const UPLOAD_LIB_EXPORT = "uploadLib";
70+
71+
/** The relative source path of the `upload-lib` module that we re-export from `entry-points`. */
72+
const UPLOAD_LIB_SRC = "./src/upload-lib";
73+
6874
/**
69-
* This plugin finds all source files that contain Action entry points.
70-
* It then generates the virtual `entry-points` module which imports all identified files,
71-
* and re-exports their `runWrapper` functions with suitable aliases.
72-
* A tiny stub file is emitted for each Action entrypoint. Each stub imports the shared bundle
73-
* and calls the respective entry point.
75+
* This plugin finds all source files that contain Action entry points. It then generates the
76+
* virtual `entry-points` module which imports all identified files, and re-exports their
77+
* `runWrapper` functions with suitable aliases.
78+
*
79+
* The virtual module additionally re-exports `upload-lib` under the `uploadLib` namespace so that
80+
* external consumers can access it via the small `lib/upload-lib.js` stub emitted below.
81+
*
82+
* A tiny stub file is emitted for each Action entrypoint, and one for `upload-lib`. Each stub
83+
* imports the shared bundle and calls/re-exports from the respective entry point.
7484
*
7585
* @type {esbuild.Plugin}
7686
*/
@@ -136,43 +146,63 @@ const entryPointsPlugin = {
136146
)
137147
.join("\n\n");
138148

149+
// Also re-export the `upload-lib` namespace so that external consumers can reach it
150+
// via the `lib/upload-lib.js` stub without us having to bundle a second copy.
151+
const uploadLibReExport = `export * as ${UPLOAD_LIB_EXPORT} from "${UPLOAD_LIB_SRC}";`;
152+
139153
return {
140-
contents: `"use strict";\n${imports}\n\n${wrappers}\n`,
154+
contents: `"use strict";\n${imports}\n\n${uploadLibReExport}\n\n${wrappers}\n`,
141155
resolveDir: ".",
142156
loader: "ts",
143157
};
144158
});
145159

146160
// Emit entry point stubs for each Action using the entry template.
147-
build.onEnd(async (result) => {
148-
// Read the entry point template.
149-
const templatePath = "action-entry.js.tpl";
150-
const template = await readFile(join(SRC_DIR, templatePath), "utf-8");
151-
152-
const makeHeader = (sourceFile) =>
161+
build.onEnd(async () => {
162+
const makeHeader = (templatePath, sourceFile) =>
153163
`// Automatically generated from '${templatePath}' for 'src/${basename(sourceFile)}'.\n\n`;
154164

165+
// Read the entry point template.
166+
const actionTemplatePath = "action-entry.js.tpl";
167+
const actionTemplate = await readFile(
168+
join(SRC_DIR, actionTemplatePath),
169+
"utf-8",
170+
);
171+
155172
// Write entry point stubs for each Action.
156173
for (const action of actions) {
157174
await writeFile(
158175
join(
159176
OUT_DIR,
160177
`${action.name}${action.isPost ? "-post" : ""}-entry.js`,
161178
),
162-
makeHeader(action.path) +
163-
template.replaceAll("__ACTION__", action.pascalCaseName),
179+
makeHeader(actionTemplatePath, action.path) +
180+
actionTemplate.replaceAll("__ACTION__", action.pascalCaseName),
164181
);
165182
}
183+
184+
// Write a small stub for `upload-lib` that re-exports it from the shared bundle.
185+
// External callers (e.g. internal testing environments) `require("./lib/upload-lib")`
186+
// and expect the same shape as before, so we expose the namespace as `module.exports`.
187+
const uploadLibStubTemplatePath = "upload-lib-stub.js.tpl";
188+
const uploadLibStubTemplate = await readFile(
189+
join(SRC_DIR, uploadLibStubTemplatePath),
190+
"utf-8",
191+
);
192+
await writeFile(
193+
join(OUT_DIR, "upload-lib.js"),
194+
makeHeader(uploadLibStubTemplatePath, `${UPLOAD_LIB_SRC}.ts`) +
195+
uploadLibStubTemplate.replaceAll(
196+
"__UPLOAD_LIB_EXPORT__",
197+
UPLOAD_LIB_EXPORT,
198+
),
199+
);
166200
});
167201
},
168202
};
169203

170204
const context = await esbuild.context({
171-
// Include upload-lib.ts as an entry point for use in testing environments.
172-
entryPoints: [
173-
{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT },
174-
join(SRC_DIR, "upload-lib.ts"),
175-
],
205+
entryPoints: [{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT }],
176206
bundle: true,
177207
format: "cjs",
178208
outdir: OUT_DIR,

lib/entry-points.js

Lines changed: 25 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)