diff --git a/.gitignore b/.gitignore index 447db337..38d66be0 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ test/fixtures/ts/node_modules/aliyun-egg/ test/fixtures/example-ts-ets/typings/ !test/fixtures/example-ts-ets/node_modules/ !test/fixtures/example-ts-simple/node_modules/ +!test/fixtures/example-ts-custom-compiler/node_modules/ **/run/*.json diff --git a/lib/command.js b/lib/command.js index e166a022..c521c40a 100644 --- a/lib/command.js +++ b/lib/command.js @@ -87,7 +87,8 @@ class Command extends BaseCommand { // load ts-node if (argv.typescript) { - execArgvObj.require.push(require.resolve(argv.tscompiler)); + // try to load from `cwd` first + execArgvObj.require.push(require.resolve(argv.tscompiler, { paths: [ cwd ] })); // tell egg loader to load ts file env.EGG_TYPESCRIPT = 'true'; diff --git a/test/fixtures/example-ts-custom-compiler/config/config.default.ts b/test/fixtures/example-ts-custom-compiler/config/config.default.ts new file mode 100644 index 00000000..feefe62a --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler/config/config.default.ts @@ -0,0 +1,3 @@ +'use strict'; + +export const key = '12345'; diff --git a/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js b/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js new file mode 100644 index 00000000..35a2064f --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = require('../../../../../node_modules/egg'); + +setTimeout(() => { + console.log('exit by master test end'); + process.exit(0); +}, 10000); diff --git a/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json b/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts-custom-compiler/package.json b/test/fixtures/example-ts-custom-compiler/package.json new file mode 100644 index 00000000..54197843 --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler/package.json @@ -0,0 +1,3 @@ +{ + "name": "example-ts-custom-compiler" +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 421edb31..bca0a115 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -5,7 +5,9 @@ const coffee = require('coffee'); const mm = require('mm'); const fs = require('fs'); const rimraf = require('mz-modules/rimraf'); +const exec = require('mz/child_process').exec; const os = require('os'); +const assert = require('assert'); describe('test/ts.test.js', () => { const eggBin = require.resolve('../bin/egg-bin'); @@ -149,6 +151,17 @@ describe('test/ts.test.js', () => { }); describe('egg.typescript = true', () => { + const tempNodeModules = path.join(__dirname, './fixtures/node_modules'); + const tempPackageJson = path.join(__dirname, './fixtures/package.json'); + afterEach(async () => { + if (fs.existsSync(tempNodeModules)) { + await rimraf(tempNodeModules); + } + if (fs.existsSync(tempPackageJson)) { + await rimraf(tempPackageJson); + } + }); + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { console.log('skip egg@1'); return; @@ -192,6 +205,19 @@ describe('test/ts.test.js', () => { .end(); }); + it('should load custom ts compiler', async () => { + const cwd = path.join(__dirname, './fixtures/example-ts-custom-compiler'); + + // install custom ts-node + await exec('npx cnpm install ts-node@8.10.2', { cwd: path.join(__dirname, './fixtures') }); + + const { stderr, code } = await coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd, env: { DEBUG: 'egg-bin' } }) + .debug() + .end(); + assert(/ts-node@8\.10\.2/.test(stderr)); + assert.equal(code, 0); + }); + it('should start app with other tscompiler without error', () => { return coffee.fork(eggBin, [ 'dev', '--ts', '--tscompiler=esbuild-register' ], { cwd: path.join(__dirname, './fixtures/example-ts'),