Skip to content

Commit 7cde6dd

Browse files
committed
fix: fix esbulid-loader bug and monaco resize bug
1 parent 051bff9 commit 7cde6dd

File tree

7 files changed

+785
-79
lines changed

7 files changed

+785
-79
lines changed

packages/hedgehog-core/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
],
1010
"scripts": {
1111
"build": "tsc -p tsconfig.json",
12+
"esbuild": "webpack ./webpack.config.js",
1213
"watch": "tsc -p tsconfig.json --watch --preserveWatchOutput",
1314
"prettier": "prettier --single-quote --write '**/*.{js,json,md}'",
1415
"lint": "eslint src --fix --ext .ts,.tsx,.js,.jsx"
@@ -21,15 +22,16 @@
2122
"dependencies": {
2223
"@babel/core": "^7.10.5",
2324
"@babel/preset-env": "^7.10.4",
24-
"@babel/preset-react": "^7.12.10",
2525
"@babel/preset-typescript": "^7.10.4",
2626
"@babel/standalone": "^7.10.5",
27-
"@tensorflow/tfjs": "^2.7.0",
2827
"babel-template": "^6.26.0",
28+
"clean-webpack-plugin": "^3.0.0",
29+
"esbuild-loader": "^2.9.1",
2930
"gpu.js": "^2.9.5",
3031
"mathjs": "^7.1.0",
3132
"nerdamer": "^1.1.5",
32-
"sync-fetch": "^0.3.0"
33+
"webpack": "^5.21.2",
34+
"webpack-cli": "^4.5.0"
3335
},
3436
"devDependencies": {
3537
"@babel/types": "^7.10.5",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const path = require("path");
2+
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
3+
const { ESBuildPlugin } = require("esbuild-loader");
4+
5+
module.exports = {
6+
resolve: {
7+
extensions: [".js", ".ts", ".json"],
8+
},
9+
devtool: "source-map",
10+
mode: "production",
11+
entry: "./src/index.ts",
12+
output: {
13+
filename: "[name].js",
14+
path: path.resolve(__dirname, "dist"),
15+
libraryTarget: "commonjs",
16+
},
17+
module: {
18+
rules: [
19+
{
20+
test: /\.tsx?$/,
21+
use: [
22+
{
23+
loader: "tslint-loader",
24+
options: {
25+
configFile: path.resolve(__dirname, "./tslint.json"),
26+
},
27+
},
28+
],
29+
},
30+
{
31+
test: /\.js$/,
32+
loader: "esbuild-loader",
33+
options: {
34+
target: "es2015",
35+
},
36+
exclude: /node_modules/,
37+
},
38+
{
39+
test: /\.ts$/,
40+
loader: "esbuild-loader",
41+
options: {
42+
target: "es2015",
43+
tsconfigRaw: path.resolve(__dirname, "./tsconfig.json"),
44+
},
45+
exclude: /node_modules/,
46+
},
47+
],
48+
},
49+
plugins: [new CleanWebpackPlugin(), new ESBuildPlugin()],
50+
};

packages/hedgehog-lab/config-overrides.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@ const { ESBuildPlugin } = require("esbuild-loader");
1111
module.exports = (config, env) => {
1212
const prod = config.mode === "production";
1313
const babelLoader = getBabelLoader(config);
14-
14+
console.log(prod, config.mode);
1515
return override(
16-
addWebpackModuleRule({
17-
test: /.tsx?$/,
18-
use: [
19-
{
20-
loader: "esbuild-loader",
21-
options: { loader: "tsx", target: "es2015" },
22-
},
23-
],
24-
}),
16+
!prod &&
17+
addWebpackModuleRule({
18+
test: /.tsx?$/,
19+
use: [
20+
{
21+
loader: "esbuild-loader",
22+
options: { loader: "tsx", target: "es2015" },
23+
},
24+
],
25+
}),
2526

26-
addWebpackModuleRule({
27-
test: /.jsx?$/,
28-
use: [
29-
{
30-
loader: "esbuild-loader",
31-
options: { loader: "jsx", target: "es2015" },
32-
},
33-
],
34-
}),
27+
!prod &&
28+
addWebpackModuleRule({
29+
test: /.jsx?$/,
30+
use: [
31+
{
32+
loader: "esbuild-loader",
33+
options: { loader: "jsx", target: "es2015" },
34+
},
35+
],
36+
}),
3537
// You can choose to just use worker-loader! instead if you want
3638
addWebpackModuleRule({
3739
test: /\.worker\.[jt]sx?$/,
@@ -40,7 +42,7 @@ module.exports = (config, env) => {
4042
{ loader: babelLoader.loader, options: babelLoader.options },
4143
],
4244
}),
43-
addWebpackPlugin(new ESBuildPlugin()),
45+
!prod && addWebpackPlugin(new ESBuildPlugin()),
4446
!prod && addBabelPlugin("react-refresh/babel"),
4547
!prod && addWebpackPlugin(new ReactRefreshPlugin())
4648
)(config, env);

packages/hedgehog-lab/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
"comlink": "^4.3.0",
1515
"esbuild-loader": "^2.9.1",
1616
"ky": "^0.23.0",
17-
"monaco-editor": "^0.20.0",
17+
"monaco-editor": "^0.23.0",
1818
"plotly.js": "^1.54.6",
1919
"qs": "^6.9.4",
2020
"react": "^16.13.1",
2121
"react-codemirror2": "^7.2.1",
2222
"react-dom": "^16.13.1",
2323
"react-markdown": "^4.3.1",
2424
"react-mathjax": "^1.0.1",
25+
"react-monaco-editor": "^0.43.0",
2526
"react-plotly.js": "^2.4.0",
2627
"react-query": "^2.5.6",
28+
"react-resize-detector": "^6.6.4",
2729
"worker-loader": "^2.0.0"
2830
},
2931
"devDependencies": {

packages/hedgehog-lab/src/components/SideBar/SaveButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const useStyles = makeStyles((theme: Theme) =>
1414
width: '50%',
1515
fontSize: '0.875rem',
1616
fontWeight: 500,
17-
color: 'white',
17+
color: 'white'
1818
}
1919
})
2020
);

