Skip to content

Commit c7ca6a9

Browse files
committed
feat: add monaco + app layout + loader + tailwind
1 parent 3981324 commit c7ca6a9

File tree

18 files changed

+1800
-99
lines changed

18 files changed

+1800
-99
lines changed

.esbuild/copyPlugin.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from "node:path";
2+
import fs from "node:fs";
3+
import { fileURLToPath } from "node:url";
4+
import * as glob from "glob";
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
8+
export default function copyPlugin({ src, dest, patterns }) {
9+
return {
10+
name: "copy",
11+
setup(build) {
12+
build.onEnd(() => {
13+
const sourceDir = path.resolve(__dirname, src);
14+
const destDir = path.resolve(__dirname, dest);
15+
patterns.forEach((pattern) => {
16+
const srcFiles = glob.sync(path.resolve(sourceDir, pattern));
17+
srcFiles.forEach((srcFile) => {
18+
const relSrcFile = path.relative(sourceDir, srcFile);
19+
const destFile = path.resolve(destDir, relSrcFile);
20+
console.log(`Copying ${srcFile} to ${destFile}`);
21+
fs.mkdirSync(path.dirname(destFile), { recursive: true });
22+
fs.copyFileSync(srcFile, destFile);
23+
});
24+
});
25+
});
26+
},
27+
};
28+
}

bin/build.js

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

bin/copyPlugin.js

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

bin/serve.js

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

esbuild.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./.esbuild/wasmPlugin.js";
4+
import copyPlugin from "./.esbuild/copyPlugin.js";
5+
6+
const entryPoint = url.fileURLToPath(new URL("./src/index", import.meta.url));
7+
const outdir = url.fileURLToPath(new URL("./dist", import.meta.url));
8+
const srcDir = url.fileURLToPath(new URL("./src", import.meta.url));
9+
10+
const { metafile } = await esbuild.build({
11+
bundle: true,
12+
entryPoints: [entryPoint],
13+
format: "esm",
14+
metafile: true,
15+
minify: true,
16+
outdir,
17+
plugins: [
18+
wasmPlugin,
19+
copyPlugin({
20+
src: srcDir,
21+
dest: outdir,
22+
patterns: ["*.html", "assets/**/*"],
23+
}),
24+
],
25+
sourcemap: true,
26+
splitting: true,
27+
target: "esnext",
28+
loader: {
29+
".ttf": "file",
30+
},
31+
});
32+
33+
const analysis = await esbuild.analyzeMetafile(metafile);
34+
console.log(analysis);

esbuild.serve.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./.esbuild/wasmPlugin.js";
4+
import copyPlugin from "./.esbuild/copyPlugin.js";
5+
6+
const entryPoint = url.fileURLToPath(new URL("./src/index", import.meta.url));
7+
const outdir = url.fileURLToPath(new URL("./dist", import.meta.url));
8+
const srcDir = url.fileURLToPath(new URL("./src", import.meta.url));
9+
10+
const ctx = await esbuild.context({
11+
bundle: true,
12+
entryPoints: [entryPoint],
13+
format: "esm",
14+
outdir,
15+
plugins: [
16+
wasmPlugin,
17+
copyPlugin({
18+
src: srcDir,
19+
dest: outdir,
20+
patterns: ["*.html", "assets/**/*"],
21+
}),
22+
],
23+
sourcemap: true,
24+
splitting: true,
25+
target: "esnext",
26+
loader: {
27+
".ttf": "file",
28+
},
29+
});
30+
31+
const { host, port } = await ctx.serve({ servedir: outdir });
32+
console.log(`Listening at ${host}:${port}`);

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
"license": "MIT",
33
"type": "module",
44
"scripts": {
5-
"build": "node bin/build.js",
6-
"serve": "node bin/serve.js"
5+
"build:js": "node ./esbuild.config.js",
6+
"build:css": "npx tailwindcss -i ./src/app.css -o ./dist/app.css",
7+
"build": "npm-run-all --parallel build:*",
8+
"watch:js": "node ./esbuild.serve.js",
9+
"watch:css": "yarn build:css --watch",
10+
"serve": "npm-run-all --parallel watch:*"
711
},
812
"dependencies": {
913
"@bjorn3/browser_wasi_shim": "^0.2.18",
10-
"@ruby/wasm-wasi": "^2.4.1"
14+
"@ruby/wasm-wasi": "^2.4.1",
15+
"monaco-editor": "^0.45.0"
1116
},
1217
"devDependencies": {
13-
"esbuild": "^0.17.10"
18+
"esbuild": "^0.17.10",
19+
"npm-run-all": "^4.1.5",
20+
"tailwindcss": "^3.4.1"
1421
}
1522
}

src/app.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

src/app.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const DEFAULT_SOURCE = `# Welcome to Ruby Next playground!
2+
# Here you can write Ruby code and see how it will be transformed by Ruby Next.
3+
# You can also execute it and see the result.
4+
5+
greet = proc do
6+
case it
7+
in hello: hello if hello =~ /human/i
8+
'🙂'
9+
in hello: 'martian'
10+
'👽'
11+
end
12+
end
13+
14+
puts greet.call(hello: 'martian')
15+
`;
16+
17+
const DEFAULT_PREVIEW = `# Here you will see the transpiled source code.`;
18+
19+
export default class App {
20+
constructor(el, vm, monaco) {
21+
this.el = el;
22+
this.vm = vm;
23+
this.monaco = monaco;
24+
}
25+
26+
bootstrap() {
27+
this.currentEditor = this.initEditor(
28+
this.el.querySelector('[target="editor"]'),
29+
DEFAULT_SOURCE
30+
);
31+
32+
this.previewEditor = this.initEditor(
33+
this.el.querySelector('[target="preview"]'),
34+
DEFAULT_PREVIEW,
35+
{
36+
readOnly: true,
37+
}
38+
);
39+
40+
this.el
41+
.querySelector('[target="transpile-btn"]')
42+
.addEventListener("click", () => {
43+
const code = this.currentEditor.getValue();
44+
const result = this.vm
45+
.eval("RubyNext::Language.transform(%q(" + code + "), using: false)")
46+
.toString();
47+
48+
this.previewEditor.setValue(result);
49+
});
50+
}
51+
52+
initEditor(target, value, opts = {}) {
53+
return this.monaco.editor.create(target, {
54+
value,
55+
language: "ruby",
56+
theme: "vs-dark",
57+
automaticLayout: true,
58+
minimap: {
59+
enabled: false,
60+
},
61+
...opts,
62+
});
63+
}
64+
}

0 commit comments

Comments
 (0)