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

@blitz/monaco-auto-import

v1.0.7

Published

Easily add auto-import to the Monaco editor, with Javascript & Typescript support.

Downloads

19

Readme

monaco-auto-import

npm version npm downloads Demo

Easily add auto-import to the Monaco editor, with Javascript & Typescript support.

Demo

Example code

import AutoImport, { regexTokeniser } from '@blitz/monaco-auto-import'

const editor = monaco.editor.create(document.getElementById('demo'), {
  value: `
    PAD
    leftPad
    rightPad
  `,
  language: 'typescript'
})

const completor = new AutoImport({ monaco, editor })

completor.imports.saveFiles([
  {
    path: './node_modules/left-pad/index.js',
    aliases: ['left-pad'],
    imports: regexTokeniser(`
      export const PAD = ''
      export function leftPad() {}
      export function rightPad() {}
    `)
  }
])

Getting started

Installing

yarn add @blitz/monaco-auto-import
# or
npm i @blitz/monaco-auto-import --save

Using

Initializing a new instance

Simply create a new Monaco editor instance and pass it to AutoImport. This will register custom completion providers for Monaco's javascript and typescript language services.

import AutoImport from '@blitz/monaco-auto-import'

const editor = monaco.editor.create(document.getElementById('demo'), {
  language: 'typescript'
})

const completor = new AutoImport({ monaco, editor })

Providing completion items

To make the auto-importer aware of a file with exports, simply call completor.imports.saveFile.

completor.imports.saveFile({
  path: './src/my-app.js',
  imports: [
    {
      type: 'const',
      name: 'Testing'
    }
  ]
})

Tokenization

This package includes a built-in regexTokeniser, which uses a simple Regex to extracts exports from Javascript / Typescript code

import { regexTokeniser } from '@blitz/monaco-auto-import'

const imports = regexTokeniser(`
  export const a = 1
  export class Test {}
`)
// [{ type: 'const', name: 'a'}, { type: 'class', name: 'Test' }]

completor.imports.saveFile({
  path: './src/my-app.js',
  imports: imports
})

API

imports.saveFile(file: File): void

Saves a file to the internal store, making it available for completion

imports.saveFiles(files: File[]): void

Bulk-saves files to the internal store from an Array of files

imports.getFile(path: string): File

Fetches a file from the internal store by it's path name (or one of it's aliases).

imports.getImports(name: string): ImportObject[]

Returns all the imports that exactly match a given string.

imports.addImport(path: string, name: string, type?: Expression): boolean

Adds an import to a given file, with an optional type paramater. Returns true if the file existed

imports.removeImport(path: string, name: string): boolean

Removes an import from a given file. Returns true if the file existed