Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Wrap manager entries to handle exports #20308

Merged
merged 2 commits into from
Dec 16, 2022
Merged
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
11 changes: 8 additions & 3 deletions code/lib/builder-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
import { getData } from './utils/data';
import { safeResolve } from './utils/safeResolve';
import { readOrderedFiles } from './utils/files';
import { wrapManagerEntries } from './utils/managerEntries';

let compilation: Compilation;
let asyncIterator: ReturnType<StarterFunction> | ReturnType<BuilderFunction>;
Expand All @@ -34,10 +35,14 @@ export const getConfig: ManagerBuilder['getConfig'] = async (options) => {
getTemplatePath('addon.tsconfig.json'),
]);

const entryPoints = customManagerEntryPoint
? [...addonsEntryPoints, customManagerEntryPoint]
: addonsEntryPoints;

const realEntryPoints = await wrapManagerEntries(entryPoints);

return {
entryPoints: customManagerEntryPoint
? [...addonsEntryPoints, customManagerEntryPoint]
: addonsEntryPoints,
entryPoints: realEntryPoints,
outdir: join(options.outputDir || './', 'sb-addons'),
format: 'esm',
write: false,
Expand Down
32 changes: 32 additions & 0 deletions code/lib/builder-manager/src/utils/managerEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { join, parse, relative } from 'path';
import fs from 'fs-extra';
import os from 'os';

/**
* Manager entries should be **self-invoking** bits of code.
* They can of-course import from modules, and ESbuild will bundle all of that into a single file.
* But they should not export anything. However this can't be enforced, so what we do is wrap the given file, in a bit of code like this:
*
* ```js
* import '<<file>>';
* ```
*
* That way we are indicating to ESbuild that we do not care about this files exports, and they will be dropped in the bundle.
*
* We do all of that so we can wrap a try-catch around the code.
* That would have been invalid syntax had the export statements been left in place.
*
* We need to wrap each managerEntry with a try-catch because if we do not, a failing managerEntry can stop execution of other managerEntries.
*/
export async function wrapManagerEntries(entrypoints: string[]) {
return Promise.all(
entrypoints.map(async (entry) => {
const { name, dir } = parse(entry);
const location = join(os.tmpdir(), relative(process.cwd(), dir), `${name}-bundle.mjs`);
await fs.ensureFile(location);
await fs.writeFile(location, `import '${entry}';`);

return location;
})
);
}