Skip to content

Commit 8381cd8

Browse files
committed
added typescript support via SWC
1 parent d4f1d0b commit 8381cd8

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

template/.ts.swcrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://swc.rs/schema.json",
3+
"jsc": {
4+
"parser": {
5+
"syntax": "typescript"
6+
}
7+
},
8+
"module": {
9+
"type": "commonjs"
10+
},
11+
"env": {
12+
"targets": "node 22"
13+
},
14+
"isModule": false
15+
}

template/Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
FROM python:3.10.14
22

33
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-install-recommends \
4-
build-essential curl git util-linux jq sudo nodejs npm fonts-noto-cjk
4+
build-essential curl git util-linux jq sudo fonts-noto-cjk
5+
6+
# Install Node.js 20.x from NodeSource
7+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
8+
apt-get install -y nodejs
59

610
ENV PIP_DEFAULT_TIMEOUT=100 \
711
PIP_DISABLE_PIP_VERSION_CHECK=1 \
@@ -24,10 +28,13 @@ RUN R -e "install.packages('IRkernel', repos='https://cloud.r-project.org')"
2428
RUN R -e "IRkernel::installspec(user = FALSE, name = 'r', displayname = 'R')"
2529

2630
# Javascript Kernel
27-
RUN npm install -g node-gyp
2831
RUN npm install -g --unsafe-perm ijavascript
2932
RUN ijsinstall --install=global
3033

34+
## TypeScript support
35+
RUN npm install -g @swc/cli @swc/core
36+
COPY .ts.swcrc /root/.ts.swcrc
37+
3138
# Deno Kernel
3239
COPY --from=denoland/deno:bin-2.0.4 /deno /usr/bin/deno
3340
RUN chmod +x /usr/bin/deno

template/server/contexts.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
logger = logging.Logger(__name__)
1313

14+
def get_kernel_for_language(language: str) -> str:
15+
if language == "typescript":
16+
return "javascript"
17+
18+
return language
1419

1520
def normalize_language(language: Optional[str]) -> str:
1621
if not language:
@@ -21,13 +26,16 @@ def normalize_language(language: Optional[str]) -> str:
2126
if language == "js":
2227
return "javascript"
2328

29+
if language == "ts":
30+
return "typescript"
31+
2432
return language
2533

2634

2735
async def create_context(client, websockets: dict, language: str, cwd: str) -> Context:
2836
data = {
2937
"path": str(uuid.uuid4()),
30-
"kernel": {"name": language},
38+
"kernel": {"name": get_kernel_for_language(language)},
3139
"type": "notebook",
3240
"name": str(uuid.uuid4()),
3341
}

template/server/messaging.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import uuid
55
import asyncio
6+
import subprocess
67

78
from asyncio import Queue
89
from envs import get_envs
@@ -27,6 +28,7 @@
2728

2829
logger = logging.getLogger(__name__)
2930

31+
compile_typescript_cmd = "/usr/lib/node_modules/@swc/cli/bin/swc.js --config-file /root/.ts.swcrc --filename index.ts"
3032

3133
class Execution:
3234
def __init__(self, in_background: bool = False):
@@ -199,6 +201,23 @@ async def execute(
199201
+ code
200202
)
201203

204+
if self.language == "typescript":
205+
logger.info("Compiling TypeScript: %s", code)
206+
207+
# call swc to compile the typescript code
208+
compile_result = subprocess.run(compile_typescript_cmd.split(), input=code.encode(), capture_output=True)
209+
210+
if compile_result.returncode != 0:
211+
logger.error("Error during TypeScript compilation: %s", compile_result.stderr.decode())
212+
yield Error(
213+
name="TypeScriptCompilerError",
214+
value=compile_result.stderr.decode(),
215+
traceback="",
216+
)
217+
return
218+
219+
code = compile_result.stdout.decode()
220+
202221
logger.info(code)
203222
request = self._get_execute_request(message_id, code, False)
204223

template/test.Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ COPY --from=eclipse-temurin:11-jdk $JAVA_HOME $JAVA_HOME
55
ENV PATH="${JAVA_HOME}/bin:${PATH}"
66

77
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-install-recommends \
8-
build-essential curl git util-linux jq sudo nodejs npm fonts-noto-cjk
8+
build-essential curl git util-linux jq sudo fonts-noto-cjk
9+
10+
# Install Node.js 20.x from NodeSource
11+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
12+
apt-get install -y nodejs
913

1014
ENV PIP_DEFAULT_TIMEOUT=100 \
1115
PIP_DISABLE_PIP_VERSION_CHECK=1 \
@@ -19,10 +23,13 @@ COPY ./template/requirements.txt requirements.txt
1923
RUN pip install --no-cache-dir -r requirements.txt && ipython kernel install --name "python3" --user
2024

2125
# Javascript Kernel
22-
RUN npm install -g node-gyp
2326
RUN npm install -g --unsafe-perm ijavascript
2427
RUN ijsinstall --install=global
2528

29+
## TypeScript support
30+
RUN npm install -g @swc/cli @swc/core
31+
COPY ./template/.ts.swcrc /root/.ts.swcrc
32+
2633
# Deno Kernel
2734
COPY --from=denoland/deno:bin-2.0.4 /deno /usr/bin/deno
2835
RUN chmod +x /usr/bin/deno

0 commit comments

Comments
 (0)