Skip to content

Commit 259d97c

Browse files
feat: #9 initial editor experience (#18)
1 parent ac1de47 commit 259d97c

17 files changed

Lines changed: 474 additions & 164 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*DS_Store
22
*.log
3+
.vscode/
34
node_modules/
45
public/
56
.greenwood/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { ResourcePlugin } from "@greenwood/cli";
2+
3+
// https://github.com/microsoft/monaco-editor/issues/886
4+
// have to strip out CSS `import` statements from the ESM build of Monaco Editor,
5+
// otherwise the browser will attempt to load them as ESM and fail
6+
class StripCssImportsResource {
7+
extensions: string[];
8+
contentType: string;
9+
10+
constructor() {
11+
this.extensions = ["js"];
12+
this.contentType = "application/javascript";
13+
}
14+
15+
async shouldIntercept(url: URL) {
16+
return url.pathname.includes("node_modules/monaco-editor") && url.pathname.endsWith(".js");
17+
}
18+
19+
async intercept(url: URL, request: Request, response: Response) {
20+
const contents = await response.text();
21+
const stripped = contents.replace(/^\s*import\s+[^;]*['"]([^'"]+\.css)['"]\s*;?\s*$/gm, "");
22+
23+
return new Response(stripped, {
24+
headers: {
25+
"Content-Type": this.contentType,
26+
},
27+
});
28+
}
29+
}
30+
31+
export function monacoEditorEsmShimPlugin(): ResourcePlugin {
32+
return {
33+
type: "resource",
34+
name: "monaco-editor-strip-css-imports-plugin",
35+
provider: () => new StripCssImportsResource(),
36+
};
37+
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as rollup from "rollup";
2-
// TODO: depend on these modules first party
2+
// TODO: depend on these modules first party?
33
import { nodeResolve } from "@rollup/plugin-node-resolve";
44
import commonjs from "@rollup/plugin-commonjs";
55
import type { ResourcePlugin } from "@greenwood/cli";
@@ -52,11 +52,23 @@ class ReplBundlerResource {
5252
nodeResolve(),
5353
commonjs(),
5454
],
55+
onLog(level, log) {
56+
// silence circular dependency warnings from sucrase
57+
if (
58+
log.pluginCode === "CIRCULAR_DEPENDENCY" &&
59+
log.message.includes("node_modules/sucrase")
60+
) {
61+
return;
62+
}
63+
},
5564
});
5665
const { output } = await bundle.generate({
5766
format: "esm",
5867
});
5968

69+
// Create a buffer from the code string to avoid body consumption issues
70+
// const codeBuffer = Buffer.from(output[0].code, 'utf-8');
71+
6072
return new Response(output[0].code, {
6173
headers: {
6274
"Content-Type": "text/javascript",

greenwood.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
22
import { greenwoodPluginCssModules } from "@greenwood/plugin-css-modules";
33
import { greenwoodPluginImportJsx } from "@greenwood/plugin-import-jsx";
4-
import { replBundlerResourcePlugin } from "./repl-bundler-plugin.ts";
4+
import { replBundlerResourcePlugin } from "./greenwood-plugins/repl-bundler-plugin.ts";
5+
import { monacoEditorEsmShimPlugin } from "./greenwood-plugins/monaco-editor-esm-shim-plugin.ts";
56
import type { Config } from "@greenwood/cli";
67

78
const config: Config = {
@@ -11,6 +12,7 @@ const config: Config = {
1112
greenwoodPluginImportRaw(),
1213
greenwoodPluginImportJsx(),
1314
replBundlerResourcePlugin(),
15+
monacoEditorEsmShimPlugin(),
1416
],
1517
};
1618

0 commit comments

Comments
 (0)