From 7499cfef7914cc87ec1f0b906c1355f80868e2c0 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Tue, 8 Aug 2023 11:55:41 -0600 Subject: [PATCH 1/3] fix(bundle-source): more typing and export refinements --- packages/bundle-source/exported.js | 3 +- packages/bundle-source/src/exports.d.ts | 1 + packages/bundle-source/src/exports.js | 2 + packages/bundle-source/src/index.js | 6 +- packages/bundle-source/src/types.js | 81 +++++++++++++------------ 5 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 packages/bundle-source/src/exports.d.ts create mode 100644 packages/bundle-source/src/exports.js diff --git a/packages/bundle-source/exported.js b/packages/bundle-source/exported.js index f4cba017ea..da056f3ed5 100644 --- a/packages/bundle-source/exported.js +++ b/packages/bundle-source/exported.js @@ -1 +1,2 @@ -import './src/types.js'; +/* eslint-disable import/export */ +export * from './src/types.js'; diff --git a/packages/bundle-source/src/exports.d.ts b/packages/bundle-source/src/exports.d.ts new file mode 100644 index 0000000000..06c33f562f --- /dev/null +++ b/packages/bundle-source/src/exports.d.ts @@ -0,0 +1 @@ +export type * from './types.js'; diff --git a/packages/bundle-source/src/exports.js b/packages/bundle-source/src/exports.js new file mode 100644 index 0000000000..b59f8e9710 --- /dev/null +++ b/packages/bundle-source/src/exports.js @@ -0,0 +1,2 @@ +// Just a dummy to use exports.d.ts and satisfy runtime imports. +export {}; diff --git a/packages/bundle-source/src/index.js b/packages/bundle-source/src/index.js index f2a0372182..c48311dbf1 100644 --- a/packages/bundle-source/src/index.js +++ b/packages/bundle-source/src/index.js @@ -17,8 +17,6 @@ import { encodeBase64 } from '@endo/base64'; import SourceMaps from 'source-map'; import { whereEndoCache } from '@endo/where'; -import './types.js'; - const SourceMapConsumer = SourceMaps.SourceMapConsumer; const parseBabel = babelParser.default ? babelParser.default.parse @@ -547,7 +545,7 @@ ${sourceMap}`; throw Error(`unrecognized moduleFormat ${moduleFormat}`); } -/** @type {BundleSource} */ +/** @type {import('./types').BundleSource} */ const bundleSource = async ( startFilename, options = {}, @@ -556,7 +554,7 @@ const bundleSource = async ( if (typeof options === 'string') { options = { format: options }; } - /** @type {{ format: ModuleFormat }} */ + /** @type {{ format: import('./types').ModuleFormat }} */ const { format: moduleFormat = DEFAULT_MODULE_FORMAT } = options; switch (moduleFormat) { diff --git a/packages/bundle-source/src/types.js b/packages/bundle-source/src/types.js index c96f507ebf..2041ea3f1a 100644 --- a/packages/bundle-source/src/types.js +++ b/packages/bundle-source/src/types.js @@ -1,62 +1,65 @@ +// @ts-check +export {}; + +/** + * @typedef {'endoZipBase64' | 'nestedEvaluate' | 'getExport'} ModuleFormat + */ + /** - * @typedef {'getExport' | 'nestedEvaluate' | 'endoZipBase64'} ModuleFormat + * @typedef { & + * BundleSourceSimple & + * BundleSourceWithFormat & + * BundleSourceWithOptions & + * BundleSourceFallback} BundleSource */ /** - * @typedef {BundleSourceEndoZipBase64 & BundleSourceGetExport & BundleSourceNestedEvaluate} BundleSource + * @template {ModuleFormat} T + * @typedef {T extends 'endoZipBase64' ? { + * moduleFormat: 'endoZipBase64', + * endoZipBase64: string, + * endoZipBase64Sha512: string, + * } : T extends 'getExport' | 'nestedEvaluate' ? { + * moduleFormat: T, + * source: string, + * sourceMap: string, + * } : never} BundleSourceResult */ /** - * @callback BundleSourceEndoZipBase64 - * @param {string} startFilename - the filepath to start the bundling from - * @param {ModuleFormatOrOptions<'endoZipBase64' | undefined>} moduleFormat - * @param {object=} powers - * @param {ReadFn=} powers.read - * @param {CanonicalFn=} powers.canonical - * @returns {Promise<{ - * moduleFormat: 'endoZipBase64', - * endoZipBase64: string, - * endoZipBase64Sha512: string, - * }>} + * @typedef {( + * startFilename: string, + * ) => Promise>} BundleSourceSimple */ /** - * @callback BundleSourceGetExport - * @param {string} startFilename - the filepath to start the bundling from - * @param {ModuleFormatOrOptions<'getExport'>} moduleFormat - * @param {object=} powers - * @param {ReadFn=} powers.read - * @param {CanonicalFn=} powers.canonical - * @returns {Promise<{ - * moduleFormat: 'getExport', - * source: string, - * sourceMap: string, - * }>} + * @typedef {( + * startFilename: string, + * format: T, + * powers?: { read?: ReadFn; canonical?: CanonicalFn }, + * ) => Promise>} BundleSourceWithFormat */ /** - * @callback BundleSourceNestedEvaluate - * @param {string} startFilename - the filepath to start the bundling from - * @param {ModuleFormatOrOptions<'nestedEvaluate'>} moduleFormat - * @param {object=} powers - * @param {ReadFn=} powers.read - * @param {CanonicalFn=} powers.canonical - * @returns {Promise<{ - * moduleFormat: 'nestedEvaluate', - * source: string, - * sourceMap: string, - * }>} + * @typedef {( + * startFilename: string, + * bundleOptions: BundleOptions, + * powers?: { read?: ReadFn; canonical?: CanonicalFn }, + * ) => Promise>} BundleSourceWithOptions */ /** - * @template {ModuleFormat | undefined} T - * @typedef {T | BundleOptions} ModuleFormatOrOptions + * @typedef {( + * startFilename: string, + * formatOrOptions?: T | BundleOptions, + * powers?: { read?: ReadFn; canonical?: CanonicalFn }, + * ) => Promise>} BundleSourceFallback */ /** - * @template {ModuleFormat | undefined} T + * @template {ModuleFormat} T * @typedef {object} BundleOptions - * @property {T} format + * @property {T} [format] * @property {boolean} [dev] - development mode, for test bundles that need * access to devDependencies of the entry package. */ From e4ece8899bf5982c5e5857435d327c39f82ec8a6 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Thu, 17 Aug 2023 16:54:32 -0600 Subject: [PATCH 2/3] chore(bundle-source): file moves --- packages/bundle-source/exported.js | 2 -- packages/bundle-source/src/{index.js => bundle-source.js} | 0 2 files changed, 2 deletions(-) delete mode 100644 packages/bundle-source/exported.js rename packages/bundle-source/src/{index.js => bundle-source.js} (100%) diff --git a/packages/bundle-source/exported.js b/packages/bundle-source/exported.js deleted file mode 100644 index da056f3ed5..0000000000 --- a/packages/bundle-source/exported.js +++ /dev/null @@ -1,2 +0,0 @@ -/* eslint-disable import/export */ -export * from './src/types.js'; diff --git a/packages/bundle-source/src/index.js b/packages/bundle-source/src/bundle-source.js similarity index 100% rename from packages/bundle-source/src/index.js rename to packages/bundle-source/src/bundle-source.js From a19794393c8a51fbb0626cfd8adc84896a6547e5 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Thu, 17 Aug 2023 17:01:20 -0600 Subject: [PATCH 3/3] chore(bundle-source): clean up exports --- packages/bundle-source/src/exports.d.ts | 9 ++++++++- packages/bundle-source/src/index.js | 6 ++++++ packages/bundle-source/src/types.js | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 packages/bundle-source/src/index.js diff --git a/packages/bundle-source/src/exports.d.ts b/packages/bundle-source/src/exports.d.ts index 06c33f562f..b274ab6a3b 100644 --- a/packages/bundle-source/src/exports.d.ts +++ b/packages/bundle-source/src/exports.d.ts @@ -1 +1,8 @@ -export type * from './types.js'; +export type { + BundleOptions, + BundleSource, + BundleSourceResult, + ModuleFormat, + CanonicalFn, + ReadFn, +} from './types.js'; diff --git a/packages/bundle-source/src/index.js b/packages/bundle-source/src/index.js new file mode 100644 index 0000000000..518a52d613 --- /dev/null +++ b/packages/bundle-source/src/index.js @@ -0,0 +1,6 @@ +import bundleSource from './bundle-source.js'; + +export default bundleSource; + +// eslint-disable-next-line import/export +export * from './exports.js'; diff --git a/packages/bundle-source/src/types.js b/packages/bundle-source/src/types.js index 2041ea3f1a..31a2d2ad22 100644 --- a/packages/bundle-source/src/types.js +++ b/packages/bundle-source/src/types.js @@ -5,6 +5,8 @@ export {}; * @typedef {'endoZipBase64' | 'nestedEvaluate' | 'getExport'} ModuleFormat */ +// The order of these intersections matters, insofar as Typescript treats the +// last one as the "most generic" version of the overloads. /** * @typedef { & * BundleSourceSimple &