|
| 1 | +<h1 align="center">module-from-string</h1> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | +> Load module from string, require and import. |
| 5 | +</p> |
| 6 | +
|
| 7 | +<p align="center"> |
| 8 | +<a href="https://www.npmjs.com/package/module-from-string"> |
| 9 | +<img alt="npm" src="https://img.shields.io/npm/v/module-from-string"> |
| 10 | +</a> |
| 11 | +<a href="https://github.com/exuanbo/module-from-string/actions?query=workflow"> |
| 12 | +<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/exuanbo/module-from-string/Node.js%20CI/main"> |
| 13 | +</a> |
| 14 | +<a href="https://liberamanifesto.com"> |
| 15 | +<img alt="The Libera Manifesto" src="https://img.shields.io/badge/libera-manifesto-lightgrey.svg"> |
| 16 | +</a> |
| 17 | +</p> |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +```sh |
| 22 | +npm install module-from-string |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +```js |
| 28 | +const { requireFromString, importFromString } = require('module-from-string') |
| 29 | + |
| 30 | +requireFromString("module.exports = 'hi'") // => 'hi' |
| 31 | +requireFromString("exports.salute = 'hi'") // => { salute: 'hi' } |
| 32 | + |
| 33 | +importFromString({ code: "export default 'hi'" }) // => { default: 'hi' } |
| 34 | +importFromString({ code: "export const salute = 'hi'" }) // => { salute: 'hi' } |
| 35 | +``` |
| 36 | + |
| 37 | +## API |
| 38 | + |
| 39 | +```ts |
| 40 | +import { TransformOptions } from 'esbuild' |
| 41 | + |
| 42 | +declare const requireFromString: ( |
| 43 | + code: string, |
| 44 | + globals?: Record<string, unknown> |
| 45 | +) => any |
| 46 | + |
| 47 | +interface ImprotOptions { |
| 48 | + code: string |
| 49 | + transformOptions?: TransformOptions |
| 50 | + globals?: Record<string, unknown> |
| 51 | +} |
| 52 | +declare const importFromString: ({ |
| 53 | + code, |
| 54 | + transformOptions, |
| 55 | + globals |
| 56 | +}: ImprotOptions) => any |
| 57 | + |
| 58 | +export { importFromString, requireFromString } |
| 59 | +``` |
| 60 | + |
| 61 | +### globals? |
| 62 | + |
| 63 | +Underneath the hood, it uses Node.js built-in `vm` module to execute code from string. |
| 64 | + |
| 65 | +```ts |
| 66 | +const _module = new Module(String(new Date().valueOf())) |
| 67 | + |
| 68 | +const context = vm.createContext({ |
| 69 | + __dirname, |
| 70 | + __filename, |
| 71 | + exports: _module.exports, |
| 72 | + module: _module, |
| 73 | + require, |
| 74 | + ...globals |
| 75 | +}) |
| 76 | + |
| 77 | +vm.runInContext(code, context) |
| 78 | +``` |
| 79 | + |
| 80 | +By default, only above variables are passed into the created `Context`. In order to use other global objects you need to add them to option `globals`. |
| 81 | + |
| 82 | +```js |
| 83 | +requireFromString('module.exports = process.cwd()', { process }) |
| 84 | + |
| 85 | +importFromString({ |
| 86 | + code: 'export default process.cwd()', |
| 87 | + globals: { process } |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +### transformOptions? |
| 92 | + |
| 93 | +As bundled `index.d.ts` above, it uses `esbuild` to transform ES Module syntax to CommonJS. So it can do much more by providing transform options to esbuild. See [documentation](https://esbuild.github.io/api/#transform-api). |
| 94 | + |
| 95 | +```js |
| 96 | +const { salute } = importFromString({ |
| 97 | + code: "export const salute: string = () => 'hi'", |
| 98 | + transformOptions: { loader: 'ts' } |
| 99 | +}) |
| 100 | + |
| 101 | +salute() // => 'hi' |
| 102 | +``` |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +[MIT License](https://github.com/exuanbo/module-from-string/blob/main/LICENSE) © 2020 [Exuanbo](https://github.com/exuanbo) |
0 commit comments