npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsx6/editor-monaco

v0.48.0

Published

Monaco editor pre-build for standalone usage

Downloads

71

Readme

note

This could easily become obsolete if I find similarly simple way to do this with official builds. For now, I need this build. I am starting to realise that unpkg removes this from cache realetively fast, because there are not many users/downloads. So When I open sites using this, first load can take tens of seconds.

Pre-built Monaco editor with workers

Version 0.34.100

Purpose of this repo is to be able to use Monaco editor in my projects without advanced tools. Monaco is such big editor that I want to load it separately, and if I include it as a dependency I then need to use advanced tooling or complicate my builds.

I admire Vite and it is my favourite tool at the moment because of ease of use. Having big libraries pre-built manually also makes Vite start faster. I love those advanced tools, as they do help a lot, but when possible I want to be able to do things without them. That way I am less dependent on the currently popular tools and can learn more.

Monaco editor can be easily built using esbuild, but to get it working properly, the workers also need to be built as separate JS files. Monaco then needs to know where the workers are, to be able to run the web-workers.

To tell Monaco where worker's JS is for each language we need to declare globally self.MonacoEnvironment so when Monaco editor wants to start the worker it can ask where specific worker JS file is. We communicate this information using a global variable (using too many global variables is bad, but there are legitimate use-cases, and this one is).

  self.MonacoEnvironment = { getWorkerUrl: (moduleId, label)=>{
    if(label === 'javascript') return 'ts.worker.js'
    // .....
  }}

Same worker bundle can handle multiple languages, so we usually will have a mapping similar to this

const workerMap = {
  editorWorkerService: 'editor',
  css: 'css',
  html: 'html',
  json: 'json',
  typescript: 'ts',
  javascript: 'ts',
  less: 'css',
  scss: 'css',
  handlebars: 'html',
  razor: 'html',
}

The worker files are named by convention: editor.worker.js, ts.worker.js, html.worker.js, css.worker.js, json.worker.js so their urls can easily be generated.

workers and CDN

Although you can simply use the main editor code from CDN by adding script tag to your html:

<script src="https://www.unpkg.com/@jsx6/[email protected]/dist/index.js"></script>

it is not as simple as that with web workers. Trying to run worker from CDN on your page will cause a CORS error.

There is a neat trick that I learned about while preparing to use this pre-built Monaco via CDN. The trick was part of Monaco examples at some point and I found it by googling in some old commits. They removed it later on (not sure why).

To be able to run worker code from CDN you need to generate data url with a script inside that tells Monaco where base is, and call importScripts inside the worker script. This way the worker is local (no CORS error) and then our small two-liner worker loads the external script ... and it works :).

const workerBase = 'https://www.unpkg.com/@jsx6/[email protected]/dist/'
self.MonacoEnvironment = { getWorkerUrl: (moduleId, label)=>{
  if(label === 'javascript'){
    const proxyCode = `self.MonacoEnvironment = { baseUrl: '${workerBase}'};
      importScripts('${workerBase}/ts.worker.js');`
        
    return `data:text/javascript;charset=utf-8,${encodeURIComponent(proxyCode)}`
  } 
    // .....
}}

To streamline this and avoid too much copy/pasta setPreBuiltWorkerBase script is built-in and added to Monaco export.

Local copy of Monaco pre-built files more details

<link rel="stylesheet" href="./monaco/index.css">
<script src="./monaco/index.js"></script>
<script>monaco.setPreBuiltWorkerBase('./monaco')</script>

Monaco pre-built files from CDN more details

<link rel="stylesheet" href="https://www.unpkg.com/@jsx6/[email protected]/dist/index.css">
<script src="https://www.unpkg.com/@jsx6/[email protected]/dist/index.js"></script>
<script>monaco.setPreBuiltWorkerBase('https://www.unpkg.com/@jsx6/[email protected]/dist', true)</script>

versions

  • [@jsx6/editor version] -> [monaco version]
  • 0.48.0 -> [monaco/0.48.0]
  • 0.34.100 -> [monaco/0.34.0] - with some internal patches
  • 0.34.0 -> [monaco/0.34.0] - initial version