Skip to content

Commit 919a47d

Browse files
author
Mohsen Azimi
committed
WIP: make TS compiler work in browser
1 parent a818aca commit 919a47d

File tree

10 files changed

+3685
-77
lines changed

10 files changed

+3685
-77
lines changed

docs/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/editor.tsx

Lines changed: 0 additions & 67 deletions
This file was deleted.

editor/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Web Editor
2+
3+
To start the development server
4+
5+
```
6+
yarn
7+
yarn start
8+
```

editor/editor.tsx

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import * as React from 'react';
2+
import { render } from 'react-dom';
3+
import MonacoEditor from 'react-monaco-editor';
4+
import * as ts from 'typescript';
5+
6+
import { allTransforms } from '../src'
7+
import {react} from './react';
8+
9+
const defaultInput = `
10+
import * as React from 'react';
11+
12+
export default class MyComponent extends React.Component {
13+
render() {
14+
return <div />;
15+
}
16+
}
17+
`;
18+
19+
interface AppState {
20+
input: string;
21+
output: string;
22+
windowSize: { width: number; height: number; };
23+
}
24+
25+
class App extends React.Component<{}, AppState> {
26+
static toolbarHeight = 50;
27+
state = { input: defaultInput, output: '', windowSize: { width: 0, height: 0 } }
28+
compilerOptions: ts.CompilerOptions = {
29+
target: ts.ScriptTarget.ES2017,
30+
module: ts.ModuleKind.ES2015,
31+
}
32+
host: ts.CompilerHost = {
33+
getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => {
34+
console.log('getSourceFile', fileName)
35+
return ts.createSourceFile(fileName, this.state.input, languageVersion);
36+
},
37+
writeFile: (fileName: string, data: string, writeByteOrderMark: boolean, ) => undefined,
38+
getDirectories: () => ['/'],
39+
getCanonicalFileName: () => 'main.tsx',
40+
useCaseSensitiveFileNames: () => true,
41+
getNewLine: () => '\n',
42+
getCurrentDirectory: () => '/',
43+
getDefaultLibFileName: () => 'lib.ts',
44+
fileExists: (fileName: string) => {
45+
console.log('fileExist', fileName)
46+
return fileName === 'main.tsx';
47+
},
48+
readFile: (fileName: string) => {
49+
console.log('readFile', fileName)
50+
return this.state.input
51+
},
52+
}
53+
getText = (start: number, end: number) => {
54+
console.log('getText')
55+
return this.state.input.substring(start, end);
56+
}
57+
getLength = () => this.state.input.length;
58+
setWindowSize = () => {
59+
this.setState({
60+
windowSize: { width: window.innerWidth, height: innerHeight }
61+
});
62+
}
63+
componentWillMount() {
64+
this.setWindowSize();
65+
this.setState({ output: this.compile(this.state.input) });
66+
window.addEventListener('resize', this.setWindowSize);
67+
}
68+
editorDidMount = (editor: monaco.editor.ICodeEditor, m: typeof monaco) => {
69+
editor.focus();
70+
}
71+
onChange = (newValue: string, e: monaco.editor.IModelContentChangedEvent2) => {
72+
this.setState({input: newValue});
73+
try {
74+
const output = this.compile(this.state.input);
75+
this.setState({ output })
76+
} catch (error) {
77+
console.error(error);
78+
}
79+
}
80+
compile = (input: string) => {
81+
const sourceFile = ts.createSourceFile('main.tsx', this.state.input, ts.ScriptTarget.ES2017);
82+
const program = ts.createProgram(['main.tsx'], this.compilerOptions, this.host);
83+
const typeChecker = program.getTypeChecker();
84+
const result = ts.transform([sourceFile], allTransforms.map(f => f(typeChecker)));
85+
const printer = ts.createPrinter();
86+
return printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFile);
87+
}
88+
render() {
89+
const { input, output, windowSize } = this.state;
90+
return (
91+
<div>
92+
<Toolbar height={App.toolbarHeight} />
93+
<div style={{ display: 'flex' }}>
94+
<MonacoEditor
95+
width={windowSize.width / 2}
96+
height={windowSize.height - App.toolbarHeight}
97+
language='javascript'
98+
value={input}
99+
options={{}}
100+
onChange={this.onChange}
101+
editorDidMount={this.editorDidMount}
102+
/>
103+
<MonacoEditor
104+
width={windowSize.width / 2}
105+
height={windowSize.height - App.toolbarHeight}
106+
language='typescript'
107+
value={output}
108+
options={{ readOnly: true }}
109+
/>
110+
</div>
111+
</div>
112+
);
113+
}
114+
}
115+
116+
const Toolbar = ({ height }: { height: number }) => (
117+
<div style={{ height: `${height}px` }}>
118+
Convert React code to TypeScript
119+
</div>
120+
)
121+
122+
render(
123+
<App />,
124+
document.getElementById('root')
125+
);
File renamed without changes.

docs/package.json renamed to editor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"react-monaco-editor": "^0.8.1",
2222
"ts-loader": "^2.3.0",
2323
"ts-node": "^3.2.0",
24-
"typescript": "^2.4.1",
24+
"typescript": "2.4.1",
2525
"webpack": "^3.3.0",
2626
"webpack-dev-server": "^2.5.1"
2727
}

0 commit comments

Comments
 (0)