Skip to content

Commit

Permalink
fix(bundle-source): Export types properly (merge #2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Feb 28, 2024
2 parents 70c85ef + c180049 commit 18fb659
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/bundle-source/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const makeBundleCache = (wr, cwd, readPowers, opts) => {

/**
* @param {string} dest
* @param {{ format?: string, cacheOpts?: CacheOpts, cacheSourceMaps: boolean, dev?: boolean, log?: Logger }} options
* @param {{ format?: string, cacheOpts?: CacheOpts, cacheSourceMaps?: boolean, dev?: boolean, log?: Logger }} options
* @param {(id: string) => Promise<any>} loadModule
* @param {number} [pid]
* @param {number} [nonce]
Expand Down
11 changes: 6 additions & 5 deletions packages/bundle-source/src/bundle-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
const DEFAULT_MODULE_FORMAT = 'endoZipBase64';
const SUPPORTED_FORMATS = ['getExport', 'nestedEvaluate', 'endoZipBase64'];

/** @type {import('./types').BundleSource} */
// @ts-ignore cast
/**
* @type {import('./types').BundleSourceGeneral}
*/
const bundleSource = async (
startFilename,
options = {},
Expand All @@ -13,8 +14,6 @@ const bundleSource = async (
if (typeof options === 'string') {
options = { format: options };
}
/** @type {{ format: import('./types').ModuleFormat }} */
// @ts-expect-error cast (xxx params)
const { format: moduleFormat = DEFAULT_MODULE_FORMAT } = options;

switch (moduleFormat) {
Expand Down Expand Up @@ -52,4 +51,6 @@ const bundleSource = async (
}
};

export default bundleSource;
export default /** @satisfies {import('./types').BundleSource} */ (
bundleSource
);
9 changes: 4 additions & 5 deletions packages/bundle-source/src/nested-evaluate-and-get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ function longestCommonPrefix(strings) {
}

/**
* @template {'nestedEvaluate' | 'getExport'} T
* @param {string} startFilename
* @param {T} moduleFormat
* @param {any} powers
* @type {import('./types').BundleSourceWithFormat}
*/
export async function bundleNestedEvaluateAndGetExports(
startFilename,
Expand All @@ -47,7 +44,7 @@ export async function bundleNestedEvaluateAndGetExports(
pathResolve = path.resolve,
pathToFileURL = url.pathToFileURL,
externals = [],
} = powers || {};
} = /** @type {any} */ (powers || {});
const resolvedPath = pathResolve(startFilename);
const bundle = await rollup({
input: resolvedPath,
Expand Down Expand Up @@ -160,6 +157,7 @@ ${sourceBundle[entrypoint]}
return module.exports;
}
${sourceMap}`;
// @ts-expect-error generic T not assignable to moduleFormat
return harden({ moduleFormat, source, sourceMap });
} else if (moduleFormat === 'nestedEvaluate') {
sourceMap = `//# sourceURL=${DEFAULT_FILE_PREFIX}-preamble.js\n`;
Expand Down Expand Up @@ -284,6 +282,7 @@ function getExportWithNestedEvaluate(filePrefix) {
return computeExports(entrypoint, { require: systemRequire, systemEval }, {});
}
${sourceMap}`;
// @ts-expect-error generic T not assignable to moduleFormat
return harden({ moduleFormat, source, sourceMap });
}

Expand Down
5 changes: 4 additions & 1 deletion packages/bundle-source/src/zip-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const readPowers = makeReadPowers({ fs, url, crypto });

/**
* @type {import('./types').BundleSourceWithOptions}
*/
export async function bundleZipBase64(
startFilename,
options = {},
options,
grantedPowers = {},
) {
const { dev = false, cacheSourceMaps = false, commonDependencies } = options;
Expand Down

0 comments on commit 18fb659

Please sign in to comment.