Skip to content

Commit

Permalink
refactor: support load tsx files
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 22, 2019
1 parent a93b842 commit ee7ab7c
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/midway-core/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class MidwayContainer extends Container implements IContainer {
const loadDirs = [].concat(opts.loadDir || []);

for (const dir of loadDirs) {
const fileResults = globby.sync(['**/**.ts', '**/**.js', '!**/**.d.ts'].concat(opts.pattern || []), {
const fileResults = globby.sync(['**/**.ts', '**/**.tsx', '**/**.js', '!**/**.d.ts'].concat(opts.pattern || []), {
cwd: dir,
ignore: [
'**/node_modules/**',
Expand Down
6 changes: 5 additions & 1 deletion packages/midway-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
],
"license": "MIT",
"devDependencies": {
"@types/react": "^16.0.38",
"@types/react-dom": "^16.0.4",
"midway-bin": "^1.3.0",
"midway-mock": "^1.3.0",
"pedding": "^1.1.0"
"pedding": "^1.1.0",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"dependencies": {
"@eggjs/router": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-web/src/loader/webLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class MidwayWebLoader extends MidwayLoader {

let arr = [];
names.forEach((name) => {
arr = arr.concat([name + '.ts', name + '.js']);
arr = arr.concat([name + '.ts', name + '.tsx', name + '.js']);
});
return arr.concat(['!**/**.d.ts']);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/midway-web/test/enhance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,24 @@ describe('/test/enhance.test.ts', () => {
.expect('root_test', done);
});
});

describe('load tsx file', () => {
let app;
before(() => {
app = utils.app('enhance/base-app-controller-tsx', {
typescript: true
});
return app.ready();
});

after(() => app.close());

it('should load tsx controller', (done) => {
request(app.callback())
.get('/')
.expect(200)
.expect(/react/, done);
});

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { provide } from 'injection';
import { controller, get } from '../../../../../../../src/';
import * as React from 'react';
import * as ReactDOM from 'react-dom/server';
import App from '../../shared/App';

@provide()
@controller('/')
export class My {
@get('/')
async index(ctx) {
ctx.body = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>SSR Test</title>
</head>
<body>
<div id="app">${ReactDOM.renderToString(<App />)}</div>
</body>
</html>
`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};

export const plugins = {
bucLogin: false,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.hello = {
b: 4,
c: 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

h1 {
font-size: 2rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';
import Header from './components/Header';
// import './App.css';

export default () => {
return (
<div>
<Header />
<h1>App</h1>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';

export default () => {
return (
<ul style={{display: 'flex'}}>
<li>Home</li>
<li style={{marginLeft: 'auto'}}>Menu1</li>
<li>Menu2</li>
<li>Menu3</li>
</ul>
);
};
3 changes: 2 additions & 1 deletion packages/midway-web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"declaration": true,
"outDir": "dist",
"lib": ["es2017", "dom"],
"sourceMap": true
"sourceMap": true,
"jsx": "react"
},
"exclude": [
"dist",
Expand Down

0 comments on commit ee7ab7c

Please sign in to comment.