packages/hedgehog-lab/src/components/YourCode/YourCode.tsx

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React, { Dispatch, SetStateAction } from 'react';
2-
import { Button, Card, CardContent, CardHeader } from '@material-ui/core';
3-
import { ControlledEditor, ControlledEditorOnChange } from '@monaco-editor/react';
4-
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
5-
import { queryCache } from 'react-query';
1+
import React, { useState, Dispatch, SetStateAction } from "react";
2+
import { Button, Card, CardContent, CardHeader } from "@material-ui/core";
3+
import MonacoEditor, { EditorDidMount } from "react-monaco-editor";
4+
import * as monacoEditor from "monaco-editor/esm/vs/editor/editor.api";
5+
import ResizeObserver from "react-resize-detector";
6+
import { queryCache } from "react-query";
67

7-
const COMPILE_AND_RUN_BUTTON_ID = 'compile-and-run-button-id';
8+
const COMPILE_AND_RUN_BUTTON_ID = "compile-and-run-button-id";
89

910
interface YourCodeProps {
1011
handleCompileAndRun: (event: React.MouseEvent) => void;
@@ -15,33 +16,41 @@ interface YourCodeProps {
1516

1617
const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
1718
const { handleCompileAndRun, loading, setSource, source } = props;
19+
const [
20+
editor,
21+
setEditor,
22+
] = useState<monacoEditor.editor.IStandaloneCodeEditor | null>(null);
23+
const [monaco, setMonaco] = useState<typeof monacoEditor | null>(null);
1824

19-
const handleUploadSource: ControlledEditorOnChange = (e, v) => {
25+
const handleUploadSource = (v: string) => {
2026
setSource(v as string);
2127
};
2228

2329
const options = {
24-
wordWrap: 'on' as const,
25-
scrollBeyondLastLine: false
30+
wordWrap: "on" as const,
31+
scrollBeyondLastLine: false,
2632
};
2733

28-
const handleEditorDidMount = (
29-
_: () => string,
30-
editor: monacoEditor.editor.IStandaloneCodeEditor
34+
const EditorDidMountHandle = (
35+
editor: monacoEditor.editor.IStandaloneCodeEditor,
36+
monaco: typeof monacoEditor
3137
) => {
3238
editor.addAction({
3339
id: COMPILE_AND_RUN_BUTTON_ID,
34-
label: 'compile-and-run-butt-label',
40+
label: "compile-and-run-butt-label",
3541
keybindings: [2051], // Monaco.KeyMod.CtrlCmd | Monaco.KeyCode.Enter == 2051
3642
run: () => {
3743
document.getElementById(COMPILE_AND_RUN_BUTTON_ID)?.click();
38-
}
44+
},
3945
});
46+
47+
setEditor(editor);
48+
setMonaco(monaco);
4049
};
4150

4251
return (
43-
<div style={{ height: '100%' }}>
44-
<Card className={'your-code-card'} style={{ height: '100%' }}>
52+
<div style={{ height: "100%" }}>
53+
<Card className={"your-code-card"} style={{ height: "100%" }}>
4554
<CardHeader
4655
action={
4756
<div className="run-button">
@@ -50,51 +59,61 @@ const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
5059
variant="contained"
5160
color="secondary"
5261
style={{
53-
textTransform: 'none',
62+
textTransform: "none",
63+
width: 140,
5464
}}
55-
size = "small"
5665
onClick={() => {
5766
// stop the web-worker
58-
queryCache.cancelQueries(['compiler']);
67+
queryCache.cancelQueries(["compiler"]);
5968
// set result to initial state
60-
queryCache.setQueryData(['compiler', source], (data) => ({
69+
queryCache.setQueryData(["compiler", source], (data) => ({
6170
outputItem: [],
62-
outputString: ''
71+
outputString: "",
6372
}));
64-
}}>
73+
}}
74+
>
6575
Stop
6676
</Button>
6777
) : (
6878
<Button
6979
id={COMPILE_AND_RUN_BUTTON_ID}
7080
variant="contained"
7181
color="primary"
72-
size = "small"
7382
onClick={(e) => handleCompileAndRun(e)}
7483
style={{
75-
textTransform: 'none'
76-
}}>
77-
Run
84+
textTransform: "none",
85+
}}
86+
>
87+
Compile and run
7888
</Button>
7989
)}
8090
</div>
8191
}
92+
title="Your code:"
8293
/>
8394

8495
<CardContent>
85-
<div
86-
style={{
87-
height: 'calc(100vh - 174px)'
88-
}}>
89-
<ControlledEditor
90-
language="javascript"
91-
value={source}
92-
onChange={handleUploadSource}
93-
options={options}
94-
editorDidMount={handleEditorDidMount}
95-
96-
/>
97-
</div>
96+
<ResizeObserver
97+
onResize={(width, height) => {
98+
if (editor) {
99+
editor.layout();
100+
}
101+
}}
102+
>
103+
<div
104+
style={{
105+
height: "calc(100vh - 174px)",
106+
}}
107+
>
108+
<MonacoEditor
109+
language="javascript"
110+
value={source}
111+
onChange={handleUploadSource}
112+
options={options}
113+
editorDidMount={EditorDidMountHandle}
114+
/>
115+
</div>
116+
</ResizeObserver>
98117
</CardContent>
99118
</Card>
100119
</div>

0 commit comments

Comments
 (0)