my-runner v3.1.3
my-runner
Run CommonJS script in sandbox with customized style
How it works?
my-runner uses vm module for mocking an CommonJS's script environment. So we can make the environment as your wish.
For example, jest uses customized environment for running test files. And we could use it running browser's script.
Installation
npm install my-runner
# or use yarn
yarn add my-runnerUsage
Package
const myRunner = require('my-runner')
myRunner.runFile('/path/to/file.js').module.exportsCli
npm i my-runner -g
my-runner --helpAPI
run
Run code script
Parameters
code{string}opts{{}} (optional, default{})opts.rootDirstring? The run environment's root directory path.opts.myrunnerrcboolean? Whether or not to look up .my-runnerrc or my-runner.config.js file.opts.preset({} | Function | {name: string, options: any})? A preset that is used as a base for configuration.opts.moduleNameMapperObject<string, string>? A map from regular expressions to module names that allow to stub out resources, like images or styles with a single module.opts.modulePathIgnorePatternsArray<string>? An array of regexp pattern strings that are matched against all module paths before those paths are to be considered 'visible' to the module loader.opts.modulePathsArray<string>? An array of absolute paths to additional locations to search when resolving modules. Use NODE_PATH env variable by default.opts.moduleFileExtensionsArray<string> An array of file extensions your modules use. (optional, default['.js','.json','.jsx','.ts','.tsx','.node'])opts.moduleDirectoriesArray<string> An array of directory names to be searched recursively up from the requiring module's location. (optional, default['node_modules'])opts.preserveSymlinksboolean? preserveSymlinksopts.resolverFunction? This option allows the use of a custom resolver. More information see jest.opts.browserboolean Respect Browserify's "browser" field in package.json when resolving modules. (optional, defaultfalse)opts.transformIgnorePatternsArray<string> An array of regexp pattern strings that are matched against all source file paths before transformation. If the test path matches any of the patterns, it will not be transformed. (optional, default['/node_modules/'])opts.transformObject<string, (string | Function | {name: string, options: any})> A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files. (optional, default{})opts.globalSetupstring? This option allows the use of a custom global setup module which exports an async function that is triggered once.opts.globalsObject? A set of global variables that need to be available in environments.opts.transformContextFunction? A sync function for transforming context.opts.globalany The reference on global environment. (optional, default{...global})opts.moduleCache{}? The reference ofrequire.cacheopts.moduleMain{}? The reference ofrequire.mainopts.fsObject The fs module. (optional, defaultrequire('fs'))opts.vmObject The vm module. (optional, defaultrequire('vm'))
Returns RunResult
runFile
Run file script
Parameters
Returns RunResult
RunResult
Type: {module: {exports}, exports, require, global}
defaultAdvancedOptions
The default options
How to mock module?
my-runner had injected myRunner in global.
for example, Mocking prop-types and setting moduleNameMapper to be {"^prop-types$": "/path/to/mock-prop-types.js"}
myRunner
requireActual
Requires the actual module rather than the mock module.
- Type:
Function
Contributing
- Fork it!
- Create your new branch:
git checkout -b feature-neworgit checkout -b fix-which-bug - Start your magic work now
- Make sure npm test passes
- Commit your changes:
git commit -am 'feat: some description (close #123)'orgit commit -am 'fix: some description (fix #123)' - Push to the branch:
git push - Submit a pull request :)
Authors
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
License
MIT - imcuttle 🐟
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago