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

feat: support esm #268

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.tsx?$': 'ts-jest',
},
preset: 'ts-jest',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testEnvironment: 'node',
testPathIgnorePatterns: [
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@artus/core",
"version": "2.1.1",
"version": "2.2.0",
"description": "Core package of Artus",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -44,19 +44,20 @@
"@artus/eslint-config-artus": "0.0.1",
"@artus/tsconfig": "1.0.1",
"@babel/core": "^7.18.6",
"@types/jest": "^27.4.1",
"@types/jest": "^29.5.11",
"@types/js-yaml": "^4.0.5",
"@types/koa": "^2.13.4",
"@types/minimatch": "^3.0.5",
"@types/node": "^20.10.5",
"axios": "^0.26.1",
"babel-jest": "^27.5.1",
"babel-jest": "^29.7.0",
"egg-ci": "^2.1.0",
"eslint": "^8.19.0",
"eslint-plugin-import": "^2.26.0",
"jest": "^27.5.1",
"jest": "^29.7.0",
"koa": "^2.13.4",
"reflect-metadata": "^0.1.13",
"ts-jest": "^27.1.3",
"ts-jest": "^29.1.1",
"typescript": "^4.8.0"
},
"dependencies": {
Expand All @@ -71,4 +72,4 @@
"ci": {
"version": "16, 18"
}
}
}
2 changes: 1 addition & 1 deletion src/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class ConfigurationHandler {
}

async setConfigByFile(fileItem: ManifestItem) {
const configContent: ConfigObject = await compatibleRequire(fileItem.path);
const configContent: ConfigObject = await compatibleRequire(fileItem.path + fileItem.extname);
if (configContent) {
const env = ConfigurationHandler.getEnvFromFilename(fileItem.filename);
this.setConfig(env, configContent);
Expand Down
2 changes: 1 addition & 1 deletion src/loader/impl/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConfigLoader implements Loader {
}

protected async loadConfigFile(item: ManifestItem): Promise<Record<string, any>> {
const originConfigObj = await compatibleRequire(item.path);
const originConfigObj = await compatibleRequire(item.path + item.extname);
let configObj = originConfigObj;
if (typeof originConfigObj === 'function') {
const app = this.container.get(ArtusInjectEnum.Application);
Expand Down
2 changes: 1 addition & 1 deletion src/loader/impl/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LifecycleLoader implements Loader {
}

async load(item: ManifestItem) {
const origin: Constructable<ApplicationLifecycle>[] = await compatibleRequire(item.path, true);
const origin: Constructable<ApplicationLifecycle>[] = await compatibleRequire(item.path + item.extname, true);
item.loaderState = Object.assign({ exportNames: ['default'] }, item.loaderState);
const { loaderState: state } = item as { loaderState: { exportNames: string[] } };

Expand Down
2 changes: 1 addition & 1 deletion src/loader/impl/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ModuleLoader implements Loader {
}

async load(item: ManifestItem): Promise<Constructable[]> {
const origin = await compatibleRequire(item.path, true);
const origin = await compatibleRequire(item.path + item.extname, true);
item.loaderState = Object.assign({ exportNames: ['default'] }, item.loaderState);
const { loaderState: state } = item as { loaderState: { exportNames: string[] } };

Expand Down
16 changes: 14 additions & 2 deletions src/utils/compatible_require.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import assert from 'assert';
import * as tslib from 'tslib';

/**
* compatible esModule require
* @param path
*/
export default async function compatibleRequire(path: string, origin = false): Promise<any> {
const requiredModule = await import(path);
if (path.endsWith('.json')) {
return require(path);
}

assert(requiredModule, `module '${path}' exports is undefined`);
let requiredModule;
try {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
requiredModule = tslib.__importStar(require(path));
assert(requiredModule, `module '${path}' exports is undefined`);
} catch {
requiredModule = await import(path);
assert(requiredModule, `module '${path}' exports is undefined`);
requiredModule = requiredModule.__esModule ? requiredModule.default ?? requiredModule : requiredModule;
}

return origin ? requiredModule : (requiredModule.default || requiredModule);
}
Loading
Loading