Skip to content

Commit

Permalink
test: add ts module loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 15, 2024
1 parent 9aaeaa4 commit 7bdda0d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { debuglog } from 'node:util';
import path from 'node:path';
import fs from 'node:fs';
import BuiltinModule from 'node:module';
import { isGeneratorFunction } from 'is-type-of';

const debug = debuglog('egg-core:utils');

export type Fun = (...args: any[]) => any;

// Guard against poorly mocked module constructors.
Expand All @@ -11,29 +14,34 @@ const Module = typeof module !== 'undefined' && module.constructor.length > 1
/* istanbul ignore next */
: BuiltinModule;

const extensions = (Module as any)._extensions;
debug('Module extensions: %j', Object.keys(extensions));

export default {
extensions: (Module as any)._extensions,
extensions,

async loadFile(filepath: string) {
try {
// if not js module, just return content buffer
const extname = path.extname(filepath);
if (extname && !(Module as any)._extensions[extname]) {
if (extname && !extensions[extname]) {
return fs.readFileSync(filepath);
}
let obj: any;
let isESM = false;
if (typeof require === 'function') {
// commonjs
obj = require(filepath);
debug('require %s => %o', filepath, obj);
if (obj && obj.__esModule) {
isESM = true;
}
} else {
// esm
obj = await import(filepath);
debug('await import %s => %o', filepath, obj);
isESM = true;
if (obj && obj.__esModule && 'default' in obj) {
if (obj && 'default' in obj) {
// default: { default: [Function (anonymous)] }
obj = obj.default;
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/load_dirs/ts_module/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function() {
return { a: 1 };
}
2 changes: 2 additions & 0 deletions test/fixtures/load_dirs/ts_module/mod2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'bar';
export class HelloFoo {}
4 changes: 4 additions & 0 deletions test/fixtures/load_dirs/ts_module/mod3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
ok: true,
foo: 'bar',
}
15 changes: 14 additions & 1 deletion test/loader/file_loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,20 @@ describe('test/loader/file_loader.test.ts', () => {
directory: path.join(dirBase, 'es6_module'),
target: app.model,
}).load();
assert.deepEqual(app.model.mod.a, 1);
assert.equal(app.model.mod.a, 1);
});

it('should pass ts module', async () => {
const app: Record<string, any> = { model: {} };
await new FileLoader({
directory: path.join(dirBase, 'ts_module'),
target: app.model,
}).load();
assert.equal(app.model.mod.a, 1);
assert.equal(app.model.mod2.foo, 'bar');
assert(app.model.mod2.HelloFoo);
assert.equal(app.model.mod3.ok, true);
assert.equal(app.model.mod3.foo, 'bar');
});

it('should contain syntax error filepath', async () => {
Expand Down

0 comments on commit 7bdda0d

Please sign in to comment.