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: allow loading ts compiler from cwd #169

Merged
merged 2 commits into from
Feb 16, 2022
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
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') });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adapt for node 8


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