Skip to content

Commit

Permalink
test: add esm case
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jan 25, 2024
1 parent ccc6bf9 commit cea8573
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class EntryGenerator {
private program!: ts.Program;
private sourceDir!: string;
private programFiles!: string[];
private isESM = false;

DEFAULT_PATTERN = ['**/**.ts', '**/**.tsx', '**/**.js'];
DEFAULT_IGNORE_PATTERN = [
Expand Down Expand Up @@ -84,6 +85,11 @@ export class EntryGenerator {
);
}

const pkg = safeRequire(resolve(baseUrl, 'package.json'));
if (pkg?.type === 'module') {
this.isESM = true;
}

this.sourceDir = resolve(baseUrl, srcDir);
this.programFiles = run(
this.DEFAULT_PATTERN.concat(options.pattern || []),
Expand Down Expand Up @@ -156,7 +162,7 @@ export class EntryGenerator {
return `export * from './${formatWindowsPath(
relative(this.sourceDir, path)
)
.replace(extname(path), '')
.replace(extname(path), this.isESM ? '.js' : '')
.replace(/\\/g, '/')}';\n`;
});

Expand All @@ -165,7 +171,10 @@ export class EntryGenerator {
collection.configurationClz
} as Configuration } from './${formatWindowsPath(
relative(this.sourceDir, collection.configurationFilepath)
).replace(extname(collection.configurationFilepath), '')}';\n`
).replace(
extname(collection.configurationFilepath),
this.isESM ? '.js' : ''
)}';\n`
);

exportCodes.unshift(this.BANNER);
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ export * from './interface';
export * from './service/user.service';
"
`;

exports[`/test/index.test.ts support esm project 1`] = `
"/** This file generated by @midwayjs/bundle-helper */
export { ContainerLifeCycle as Configuration } from './configuration.js';
export * from './config/config.default.js';
export * from './controller/test/api.controller.js';
export * from './controller/ych/home.controller.js';
export * from './interface.js';
export * from './service/user.service.js';
"
`;
3 changes: 3 additions & 0 deletions test/fixtures/base-app-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
9 changes: 9 additions & 0 deletions test/fixtures/base-app-esm/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MidwayConfig } from '@midwayjs/core';

const config: MidwayConfig = {};

config.koa = {
port: 7001,
}

export default config;
28 changes: 28 additions & 0 deletions test/fixtures/base-app-esm/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Configuration, App, Inject } from '@midwayjs/decorator';
import * as koa from '@midwayjs/koa';
import * as bodyParser from 'koa-bodyparser';
import * as session from 'koa-session';
import * as axios from '@midwayjs/axios';

@Configuration({
imports: [
koa,
axios,
],
})
export class ContainerLifeCycle {
@App()
app: koa.Application;

@Inject()
httpService: axios.HttpService;

async onReady() {
this.app.keys = ['default value'];
this.app.use(session({key: "SESSIONID"}, this.app))
// bodyparser options see https://github.com/koajs/bodyparser
this.app.useMiddleware([bodyParser()]);

return await this.httpService.get('http://www.weather.com.cn/data/cityinfo/101010100.html');
}
}
Empty file.
13 changes: 13 additions & 0 deletions test/fixtures/base-app-esm/src/controller/test/api.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Inject, Controller, Get } from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';

@Controller('/api')
export class APIController {
@Inject()
ctx: Context;

@Get('/get_user')
async getUser() {
return 'bbb';
}
}
14 changes: 14 additions & 0 deletions test/fixtures/base-app-esm/src/controller/ych/home.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller, Get } from '@midwayjs/decorator';

@Controller('/')
export class HomeController {
@Get('/')
async home(): Promise<string> {
return 'home value';
}

@Get('/login')
async login(): Promise<string> {
return 'login page'
}
}
6 changes: 6 additions & 0 deletions test/fixtures/base-app-esm/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @description User-Service parameters
*/
export interface IUserOptions {
uid: number;
}
14 changes: 14 additions & 0 deletions test/fixtures/base-app-esm/src/service/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Provide } from '@midwayjs/decorator';
import { IUserOptions } from '../interface';

@Provide()
export class UserService {
async getUser(options: IUserOptions) {
return {
uid: options.uid,
username: 'mockedName',
phone: '12345678901',
email: '[email protected]',
};
}
}
24 changes: 24 additions & 0 deletions test/fixtures/base-app-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"skipLibCheck": false,
"pretty": true,
"declaration": true,
"typeRoots": [ "./typings", "./node_modules/@types"],
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
11 changes: 11 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ describe('/test/index.test.ts', () => {
readFileSync(resolve(baseUrl, 'src/index.ts')).toString()
).toMatchSnapshot();
});

it('support esm project', async () => {
const baseUrl = resolve(__dirname, './fixtures/base-app-esm');
const generator = new EntryGenerator({
baseUrl,
});
generator.run();
expect(
readFileSync(resolve(baseUrl, 'src/index.ts')).toString()
).toMatchSnapshot();
});
});

0 comments on commit cea8573

Please sign in to comment.