Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*DS_Store
*.log
.vscode/
node_modules/
public/
.greenwood/
37 changes: 37 additions & 0 deletions greenwood-plugins/monaco-editor-esm-shim-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ResourcePlugin } from "@greenwood/cli";

// https://github.com/microsoft/monaco-editor/issues/886
// have to strip out CSS `import` statements from the ESM build of Monaco Editor,
// otherwise the browser will attempt to load them as ESM and fail
class StripCssImportsResource {
extensions: string[];
contentType: string;

constructor() {
this.extensions = ["js"];
this.contentType = "application/javascript";
}

async shouldIntercept(url: URL) {
return url.pathname.includes("node_modules/monaco-editor") && url.pathname.endsWith(".js");
}

async intercept(url: URL, request: Request, response: Response) {
const contents = await response.text();
const stripped = contents.replace(/^\s*import\s+[^;]*['"]([^'"]+\.css)['"]\s*;?\s*$/gm, "");

return new Response(stripped, {
headers: {
"Content-Type": this.contentType,
},
});
}
}

export function monacoEditorEsmShimPlugin(): ResourcePlugin {
return {
type: "resource",
name: "monaco-editor-strip-css-imports-plugin",
provider: () => new StripCssImportsResource(),
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as rollup from "rollup";
// TODO: depend on these modules first party
// TODO: depend on these modules first party?
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import type { ResourcePlugin } from "@greenwood/cli";
Expand Down Expand Up @@ -52,11 +52,23 @@ class ReplBundlerResource {
nodeResolve(),
commonjs(),
],
onLog(level, log) {
// silence circular dependency warnings from sucrase
if (
log.pluginCode === "CIRCULAR_DEPENDENCY" &&
log.message.includes("node_modules/sucrase")
) {
return;
}
},
});
const { output } = await bundle.generate({
format: "esm",
});

// Create a buffer from the code string to avoid body consumption issues
// const codeBuffer = Buffer.from(output[0].code, 'utf-8');

return new Response(output[0].code, {
headers: {
"Content-Type": "text/javascript",
Expand Down
4 changes: 3 additions & 1 deletion greenwood.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
import { greenwoodPluginCssModules } from "@greenwood/plugin-css-modules";
import { greenwoodPluginImportJsx } from "@greenwood/plugin-import-jsx";
import { replBundlerResourcePlugin } from "./repl-bundler-plugin.ts";
import { replBundlerResourcePlugin } from "./greenwood-plugins/repl-bundler-plugin.ts";
import { monacoEditorEsmShimPlugin } from "./greenwood-plugins/monaco-editor-esm-shim-plugin.ts";
import type { Config } from "@greenwood/cli";

const config: Config = {
Expand All @@ -11,6 +12,7 @@ const config: Config = {
greenwoodPluginImportRaw(),
greenwoodPluginImportJsx(),
replBundlerResourcePlugin(),
monacoEditorEsmShimPlugin(),
],
};

Expand Down
Loading