Skip to content

Commit

Permalink
fix: reading module export item
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Mar 10, 2023
1 parent 7e318f3 commit adf4115
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 51 deletions.
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
},
"rules": {
"class-methods-use-this": "off",
"import/no-cycle": [2, {
"maxDepth": 1
}],
"no-shadow": "off",
"no-use-before-define": "off",

Expand Down
8 changes: 4 additions & 4 deletions src/loader/built-in/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
isObject,
} from '../../../utils';
import type { Loader } from '../../type';
import type { ScriptFileLoadOptions } from './type';
import type { ModuleLoadOptions } from './type';
import { isJestRuntimeEnvironment, isTsNodeRuntimeEnvironment } from './utils';

export class ModuleLoader implements Loader {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class ModuleLoader implements Loader {

async load(
data: LocatorInfo | string,
options?: ScriptFileLoadOptions,
options?: ModuleLoadOptions,
) : Promise<unknown> {
options = options || {};
const [name, locatorInfo] = this.build(data, options);
Expand Down Expand Up @@ -132,7 +132,7 @@ export class ModuleLoader implements Loader {

loadSync(
data: LocatorInfo | string,
options?: ScriptFileLoadOptions,
options?: ModuleLoadOptions,
) : unknown {
options = options || {};
const [name, locatorInfo] = this.build(data, options);
Expand Down Expand Up @@ -174,7 +174,7 @@ export class ModuleLoader implements Loader {

private build(
data: LocatorInfo | string,
options: ScriptFileLoadOptions,
options: ModuleLoadOptions,
) : [string, LocatorInfo | undefined] {
let name : string;
let locatorInfo : LocatorInfo | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/loader/built-in/module/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

export type LoaderFilterFn = (key: string, value: unknown) => boolean;

export type ScriptFileExportItem = {
export type ModuleExport = {
key: string,
value: unknown
};

export type ScriptFileLoadOptions = {
export type ModuleLoadOptions = {
withExtension?: boolean,
withFilePrefix?: boolean
};
48 changes: 48 additions & 0 deletions src/loader/built-in/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* view the LICENSE file that was distributed with this source code.
*/

import { BaseError } from 'ebec';
import { hasOwnProperty, isObject } from '../../../utils';
import type { LoaderFilterFn, ModuleExport } from './type';

export function isJestRuntimeEnvironment() : boolean {
return process.env &&
process.env.JEST_WORKER_ID !== undefined;
Expand All @@ -15,3 +19,47 @@ export function isTsNodeRuntimeEnvironment() : boolean {
// @ts-ignore
return !!process[Symbol.for('ts-node.register.instance')];
}

export function getModuleExport(
data: Record<string, any>,
filterFn?: LoaderFilterFn,
): ModuleExport {
if (filterFn) {
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (filterFn(keys[i] as string, data[keys[i] as string])) {
return {
key: keys[i] as string,
value: data[keys[i] as string],
};
}
}

throw new BaseError('Cannot find specific module export.');
}

let value: any;

if (
hasOwnProperty(data, '__esModule') &&
// eslint-disable-next-line no-underscore-dangle
!!data.__esModule &&
hasOwnProperty(data, 'default')
) {
value = data.default;
} else {
value = data;
}

if (
isObject(value) &&
hasOwnProperty(value, 'default')
) {
value = value.default;
}

return {
key: 'default',
value,
};
}
1 change: 0 additions & 1 deletion src/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
export * from './built-in';
export * from './helpers';
export * from './module';
export * from './utils';
34 changes: 0 additions & 34 deletions src/loader/utils.ts

This file was deleted.

15 changes: 8 additions & 7 deletions test/unit/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import path from "node:path";
import {
load,
loadSync,
getExportItem, LoaderManager
getModuleExport,
LoaderManager
} from "../../src";
import {LoaderId} from "../../src/loader/constants";

Expand Down Expand Up @@ -69,7 +70,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file.js');

let loaderContent = await load(filePath) as Record<string, any>;
loaderContent = getExportItem(loaderContent);
loaderContent = getModuleExport(loaderContent);
expect(loaderContent).toBeDefined();
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({foo: 'bar'});
Expand All @@ -79,7 +80,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file.js');

let loaderContent = loadSync(filePath) as Record<string, any>;
loaderContent = getExportItem(loaderContent);
loaderContent = getModuleExport(loaderContent);
expect(loaderContent).toBeDefined();
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({foo: 'bar'});
Expand All @@ -97,7 +98,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file-ts.ts');

let loaderContent = await load(filePath) as Record<string, any>;
loaderContent = getExportItem(loaderContent);
loaderContent = getModuleExport(loaderContent);
expect(loaderContent).toBeDefined();
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({bar: 'baz'});
Expand All @@ -115,7 +116,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file-ts.ts');

let loaderContent = loadSync(filePath) as Record<string, any>;
loaderContent = getExportItem(loaderContent);
loaderContent = getModuleExport(loaderContent);
expect(loaderContent).toBeDefined();
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({bar: 'baz'});
Expand All @@ -125,7 +126,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file-many-ts.ts');

let loaderContent = await load(filePath) as Record<string, any>;
loaderContent = await getExportItem(loaderContent, (key) => {
loaderContent = await getModuleExport(loaderContent, (key) => {
return key === 'bar';
}) as Record<string, any>;

Expand All @@ -138,7 +139,7 @@ describe('src/loader/**', () => {
const filePath = path.join(basePath, 'file-many-ts.ts');

let loaderContent = loadSync(filePath) as Record<string, any>;
loaderContent = getExportItem(loaderContent, (key) => {
loaderContent = getModuleExport(loaderContent, (key) => {
return key === 'bar';
}) as Record<string, any>;
expect(loaderContent).toBeDefined();
Expand Down

0 comments on commit adf4115

Please sign in to comment.