Skip to content

Commit

Permalink
feat: allow loading ts compiler from cwd (#169)
Browse files Browse the repository at this point in the history
test case under node 8

Co-authored-by: dengruoqi <[email protected]>
  • Loading branch information
luckydrq and dengruoqi authored Feb 16, 2022
1 parent d81e9ec commit 4a54cec
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export const key = '12345';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/example-ts-custom-compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example-ts-custom-compiler"
}
26 changes: 26 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 [email protected]', { 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'),
Expand Down

0 comments on commit 4a54cec

Please sign in to comment.