Skip to content

Commit

Permalink
perf: resolved modules caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Jun 8, 2020
1 parent a1b04d2 commit 7eab36d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 63 deletions.
5 changes: 0 additions & 5 deletions src/shims/fibers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ declare namespace fibers {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Fiber {}
}

declare module "fibers" {
const fiber: fibers.Fiber;
export = fiber;
}
9 changes: 2 additions & 7 deletions src/shims/less.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare namespace less {
interface File {
contents?: string;
filename?: string;
contents: string;
filename: string;
}

class AbstractFileManager {
Expand Down Expand Up @@ -66,8 +66,3 @@ declare namespace less {
render(input: string, options?: Options): Promise<RenderOutput>;
}
}

declare module "less" {
const less: less.Less;
export = less;
}
11 changes: 1 addition & 10 deletions src/shims/sass.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ declare namespace sass {

interface Sass {
render: (options: Options, callback: Callback) => void;
renderSync: (options: Options) => Result;
}
}

declare module "sass" {
const sass: sass.Sass;
export = sass;
}

declare module "node-sass" {
const sass: sass.Sass;
export = sass;
}
23 changes: 10 additions & 13 deletions src/shims/stylus.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
declare namespace stylus {
type Callback = (err: Error, css: string) => void;

interface SourceMapOptions {
comment?: boolean;
inline?: boolean;
sourceRoot?: string;
basePath?: string;
}

interface Options {
imports?: string[];
paths?: string[];
filename?: string;
sourcemap?: {
comment?: boolean;
inline?: boolean;
sourceRoot?: string;
basePath?: string;
};
sourcemap?: SourceMapOptions;
}

type Callback = (err: Error, css: string) => void;

interface Renderer {
render(callback: Callback): void;
deps(): string[];
Expand All @@ -32,8 +34,3 @@ declare namespace stylus {
(code: string, options?: Options): Renderer;
}
}

declare module "stylus" {
const stylus: stylus.Stylus;
export = stylus;
}
43 changes: 15 additions & 28 deletions src/utils/load-module.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import resolveAsync from "./resolve-async";
/* eslint-disable @typescript-eslint/no-var-requires */
import { sync as resolveSync } from "resolve";

export interface ModuleImportMap extends Record<string, unknown> {
sass: sass.Sass;
"node-sass": sass.Sass;
fibers: fibers.Fiber;
less: less.Less;
stylus: stylus.Stylus;
}

export default async <T extends keyof ModuleImportMap>(
moduleId: T,
basedir = process.cwd(),
): Promise<ModuleImportMap[T] | undefined> => {
if (typeof moduleId !== "string") return;

try {
return require(moduleId) as ModuleImportMap[T];
} catch {
/* noop */
}
const loaded: Record<string, unknown> = {};
export default function (moduleId: string, basedir = process.cwd()): unknown | undefined {
if (loaded[moduleId]) return loaded[moduleId];
if (loaded[moduleId] === null) return;

try {
return require(await resolveAsync(moduleId, { basedir })) as ModuleImportMap[T];
} catch {
/* noop */
}

try {
return require(await resolveAsync(`./${moduleId}`, { basedir })) as ModuleImportMap[T];
try {
loaded[moduleId] = require(resolveSync(moduleId, { basedir })) as unknown;
} catch {
loaded[moduleId] = require(resolveSync(`./${moduleId}`, { basedir })) as unknown;
}
} catch {
loaded[moduleId] = null;
return;
}
};

return loaded[moduleId];
}

0 comments on commit 7eab36d

Please sign in to comment.