Skip to content

Commit

Permalink
rework typescript types to improve type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 12, 2023
1 parent 5d80704 commit 1a2a000
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
8 changes: 4 additions & 4 deletions lib/deno/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ declare const ESBUILD_VERSION: string

export let version = ESBUILD_VERSION

export let build: typeof types.build = (options: types.BuildOptions): Promise<any> =>
export let build: typeof types.build = (options: types.BuildOptions) =>
ensureServiceIsRunning().then(service =>
service.build(options))

export const serve: typeof types.serve = (serveOptions, buildOptions) =>
ensureServiceIsRunning().then(service =>
service.serve(serveOptions, buildOptions))

export const transform: typeof types.transform = (input, options) =>
export const transform: typeof types.transform = (input: string | Uint8Array, options?: types.TransformOptions) =>
ensureServiceIsRunning().then(service =>
service.transform(input, options))

Expand Down Expand Up @@ -275,8 +275,8 @@ let ensureServiceIsRunning = (): Promise<Service> => {
}))
},

transform: (input, options) => {
return new Promise((resolve, reject) =>
transform: (input: string | Uint8Array, options?: types.TransformOptions) => {
return new Promise<types.TransformResult>((resolve, reject) =>
service.transform({
callName: 'transform',
refs: null,
Expand Down
8 changes: 4 additions & 4 deletions lib/deno/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ declare let WEB_WORKER_FUNCTION: (postMessage: (data: Uint8Array) => void) => (e

export let version = ESBUILD_VERSION

export let build: typeof types.build = (options: types.BuildOptions): Promise<any> =>
export let build: typeof types.build = (options: types.BuildOptions) =>
ensureServiceIsRunning().then(service =>
service.build(options))

export const serve: typeof types.serve = () => {
throw new Error(`The "serve" API does not work in Deno via WebAssembly`)
}

export const transform: typeof types.transform = (input, options) =>
export const transform: typeof types.transform = (input: string | Uint8Array, options?: types.TransformOptions) =>
ensureServiceIsRunning().then(service =>
service.transform(input, options))

Expand Down Expand Up @@ -144,8 +144,8 @@ const startRunningService = async (wasmURL: string | URL, wasmModule: WebAssembl
defaultWD: '/',
callback: (err, res) => err ? reject(err) : resolve(res as types.BuildResult),
})),
transform: (input, options) =>
new Promise((resolve, reject) =>
transform: (input: string | Uint8Array, options?: types.TransformOptions) =>
new Promise<types.TransformResult>((resolve, reject) =>
service.transform({
callName: 'transform',
refs: null,
Expand Down
10 changes: 5 additions & 5 deletions lib/npm/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ declare let WEB_WORKER_FUNCTION: (postMessage: (data: Uint8Array) => void) => (e

export let version = ESBUILD_VERSION;

export let build: typeof types.build = (options: types.BuildOptions): Promise<any> =>
export let build: typeof types.build = (options: types.BuildOptions) =>
ensureServiceIsRunning().build(options);

export const serve: typeof types.serve = () => {
throw new Error(`The "serve" API only works in node`);
};

export const transform: typeof types.transform = (input, options) =>
export const transform: typeof types.transform = (input: string | Uint8Array, options?: types.TransformOptions) =>
ensureServiceIsRunning().transform(input, options);

export const formatMessages: typeof types.formatMessages = (messages, options) =>
Expand Down Expand Up @@ -122,7 +122,7 @@ const startRunningService = async (wasmURL: string | URL, wasmModule: WebAssembl
await firstMessagePromise

longLivedService = {
build: (options: types.BuildOptions): Promise<any> =>
build: (options: types.BuildOptions) =>
new Promise<types.BuildResult>((resolve, reject) =>
service.buildOrServe({
callName: 'build',
Expand All @@ -133,8 +133,8 @@ const startRunningService = async (wasmURL: string | URL, wasmModule: WebAssembl
defaultWD: '/',
callback: (err, res) => err ? reject(err) : resolve(res as types.BuildResult),
})),
transform: (input, options) =>
new Promise((resolve, reject) =>
transform: (input: string | Uint8Array, options?: types.TransformOptions) =>
new Promise<types.TransformResult>((resolve, reject) =>
service.transform({
callName: 'transform',
refs: null,
Expand Down
14 changes: 7 additions & 7 deletions lib/npm/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ let fsAsync: common.StreamFS = {

export let version = ESBUILD_VERSION;

export let build: typeof types.build = (options: types.BuildOptions): Promise<any> =>
export let build: typeof types.build = (options: types.BuildOptions) =>
ensureServiceIsRunning().build(options);

export let serve: typeof types.serve = (serveOptions, buildOptions) =>
ensureServiceIsRunning().serve(serveOptions, buildOptions);

export let transform: typeof types.transform = (input, options) =>
export let transform: typeof types.transform = (input: string | Uint8Array, options?: types.TransformOptions) =>
ensureServiceIsRunning().transform(input, options);

export let formatMessages: typeof types.formatMessages = (messages, options) =>
Expand All @@ -145,7 +145,7 @@ export let formatMessages: typeof types.formatMessages = (messages, options) =>
export let analyzeMetafile: typeof types.analyzeMetafile = (messages, options) =>
ensureServiceIsRunning().analyzeMetafile(messages, options);

export let buildSync: typeof types.buildSync = (options: types.BuildOptions): any => {
export let buildSync: typeof types.buildSync = (options: types.BuildOptions) => {
// Try using a long-lived worker thread to avoid repeated start-up overhead
if (worker_threads && !isInternalWorkerThread) {
if (!workerThreadService) workerThreadService = startWorkerThreadService(worker_threads);
Expand All @@ -165,7 +165,7 @@ export let buildSync: typeof types.buildSync = (options: types.BuildOptions): an
return result!;
};

export let transformSync: typeof types.transformSync = (input, options) => {
export let transformSync: typeof types.transformSync = (input: string | Uint8Array, options?: types.TransformOptions) => {
// Try using a long-lived worker thread to avoid repeated start-up overhead
if (worker_threads && !isInternalWorkerThread) {
if (!workerThreadService) workerThreadService = startWorkerThreadService(worker_threads);
Expand Down Expand Up @@ -320,8 +320,8 @@ let ensureServiceIsRunning = (): Service => {
defaultWD, callback: (err, res) => err ? reject(err) : resolve(res as types.ServeResult),
}))
},
transform: (input, options) => {
return new Promise((resolve, reject) =>
transform: (input: string | Uint8Array, options?: types.TransformOptions) => {
return new Promise<types.TransformResult>((resolve, reject) =>
service.transform({
callName: 'transform',
refs,
Expand Down Expand Up @@ -397,7 +397,7 @@ interface MainToWorkerMessage {

interface WorkerThreadService {
buildSync(options: types.BuildOptions): types.BuildResult;
transformSync: typeof types.transformSync;
transformSync(input: string | Uint8Array, options?: types.TransformOptions): types.TransformResult;
formatMessagesSync: typeof types.formatMessagesSync;
analyzeMetafileSync: typeof types.analyzeMetafileSync;
}
Expand Down
18 changes: 17 additions & 1 deletion lib/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,13 @@ export function createChannel(streamIn: StreamIn): StreamOut {
let outstanding = 1;
let next = () => {
if (--outstanding === 0) {
let result: types.TransformResult = { warnings, code: response!.code, map: response!.map }
let result: types.TransformResult = {
warnings,
code: response!.code,
map: response!.map,
mangleCache: undefined,
legalComments: undefined,
}
if ('legalComments' in response!) result.legalComments = response?.legalComments
if (response!.mangleCache) result.mangleCache = response?.mangleCache
callback(null, result)
Expand Down Expand Up @@ -972,6 +978,11 @@ function buildOrServeImpl(
let result: types.BuildResult = {
errors: replaceDetailsInMessages(response!.errors, details),
warnings: replaceDetailsInMessages(response!.warnings, details),
outputFiles: undefined,
rebuild: undefined,
stop: undefined,
metafile: undefined,
mangleCache: undefined,
};
copyResponseToResult(response!, result);
runOnEndCallbacks(result, logPluginError, () => {
Expand Down Expand Up @@ -1031,6 +1042,11 @@ function buildOrServeImpl(
let result2: types.BuildResult = {
errors: replaceDetailsInMessages(watchResponse.errors, details),
warnings: replaceDetailsInMessages(watchResponse.warnings, details),
outputFiles: undefined,
rebuild: undefined,
stop: undefined,
metafile: undefined,
mangleCache: undefined,
};

// Note: "onEnd" callbacks should run even when there is no "onRebuild" callback
Expand Down
29 changes: 14 additions & 15 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ export interface BuildIncremental extends BuildResult {
rebuild: BuildInvalidate;
}

export interface BuildResult {
export interface BuildResult<SpecificOptions extends BuildOptions = BuildOptions> {
errors: Message[];
warnings: Message[];
/** Only when "write: false" */
outputFiles?: OutputFile[];
outputFiles: OutputFile[] | (SpecificOptions['write'] extends false ? never : undefined);
/** Only when "incremental: true" */
rebuild?: BuildInvalidate;
rebuild: BuildInvalidate | (SpecificOptions['incremental'] extends true ? never : undefined);
/** Only when "watch: true" */
stop?: () => void;
stop: (() => void) | (SpecificOptions['watch'] extends true ? never : undefined);
/** Only when "metafile: true" */
metafile?: Metafile;
metafile: Metafile | (SpecificOptions['metafile'] extends true ? never : undefined);
/** Only when "mangleCache" is present */
mangleCache?: Record<string, string | false>;
mangleCache: Record<string, string | false> | (SpecificOptions['mangleCache'] extends Object ? never : undefined);
}

export interface BuildFailure extends Error {
Expand Down Expand Up @@ -274,14 +274,14 @@ export interface TransformOptions extends CommonOptions {
footer?: string;
}

export interface TransformResult {
export interface TransformResult<SpecificOptions extends TransformOptions = TransformOptions> {
code: string;
map: string;
warnings: Message[];
/** Only when "mangleCache" is present */
mangleCache?: Record<string, string | false>;
mangleCache: Record<string, string | false> | (SpecificOptions['mangleCache'] extends Object ? never : undefined);
/** Only when "legalComments" is "external" */
legalComments?: string;
legalComments: string | (SpecificOptions['legalComments'] extends 'external' ? never : undefined);
}

export interface TransformFailure extends Error {
Expand Down Expand Up @@ -488,10 +488,7 @@ export interface AnalyzeMetafileOptions {
*
* Documentation: https://esbuild.github.io/api/#build-api
*/
export declare function build(options: BuildOptions & { write: false }): Promise<BuildResult & { outputFiles: OutputFile[] }>;
export declare function build(options: BuildOptions & { incremental: true, metafile: true }): Promise<BuildIncremental & { metafile: Metafile }>;
export declare function build(options: BuildOptions & { incremental: true }): Promise<BuildIncremental>;
export declare function build(options: BuildOptions & { metafile: true }): Promise<BuildResult & { metafile: Metafile }>;
export declare function build<SpecificOptions extends BuildOptions>(options: SpecificOptions): Promise<BuildResult<SpecificOptions>>;
export declare function build(options: BuildOptions): Promise<BuildResult>;

/**
Expand All @@ -516,6 +513,7 @@ export declare function serve(serveOptions: ServeOptions, buildOptions: BuildOpt
*
* Documentation: https://esbuild.github.io/api/#transform-api
*/
export declare function transform<SpecificOptions extends TransformOptions>(input: string | Uint8Array, options?: SpecificOptions): Promise<TransformResult<SpecificOptions>>;
export declare function transform(input: string | Uint8Array, options?: TransformOptions): Promise<TransformResult>;

/**
Expand Down Expand Up @@ -548,7 +546,7 @@ export declare function analyzeMetafile(metafile: Metafile | string, options?: A
*
* Documentation: https://esbuild.github.io/api/#build-api
*/
export declare function buildSync(options: BuildOptions & { write: false }): BuildResult & { outputFiles: OutputFile[] };
export declare function buildSync<SpecificOptions extends BuildOptions>(options: SpecificOptions): BuildResult<SpecificOptions>;
export declare function buildSync(options: BuildOptions): BuildResult;

/**
Expand All @@ -559,7 +557,8 @@ export declare function buildSync(options: BuildOptions): BuildResult;
*
* Documentation: https://esbuild.github.io/api/#transform-api
*/
export declare function transformSync(input: string, options?: TransformOptions): TransformResult;
export declare function transformSync<SpecificOptions extends TransformOptions>(input: string, options?: SpecificOptions): TransformResult<SpecificOptions>;
export declare function transformSync(input: string | Uint8Array, options?: TransformOptions): TransformResult;

/**
* A synchronous version of "formatMessages".
Expand Down

0 comments on commit 1a2a000

Please sign in to comment.