Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/unjs/bundle-runner
Run webpack bundles in Node.js with optional VM sandboxing
https://github.com/unjs/bundle-runner
Last synced: 3 months ago
JSON representation
Run webpack bundles in Node.js with optional VM sandboxing
- Host: GitHub
- URL: https://github.com/unjs/bundle-runner
- Owner: unjs
- License: mit
- Created: 2020-04-27T12:23:52.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T10:15:04.000Z (6 months ago)
- Last Synced: 2024-07-13T04:01:43.858Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 107 KB
- Stars: 25
- Watchers: 4
- Forks: 3
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Bundle Runner
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]
[![Dependencies][david-dm-src]][david-dm-href]This package allows running a Webpack bundle in Node.js with optional sandboxed context. Useful for development, loading bundle from memory (HMR) and a consistent way of loading bundle between development and production environments.
**✅ What sandboxing is for:**
- Optional sandboxing using Node.js [VM](https://nodejs.org/api/vm.html)
- Mitigate script evaluation side-effects to global object
- Avoid unwanted shared state
- Avoid memory leaks during HMR**❌ What sandboxing is not for:**
- Fully avoid side effects of evaluation
- A secure sandbox to run untrusted code
- High performance## Install
```sh
yarn add bundle-runnernpm install bundle-runner
```## Usage
```ts
import { createBundle } from 'bundle-runner'const { evaluateEntry } = createBundle('path/to/bundle.json')
const entry = evaluateEntry(context)
```**`createBundle`**
```ts
function createBundle(bundle: Partial | string, options?: CreateBundleOptions): {
bundle: Bundle;
evaluateEntry: (context: object) => any;
evaluateModule: (filename: string, context: object) => any;
rewriteErrorTrace: (err: Error) => Promise;
}
```**`CreateBundleOptions`**
```ts
type CreateBundleOptions = {
basedir?: string;
runInNewContext?: 'once' | boolean;
runningScriptOptions?: VM.RunningScriptOptions;
}
```### Bundle Format
Input can be string (path to a `.js` file or `.json` file with bundle format) or directly bundle object with type of:
```ts
type Bundle = {
basedir: string;
entry: string;
files: {
[filename: string]: string
};
maps: {
[filename: string]: string
};
}
```### SourceMap Support
After creating bundle, a `rewriteErrorTrace` utility is exposed which you can use to rewrite traces:
```ts
const { evaluateEntry, rewriteErrorTrace } = createBundle('path/to/bundle.json')try {
const entry = evaluateEntry(context)
const app = await entry({})
} catch (err) {
await rewriteErrorTrace(err)
throw err
}
```## Credits
Inspired by [vue-server-renderer](https://www.npmjs.com/package/vue-server-renderer) made by [Evan You](https://github.com/yyx990803).
## License
MIT
[npm-version-src]: https://img.shields.io/npm/v/bundle-runner?style=flat-square
[npm-version-href]: https://npmjs.com/package/bundle-runner[npm-downloads-src]: https://img.shields.io/npm/dm/bundle-runner?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/bundle-runner[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/bundle-runner/test/master?style=flat-square
[github-actions-href]: https://github.com/nuxt-contrib/bundle-runner/actions?query=workflow%3Atest[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrib/bundle-runner/master?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt-contrib/bundle-runner[david-dm-src]: https://img.shields.io/david/nuxt-contrib/bundle-runner?style=flat-square
[david-dm-href]: https://david-dm.org/nuxt-contrib/bundle-runner