diff --git a/index.d.ts b/index.d.ts index f550a3b435..083a555a42 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ -import * as accepts from 'accepts'; -import * as KoaApplication from 'koa'; -import * as KoaRouter from 'koa-router'; +import accepts = require('accepts'); +import KoaApplication = require('koa'); +import KoaRouter = require('koa-router'); import { EventEmitter } from 'events' import { Readable } from 'stream'; import { Socket } from 'net'; diff --git a/test/fixtures/apps/app-ts-esm/.gitignore b/test/fixtures/apps/app-ts-esm/.gitignore new file mode 100644 index 0000000000..9c0069648f --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/.gitignore @@ -0,0 +1,2 @@ +*.js +node_modules \ No newline at end of file diff --git a/test/fixtures/apps/app-ts-esm/app/controller/foo.ts b/test/fixtures/apps/app-ts-esm/app/controller/foo.ts new file mode 100644 index 0000000000..cc750d40be --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/app/controller/foo.ts @@ -0,0 +1,15 @@ +import { Controller, RequestObjectBody } from 'egg'; + +// add user controller and service +declare module 'egg' { + interface IController { + foo: FooController; + } +} + +// controller +export default class FooController extends Controller { + async index() { + this.ctx.body = 'ok'; + } +} diff --git a/test/fixtures/apps/app-ts-esm/app/router.ts b/test/fixtures/apps/app-ts-esm/app/router.ts new file mode 100644 index 0000000000..f2afed0050 --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/app/router.ts @@ -0,0 +1,5 @@ +import { Application } from 'egg'; + +export default (app: Application) => { + app.get('/foo', app.controller.foo.index); +} diff --git a/test/fixtures/apps/app-ts-esm/config/config.ts b/test/fixtures/apps/app-ts-esm/config/config.ts new file mode 100644 index 0000000000..0ec84155ab --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/config/config.ts @@ -0,0 +1,4 @@ +export default { + keys: 'foo', + serverTimeout: 2 * 60 * 1000, +} diff --git a/test/fixtures/apps/app-ts-esm/package.json b/test/fixtures/apps/app-ts-esm/package.json new file mode 100644 index 0000000000..292c04d533 --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/package.json @@ -0,0 +1,4 @@ +{ + "name": "app-ts", + "version": "1.0.0" +} \ No newline at end of file diff --git a/test/fixtures/apps/app-ts-esm/tsconfig.json b/test/fixtures/apps/app-ts-esm/tsconfig.json new file mode 100644 index 0000000000..80d974e509 --- /dev/null +++ b/test/fixtures/apps/app-ts-esm/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "es2017", + "baseUrl": ".", + "paths": { + "egg": [ + "../../../../index" + ] + }, + "module": "commonjs", + "strict": true, + "noImplicitAny": false, + "esModuleInterop": true + } +} \ No newline at end of file diff --git a/test/ts/index.test.js b/test/ts/index.test.js index 5e8ad84600..93ea81751f 100644 --- a/test/ts/index.test.js +++ b/test/ts/index.test.js @@ -3,7 +3,7 @@ const assert = require('assert'); const request = require('supertest'); const mm = require('egg-mock'); -const runscript = require('runscript'); +const coffee = require('coffee'); const path = require('path'); const fs = require('fs'); const mkdirp = require('mz-modules/mkdirp'); @@ -13,7 +13,14 @@ const baseDir = path.join(__dirname, '../fixtures/apps/app-ts'); describe('test/ts/index.test.js', () => { before(async () => { - await runscript(`tsc -p ${baseDir}/tsconfig.json`, { cwd: baseDir }); + await coffee.fork( + require.resolve('typescript/bin/tsc'), + [ '-p', path.resolve(__dirname, '../fixtures/apps/app-ts/tsconfig.json') ] + ) + // .debug() + .expect('code', 0) + .end(); + const dest = path.join(baseDir, 'node_modules/egg'); await rimraf(dest); await mkdirp(path.dirname(dest)); @@ -55,5 +62,15 @@ describe('test/ts/index.test.js', () => { .expect({ env: 'unittest' }) .end(done); }); + + it('should compile with esModuleInterop without error', async () => { + await coffee.fork( + require.resolve('typescript/bin/tsc'), + [ '-p', path.resolve(__dirname, '../fixtures/apps/app-ts-esm/tsconfig.json') ] + ) + // .debug() + .expect('code', 0) + .end(); + }); }); });