From 7d4239273f67d5f1f854942554de302de9b8d64f Mon Sep 17 00:00:00 2001 From: TeamworkGuy2 Date: Mon, 21 Dec 2020 20:38:44 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#49838=20[module-de?= =?UTF-8?q?ps]=20fix=20'options.transform'=20and=20'options.resolve'=20wit?= =?UTF-8?q?h=20new=20types=20by=20@TeamworkGuy2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/module-deps/index.d.ts | 33 +++++++++++++++++++------- types/module-deps/module-deps-tests.ts | 11 +++++++-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/types/module-deps/index.d.ts b/types/module-deps/index.d.ts index 94706542a1..d9cfa6fdd0 100644 --- a/types/module-deps/index.d.ts +++ b/types/module-deps/index.d.ts @@ -1,12 +1,10 @@ -// Type definitions for module-deps 6.1 +// Type definitions for module-deps 6.2 // Project: https://github.com/browserify/module-deps // Definitions by: TeamworkGuy2 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -import * as browserResolve from "browser-resolve"; - /** * Return an object transform stream 'd' that expects entry filenames or '{ id: ..., file: ... }' objects * as input and produces objects for every dependency from a recursive module traversal as output. @@ -24,7 +22,7 @@ declare namespace moduleDeps { /** * A string or array of string transforms */ - transform?: string | string[]; + transform?: Transform | Transform[]; /** * An array path of strings showing where to look in the package.json @@ -35,7 +33,7 @@ declare namespace moduleDeps { /** * Custom resolve function using the opts.resolve(id, parent, cb) signature that browser-resolve has */ - resolve?: (id: string, opts: browserResolve.SyncOpts, cb: (err?: Error | null, file?: string, pkg?: PackageObject, fakePath?: any) => void) => void; + resolve?: (id: string, opts: ParentObject, cb: (err?: Error | null, file?: string, pkg?: PackageObject, fakePath?: any) => void) => void; /** * A custom dependency detection function. opts.detect(source) should return an array of dependency module names. By default detective is used @@ -99,7 +97,7 @@ declare namespace moduleDeps { // un-documented options used by module-deps basedir?: string; - globalTransform?: any[]; + globalTransform?: Transform | Transform[]; extensions?: string[]; modules?: { [name: string]: any }; expose?: { [name: string]: string }; @@ -108,7 +106,7 @@ declare namespace moduleDeps { } interface ModuleDepsObject extends NodeJS.ReadWriteStream { - resolve(id: string, parent: { id: string }, cb: (err: Error | null, file?: string, pkg?: PackageObject, fakePath?: any) => any): any; + resolve(id: string, parent: Partial & { id: string; [name: string]: any }, cb: (err: Error | null, file?: string, pkg?: PackageObject, fakePath?: any) => any): any; readFile(file: string, id?: any, pkg?: PackageObject): NodeJS.ReadableStream; @@ -129,11 +127,15 @@ declare namespace moduleDeps { /** * Every time a transform is applied to a file, a 'transform' event fires with the instantiated transform stream tr. */ - on(event: "transform", listener: (tr: any, file: string) => any): this; + on(event: "transform", listener: (tr: NodeJS.ReadableStream, file: string) => any): this; /** * Every time a file is read, this event fires with the file path. */ on(event: "file", listener: (file: string, id: string) => any): this; + /** + * When a transform stream emits an error it is passed along to this stream an an 'error' event. + */ + on(event: "error", listener: (err: any) => any): this; /** * When opts.ignoreMissing is enabled, this event fires for each missing package. */ @@ -147,6 +149,8 @@ declare namespace moduleDeps { type CacheCallback = (err: Error | null, res?: { source: string; package: any; deps: { [dep: string]: boolean } }) => void; + type Transform = string | ((file: string, opts: { basedir?: string }) => NodeJS.ReadWriteStream); + interface InputRow { file: string; id: string; @@ -161,6 +165,19 @@ declare namespace moduleDeps { global?: boolean; } + interface ParentObject { + id: string; + filename: string; + basedir: string; + paths: string[]; + package?: any; + packageFilter?: (p: PackageObject, x: string) => PackageObject & { __dirname: string }; + inNodeModules?: boolean; + // undocumented, see 'Options' interface + extensions?: string[]; + modules?: { [name: string]: any }; + } + interface TransformObject { id: string; file: string; diff --git a/types/module-deps/module-deps-tests.ts b/types/module-deps/module-deps-tests.ts index e0930f8f88..884f6c59b5 100644 --- a/types/module-deps/module-deps-tests.ts +++ b/types/module-deps/module-deps-tests.ts @@ -11,7 +11,8 @@ function coreDepsTest() { const opts = { resolve: () => { }, modules: coreModules, - extensions: [".js", ".json"] + extensions: [".js", ".json"], + transform: [] }; const s = moduleDeps(opts); @@ -23,14 +24,20 @@ function coreDepsTest() { } } }); + + s.resolve("id", { id: "id" }, (err, file, pkg) => { + const errMsg: string | null = err != null ? err.message : null; + const ext: string = file != null ? file.substr(file.indexOf(".")) : "js"; + }); } function rifiTest() { const md = moduleDeps({ resolve: (id, parent, cb) => { + const parentDependency = parent.id.substr(1); const dependency = id.substr(1); }, - transform: [], + transform: ["transformer", (file, opts) => null], globalTransform: [], cache: {} });