Skip to content

Commit

Permalink
fix: try to read build dist file first (#27)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced new functions and constants to enhance TypeScript module
handling.
- Added default and named exports in multiple modules for improved
export capabilities.
- Created a new `package.json` file defining module types and exports
for the project.

- **Bug Fixes**
- Updated test cases for clarity and expanded coverage for TypeScript
modules.

- **Chores**
- Renamed and removed certain exports to streamline the module
structure.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 24, 2024
1 parent 124073b commit 7a89153
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 32 deletions.
63 changes: 42 additions & 21 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import fs from 'node:fs';

const debug = debuglog('@eggjs/utils/import');

let _customRequire: NodeRequire;

export interface ImportResolveOptions {
paths?: string[];
}
Expand All @@ -17,6 +15,9 @@ export interface ImportModuleOptions extends ImportResolveOptions {
importDefaultOnly?: boolean;
}

const isESM = typeof require === 'undefined';

let _customRequire: NodeRequire;
function getRequire() {
if (!_customRequire) {
if (typeof require !== 'undefined') {
Expand All @@ -28,36 +29,56 @@ function getRequire() {
return _customRequire;
}

let supportTypeScript: boolean | undefined;
let _supportTypeScript: boolean | undefined;
function isSupportTypeScript() {
if (supportTypeScript === undefined) {
if (_supportTypeScript === undefined) {
const extensions = getRequire().extensions;
supportTypeScript = extensions['.ts'] !== undefined;
debug('[isSupportTypeScript] %o, extensions: %j', supportTypeScript, Object.keys(extensions));
_supportTypeScript = extensions['.ts'] !== undefined;
debug('[isSupportTypeScript] %o, extensions: %j', _supportTypeScript, Object.keys(extensions));
}
return _supportTypeScript;
}

function tryToGetTypeScriptMainFile(pkg: any, baseDir: string): string | undefined {
// try to read pkg.main or pkg.module first
// "main": "./dist/commonjs/index.js",
// "module": "./dist/esm/index.js"
const defaultMainFile = isESM ? pkg.module ?? pkg.main : pkg.main;
if (defaultMainFile) {
const mainIndexFilePath = path.join(baseDir, defaultMainFile);
if (fs.existsSync(mainIndexFilePath)) {
debug('[tryToGetTypeScriptMainFile] %o, use pkg.main or pkg.module: %o, isESM: %s',
mainIndexFilePath, defaultMainFile, isESM);
return;
}
}

// for the module under development
// "tshy": {
// "exports": {
// "./package.json": "./package.json",
// ".": "./src/index.ts"
// }
// }
const mainIndexFile = pkg.tshy?.exports?.['.'];
if (mainIndexFile) {
const mainIndexFilePath = path.join(baseDir, mainIndexFile);
if (fs.existsSync(mainIndexFilePath)) {
return mainIndexFilePath;
}
}
return supportTypeScript;
}

export function importResolve(filepath: string, options?: ImportResolveOptions) {
// support typescript import on absolute path
if (path.isAbsolute(filepath) && isSupportTypeScript()) {
// "tshy": {
// "exports": {
// "./package.json": "./package.json",
// ".": "./src/index.ts"
// }
// }
const pkgFile = path.join(filepath, 'package.json');
if (fs.existsSync(pkgFile)) {
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
const mainIndexFile = pkg.tshy?.exports?.['.'];
if (mainIndexFile) {
const mainIndexFilePath = path.join(filepath, mainIndexFile);
if (fs.existsSync(mainIndexFilePath)) {
debug('[importResolve] %o, options: %o => %o, use typescript', filepath, options, mainIndexFilePath);
return mainIndexFilePath;
}
debug('[importResolve] typescript file %o not exists', mainIndexFilePath);
const mainFile = tryToGetTypeScriptMainFile(pkg, filepath);
if (mainFile) {
debug('[importResolve] %o, use typescript main file: %o', filepath, mainFile);
return mainFile;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/tshy-dist/dist2/commonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.one = 2;
3 changes: 3 additions & 0 deletions test/fixtures/tshy-dist/dist2/commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
1 change: 1 addition & 0 deletions test/fixtures/tshy-dist/dist2/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const one = 2;
3 changes: 3 additions & 0 deletions test/fixtures/tshy-dist/dist2/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
29 changes: 29 additions & 0 deletions test/fixtures/tshy-dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "yadan",
"egg": {
"framework": true
},
"tshy": {
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
}
},
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist2/esm/index.d.ts",
"default": "./dist2/esm/index.js"
},
"require": {
"types": "./dist2/commonjs/index.d.ts",
"default": "./dist2/commonjs/index.js"
}
},
"./package.json": "./package.json"
},
"main": "./dist2/commonjs/index.js",
"types": "./dist2/commonjs/index.d.ts",
"module": "./dist2/esm/index.js"
}
5 changes: 5 additions & 0 deletions test/fixtures/tshy-dist/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
foo: 'bar',
};

export const one = 1;
19 changes: 18 additions & 1 deletion test/fixtures/tshy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,22 @@
".": "./src/index.ts",
"./package.json": "./package.json"
}
}
},
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist2/esm/index.d.ts",
"default": "./dist2/esm/index.js"
},
"require": {
"types": "./dist2/commonjs/index.d.ts",
"default": "./dist2/commonjs/index.js"
}
},
"./package.json": "./package.json"
},
"main": "./dist2/commonjs/index.js",
"types": "./dist2/commonjs/index.d.ts",
"module": "./dist2/esm/index.js"
}
4 changes: 0 additions & 4 deletions test/fixtures/tshy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export class Application {}
export class Agent {}
export async function startCluster() {}

export default {
foo: 'bar',
};
Expand Down
18 changes: 12 additions & 6 deletions test/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ describe('test/import.test.ts', () => {
assert.equal(importResolve(getFilepath('esm')), getFilepath('esm/index.js'));
});

it('should work on typescript', () => {
it('should work on typescript without dist', () => {
assert.equal(importResolve(getFilepath('tshy')), getFilepath('tshy/src/index.ts'));
});

it('should work on typescript with dist', () => {
assert.equal(importResolve(getFilepath('tshy-dist')), getFilepath('tshy-dist/dist2/commonjs/index.js'));
});
});

describe('importModule()', () => {
Expand Down Expand Up @@ -108,14 +112,11 @@ describe('test/import.test.ts', () => {
assert.equal(obj.one, 1);
});

it('should work on tshy', async () => {
it('should work on tshy without dist', async () => {
let obj = await importModule(getFilepath('tshy'));
assert.deepEqual(Object.keys(obj), [
'Application',
'Agent',
'one',
'startCluster',
'default',
'one',
]);
assert.equal(obj.one, 1);
assert.deepEqual(obj.default, { foo: 'bar' });
Expand All @@ -124,6 +125,11 @@ describe('test/import.test.ts', () => {
assert.deepEqual(obj, { foo: 'bar' });
});

it('should work on tshy with dist', async () => {
const obj = await importModule(getFilepath('tshy-dist'));
assert.equal(obj.one, 2);
});

it('should work on ts-module', async () => {
let obj = await importModule(getFilepath('ts-module'));
assert.deepEqual(Object.keys(obj), [ 'default', 'one' ]);
Expand Down

0 comments on commit 7a89153

Please sign in to comment.