1.0.0-pre.6 • Published 6 years ago

cdn-run v1.0.0-pre.6

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

Run custom code in a dynamic environment with access to all* npm modules

* except modules having dependencies on native modules

cdn-run lets you run custom code in an environment where npm dependencies have been resolved and can be injected dynamically. No need for Webpack, Rollup, Parcel or even npm install.

Usage

In the following example, we create a new runtime Context, and specify that we want both react@16 and react-dom@16 to be available. We also include a local file, index.js that relies on both of these npm modules and re-exports them.

const { Context } = require('cdn-run');

const runner = new Context({
    dependencies: {
        react: '16.x',
        'react-dom': '16.x',
    },
    files: {
        'index.js': `
            const React = require('react');
            const ReactDOM = require('react-dom');

            module.exports = { React, ReactDOM };
        `,
    },
});
const { React, ReactDOM } = await runner.run('index.js');

How it works

Under the hood, this tool uses resolve-npm-dependency-graph to build the dependency graph based on the required npm modules and then configures SystemJS so that this graph is respected and so that the supplied local files can be loaded from memory.

API

Context

new Context(options)

Create a new Context instance, configured with a set of dependencies and local files. Supports the options:

  • dependencies - (optional) a mapping of package names to version specs (just like you would put in package.json).
  • files - (optional) a mapping of local filenames to their string contents.

run(pathname)

Runs a local file where:

  • pathname - (required) the path to the local file that should be treated as the entrypoint

Returns a Promise that resolves to the exports of that local file.

1.0.0-pre.26

6 years ago

1.0.0-pre.25

6 years ago

1.0.0-pre.24

6 years ago

1.0.0-pre.23

6 years ago

1.0.0-pre.22

6 years ago

1.0.0-pre.21

6 years ago

1.0.0-pre.20

6 years ago

1.0.0-pre.19

6 years ago

1.0.0-pre.18

6 years ago

1.0.0-pre.17

6 years ago

1.0.0-pre.16

6 years ago

1.0.0-pre.15

6 years ago

1.0.0-pre.14

6 years ago

1.0.0-pre.13

6 years ago

1.0.0-pre.12

6 years ago

1.0.0-pre.11

6 years ago

1.0.0-pre.10

6 years ago

1.0.0-pre.9

6 years ago

1.0.0-pre.8

6 years ago

1.0.0-pre.7

6 years ago

1.0.0-pre.6

6 years ago

1.0.0-pre.5

6 years ago

1.0.0-pre.4

6 years ago

1.0.0-pre.3

6 years ago

1.0.0-pre.2

6 years ago

1.0.0-pre.1

6 years ago

1.0.0-pre.0

6 years ago