Skip to content

Commit c7466c6

Browse files
authored
Merge pull request #56 from dabbott/compile-ts
Compile ts using tsc
2 parents cdd411d + c6a82ac commit c7466c6

File tree

14 files changed

+679
-263
lines changed

14 files changed

+679
-263
lines changed

src/components/workspace/Status.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const baseTextStyle = prefix({
1111
overflow: 'hidden',
1212
whiteSpace: 'nowrap',
1313
transition: 'color 0.2s',
14+
lineHeight: '1.2',
1415
})
1516

1617
const styles = prefixObject({

src/components/workspace/Workspace.tsx

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import babelRequest, { BabelResponse } from '../../utils/BabelRequest'
1616
import { PaneOptions, containsPane } from '../../utils/Panes'
1717
import { prefixObject, rowStyle } from '../../utils/Styles'
1818
import { Tab } from '../../utils/Tab'
19-
import typeScriptRequest from '../../utils/TypeScriptRequest'
19+
import typeScriptRequest, {
20+
TypeScriptCompileRequest,
21+
} from '../../utils/TypeScriptRequest'
2022
import { Props as EditorProps } from './Editor'
2123
import ConsolePane from './panes/ConsolePane'
2224
import EditorPane from './panes/EditorPane'
@@ -30,18 +32,20 @@ import {
3032
WorkspaceStep,
3133
UserInterfaceStrings,
3234
CompilerOptions,
35+
TypeScriptOptions,
3336
} from '../../utils/options'
3437
import { WorkspaceDiff } from './App'
3538
import useRerenderEffect from '../../hooks/useRerenderEffect'
3639
import type { ExternalModule } from '../player/VendorComponents'
40+
import { basename, extname } from '../../utils/path'
3741

3842
const {
3943
reducer,
4044
actionCreators: {
4145
compiled,
4246
transpiled,
43-
babelCode,
44-
babelError,
47+
compilerSuccess,
48+
compilerError,
4549
codeChange,
4650
openEditorTab,
4751
playerRun,
@@ -77,12 +81,6 @@ export interface PlaygroundOptions {
7781
expandLevel?: number
7882
}
7983

80-
export interface TypeScriptOptions {
81-
enabled?: false
82-
libs?: string[]
83-
types?: string[]
84-
}
85-
8684
export interface ExternalStyles {
8785
consolePane?: CSSProperties
8886
consoleRow?: CSSProperties
@@ -385,37 +383,64 @@ export default function Workspace(props: Props) {
385383
[]
386384
)
387385

388-
const updateStatus = (filename: string, babelMessage: BabelResponse) => {
389-
switch (babelMessage.type) {
390-
case 'code':
391-
dispatch(babelCode(filename))
392-
break
393-
case 'error':
394-
dispatch(babelError(filename, babelMessage.error.message))
395-
break
386+
const updateStatus = (filename: string, errorMessage?: string) => {
387+
if (errorMessage) {
388+
dispatch(compilerError(filename, errorMessage))
389+
} else {
390+
dispatch(compilerSuccess(filename))
396391
}
397392
}
398393

399-
const compilerRequest = (filename: string, code: string) => {
400-
if (props.compilerOptions.type === 'none') {
401-
dispatch(compiled(filename, code))
402-
} else {
403-
babelRequest({
394+
const runBabel = useCallback((filename: string, code: string) => {
395+
babelRequest({
396+
filename,
397+
code,
398+
options: {
399+
retainLines: true,
400+
maxLoopIterations: props.compilerOptions.maxLoopIterations ?? 0,
401+
instrumentExpressionStatements:
402+
props.playgroundOptions.instrumentExpressionStatements,
403+
},
404+
}).then((response: BabelResponse) => {
405+
updateStatus(
404406
filename,
405-
code,
406-
options: {
407-
retainLines: true,
408-
maxLoopIterations: props.compilerOptions.maxLoopIterations ?? 0,
409-
instrumentExpressionStatements:
410-
props.playgroundOptions.instrumentExpressionStatements,
411-
},
412-
}).then((response: BabelResponse) => {
413-
updateStatus(filename, response)
414-
415-
if (response.type === 'code') {
416-
dispatch(compiled(response.filename, response.code))
417-
}
418-
})
407+
response.type === 'error' ? response.error.message : undefined
408+
)
409+
410+
if (response.type === 'code') {
411+
dispatch(compiled(response.filename, response.code))
412+
}
413+
})
414+
}, [])
415+
416+
// We currently ignore the output from tsc, instead transpiling the code
417+
// again through babel. This is so the console.log/playground code transformations
418+
// get applied. It could be worth rewriting them directly in tsc at some point.
419+
const runTsc = useCallback((filename: string, code: string) => {
420+
typeScriptRequest({
421+
type: 'compile',
422+
filename,
423+
}).then((response) => {
424+
if (response.type === 'error') {
425+
updateStatus(filename, response.error.message)
426+
return
427+
}
428+
429+
runBabel(filename, code)
430+
})
431+
}, [])
432+
433+
const compilerRequest = (filename: string, code: string) => {
434+
switch (props.compilerOptions.type) {
435+
case 'none':
436+
dispatch(compiled(filename, code))
437+
break
438+
case 'tsc':
439+
runTsc(filename, code)
440+
break
441+
case 'babel':
442+
default:
443+
runBabel(filename, code)
419444
}
420445
}
421446

@@ -440,26 +465,26 @@ export default function Workspace(props: Props) {
440465
}
441466

442467
if (typeof navigator !== 'undefined') {
468+
if (typescriptOptions.enabled) {
469+
typeScriptRequest({
470+
type: 'init',
471+
libs: typescriptOptions.libs || [],
472+
types: typescriptOptions.types || [],
473+
compilerOptions: typescriptOptions.compilerOptions || {},
474+
})
475+
476+
typeScriptRequest({
477+
type: 'files',
478+
files,
479+
})
480+
}
481+
443482
// Cache and compile each file
444483
Object.keys(files).forEach((filename) => {
445484
const code = files[filename]
446485

447486
state.codeCache[filename] = code
448487

449-
if (typescriptOptions.enabled) {
450-
typeScriptRequest({
451-
type: 'libs',
452-
libs: typescriptOptions.libs || [],
453-
types: typescriptOptions.types || [],
454-
})
455-
456-
typeScriptRequest({
457-
type: 'file',
458-
filename,
459-
code,
460-
})
461-
}
462-
463488
if (playerVisible) {
464489
compilerRequest(filename, code)
465490
}
@@ -477,9 +502,8 @@ export default function Workspace(props: Props) {
477502
(code: string) => {
478503
if (typescriptOptions.enabled) {
479504
typeScriptRequest({
480-
type: 'file',
481-
filename: state.activeFile,
482-
code,
505+
type: 'files',
506+
files: { [state.activeFile]: code },
483507
})
484508
}
485509

src/components/workspace/panes/EditorPane.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ import Header from '../Header'
1616
import Overlay from '../Overlay'
1717
import Status from '../Status'
1818
import Tabs from '../Tabs'
19-
import {
20-
PlaygroundOptions,
21-
PublicError,
22-
TypeScriptOptions,
23-
ExternalStyles,
24-
} from '../Workspace'
19+
import { PlaygroundOptions, PublicError, ExternalStyles } from '../Workspace'
2520
import type { WorkspaceDiff } from '../App'
26-
import { UserInterfaceStrings } from '../../../utils/options'
21+
import { TypeScriptOptions, UserInterfaceStrings } from '../../../utils/options'
2722

2823
const styles = prefixObject({
2924
editorPane: columnStyle,

src/reducers/workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export const actionCreators = {
2828
filename,
2929
code,
3030
}),
31-
babelCode: (filename: string) => ({
31+
compilerSuccess: (filename: string) => ({
3232
type: types.BABEL_CODE,
3333
filename,
3434
}),
35-
babelError: (filename: string, message: string) => ({
35+
compilerError: (filename: string, message: string) => ({
3636
type: types.BABEL_ERROR,
3737
filename,
3838
message,

src/types/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare var __webpack_public_path__: string

src/typescript-worker.js

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

0 commit comments

Comments
 (0)