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

flow typings for node's built-in Module module #5364

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
84 changes: 84 additions & 0 deletions flow-libs/module.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//@flow strict-local

// Derived from the documentation and source of Module located at
// https://nodejs.org/api/module.html#module_modules_module_api and
// https://github.com/nodejs/node/blob/cbfa2d11d4e42ea7f6a847a6930fd1022773252e/lib/internal/modules/cjs/loader.js/
// Which is licensed MIT

declare module 'module' {
declare type FilePath = string;

// Derived from https://github.com/facebook/flow/blob/61913b9f0dcd564fd4c74fefbe896b33ef447cab/lib/node.js
declare class Url {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import type {URL} from 'url'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is addressed in the upstream PR :) facebook/flow#8540

constructor(input: string, base?: string | URL): void;
hash: string;
host: string;
hostname: string;
href: string;
origin: string;
password: string;
pathname: string;
port: string;
protocol: string;
search: string;
+searchParams: URLSearchParams;
username: string;
toString(): string;
toJSON(): string;
}

declare export default class Module {
constructor(id: FilePath, parent?: Module): void;
id: string;
path: FilePath;
paths: FilePath[];
exports: {|[export: string]: mixed|};
filename: string;
loaded: boolean;
children: Module[];
globalPaths: FilePath[];
load(fileName: string): void;
require: (request: string) => {|[export: string]: mixed|};
syncBuiltinESMExports(): void;

static runMain(): void;
static wrap(code: string): string;
static createRequire(
path: string | Url,
): (request: string) => {|[export: string]: mixed|};
static builtinModules: string[];
static _cache: {|[key: FilePath]: Module|};
static _pathCache: {|[cacheKey: string]: FilePath|};
static _nodeModulePaths(from: FilePath): FilePath[];
static _initPaths(): void;
static findPnpApi(lookupSource: string): PnpApi | null;

static _findPath(
request: FilePath,
paths: FilePath[],
isMain: boolean,
): FilePath | false;

static _extensions: {|
[extension: string]: (module: Module, fileName: string) => void,
|};

static _resolveLookupPaths(
request: FilePath,
parent: Module,
): FilePath[] | null;

static _load(
request: FilePath,
parent: Module,
isMain: boolean,
): {|[export: string]: mixed|};

static _resolveFilename: (
request: string,
parent?: Module,
isMain?: boolean,
options?: {|paths?: FilePath[]|},
) => string | false;
}
}
1 change: 0 additions & 1 deletion packages/core/package-manager/src/NodePackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import ThrowableDiagnostic, {
generateJSONCodeHighlights,
} from '@parcel/diagnostic';
import nativeFS from 'fs';
// $FlowFixMe this is untyped
import Module from 'module';
import path from 'path';
import semver from 'semver';
Expand Down
1 change: 0 additions & 1 deletion packages/core/register/src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {NodePackageManager} from '@parcel/package-manager';
import {NodeFS} from '@parcel/fs';
// flowlint-next-line untyped-import:off
import defaultConfigContents from '@parcel/config-default';
// $FlowFixMe Flow can't resolve this
import Module from 'module';
import path from 'path';
import {addHook} from 'pirates';
Expand Down
1 change: 0 additions & 1 deletion packages/core/utils/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {FileSystem} from '@parcel/fs';
import promisify from './promisify';
import _resolve from 'resolve';
import {resolveConfig, resolveConfigSync} from '../';
// $FlowFixMe this is untyped
import Module from 'module';

// Lazily promisify
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/node-resolver-core/src/NodeResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default class NodeResolver {
: // $FlowFixMe injected at runtime
require('pnpapi');

let res = pnp.resolveToUnqualified(
let res = nullthrows(pnp).resolveToUnqualified(
moduleName +
// retain slash in `require('assert/')` to force loading builtin from npm
(filename[moduleName.length] === '/' ? '/' : ''),
Expand Down