Coder Social home page Coder Social logo

cweili / vm-worker Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 72 KB

Tiny virtual machine for browser to execute javascript modules in Web Worker

License: MIT License

JavaScript 100.00%
browser exec execute javascript vm webworker webworkers virtual-machine esm esmodules

vm-worker's Introduction

VM Worker

npm bundle size npm downloads license

github build coverage

Tiny virtual machine for browser to execute javascript modules in Web Worker.

Features

  • Run code in a isolated scope without pollute your environment
  • Support CommonJS and ESModules (by plugin)
  • Support TypeScript and Flow (by plugin)
  • Based on Web Worker

Usage

Basic usage

App.js

import VM from 'vm-worker'

const vm = VM({
  debug: false, // default false
  timeout: 100000, // default 100000ms
})

await vm.require([
  {
    path: 'module-one/index.js',
    src: 'module.exports = 1',
  },
  {
    path: '/dirA/a.js',
    url: 'https://xxx.com/a.js',
  },
  {
    path: '/dirB/b.js',
    src: 'module.exports = require("../dirA/a")',
  },
])

await vm.exec('/dirB/b.js', 1, 2) // => 4

vm.terminate()

a.js

module.exports = (a, b) => (a + b + require('module-one'))

ESModule Plugin

App.js

import VM from 'vm-worker'
import ESMPlugin from 'vm-worker/dist/plugins/esmodule.esm'

const vm = VM({
  plugins: [
    ESMPlugin(),
  ],
})

await vm.require([
  {
    path: 'module-one/index.js',
    src: `export const ONE = 1`
  },
  {
    path: '/dirA/a.js',
    url: 'https://xxx.com/a.js',
  },
  {
    path: '/dirB/b.js',
    src: `import { plus } from "../dirA/a"
          export default plus`,
  },
])

await vm.exec('/dirB/b.js', 1, 2) // => 4

vm.terminate()

a.js

import { ONE } from 'module-one'

export function plus(a, b) {
  return a + b + ONE
}

Sucrase plugin

Sucrase is similar to Babel, which compiles TypeScript, Flow and JSX to standard JavaScript.

Sucrase transform options document

App.js

import VM from 'vm-worker'
import SucrasePlugin from 'vm-worker/dist/plugins/sucrase.esm'

const vm = VM({
  plugins: [
    SucrasePlugin({
      ... // Sucrase transform options
    }),
  ],
})

await vm.require([
  {
    path: 'module-one/index.js',
    src: `export const ONE: number = 1`
  },
  {
    path: '/dirA/a.js',
    url: 'https://xxx.com/a.js',
  },
  {
    path: '/dirB/b.js',
    src: `import { plus } from "../dirA/a"
          export default plus`,
  },
])

await vm.exec('/dirB/b.js', 1, 2) // => 4

vm.terminate()

a.js

import { ONE } from 'module-one'

export function plus(a: number, b: number) {
  return a + b + ONE
}

Installation

npm i vm-worker

vm-worker's People

Contributors

cweili avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.