Skip to content

Instantly share code, notes, and snippets.

@mattpowell
Last active September 6, 2021 06:40
Show Gist options
  • Save mattpowell/221f7d35c4ae1273dc2e1ee469d000a7 to your computer and use it in GitHub Desktop.
Save mattpowell/221f7d35c4ae1273dc2e1ee469d000a7 to your computer and use it in GitHub Desktop.
Monaco Editor (VS Code) and Snowpack

Monaco Editor and Snowpack Example

Use Snowpack to extract and build JavaScript Modules and run in the browser.

Installation

  • Download and unzip this gist
  • npm install
  • npm run snowpack # optional, should run by default during npm install
  • npm run serve

About

TODO

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<style>
html, body, #monaco {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="monaco"></div>
<script type="module">
import * as Monaco from '/web_modules/monaco-editor.js'
self.MonacoEnvironment = {
getWorker(workerId, label) {
return new Worker(
self.MonacoEnvironment.getWorkerUrl(workerId, label),
{
name: label,
type: 'module'
}
);
},
getWorkerUrl: function(moduleId, label) {
if (label === 'json') {
return '/web_modules/monaco-editor/esm/vs/language/json/json.worker.js';
}
if (label === 'css') {
return '/web_modules/monaco-editor/esm/vs/language/css/css.worker.js';
}
if (label === 'html') {
return '/web_modules/monaco-editor/esm/vs/language/html/html.worker.js';
}
if (label === 'typescript' || label === 'javascript') {
return '/web_modules/monaco-editor/esm/vs/language/typescript/ts.worker.js';
}
return '/web_modules/monaco-editor/esm/vs/editor/editor.worker.js';
}
};
const editor = Monaco.editor.create(document.getElementById('monaco'), {
value: document.documentElement.outerHTML,
language: 'html'
});
window.addEventListener('resize', () => editor.layout(), false);
</script>
</body>
</html>
{
"name": "monaco-snowpack-demo",
"version": "0.0.1",
"description": "A bare bones/minimal demo of Monaco working via Snowpack (and JS Modules)",
"scripts": {
"serve": "python3 -m http.server 8008",
"snowpack": "snowpack",
"prepare": "npm run snowpack"
},
"keywords": ["monaco", "snowpack", "vs code"],
"author": "Matt Powell",
"license": "MIT",
"devDependencies": {
"monaco-editor": "^0.20.0",
"postcss-url": "^8.0.0",
"snowpack": "^1.4.0",
"rollup-plugin-postcss": "^2.0.6"
}
}
const postcss = require('rollup-plugin-postcss');
const url = require('postcss-url');
module.exports = {
installOptions: {
dest: "web_modules",
optimize: true
},
webDependencies: [
'monaco-editor',
'monaco-editor/esm/vs/language/json/json.worker.js',
'monaco-editor/esm/vs/language/css/css.worker.js',
'monaco-editor/esm/vs/language/html/html.worker.js',
'monaco-editor/esm/vs/language/typescript/ts.worker.js',
'monaco-editor/esm/vs/editor/editor.worker.js'
],
rollup: {
plugins: [
postcss({
plugins: [url({
url: 'inline'
})]
})
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment