Skip to content

Commit 1db85be

Browse files
committed
docs: update
1 parent f3f1958 commit 1db85be

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [3.2.0](https://github.com/exuanbo/module-from-string/compare/v3.1.4...v3.2.0) (2022-07-17)
4+
5+
### Features
6+
7+
- Add new optional option `useCurrentGlobal` to pass all the available variable from the current `global` (or `globalThis`) into the context.
8+
- Add curried functions `createRequireFromString`, `createImportFromString` and `createImportFromStringSync`.
9+
310
## [3.1.4](https://github.com/exuanbo/module-from-string/compare/v3.1.3...v3.1.4) (2021-12-23)
411

512
### Bug Fixes

README.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,38 @@ importFromStringSync(
3838
## API
3939

4040
```ts
41-
import { TransformOptions } from 'esbuild'
41+
import { Context } from 'vm';
42+
import { TransformOptions } from 'esbuild';
4243

4344
interface Options {
4445
dirname?: string
45-
globals?: Record<string, unknown>
46+
globals?: Context
47+
useCurrentGlobal?: boolean
4648
}
4749

50+
declare const requireFromString: (code: string, options?: Options) => any
51+
declare const createRequireFromString: (options?: Options) => typeof requireFromString
52+
4853
interface ImportOptions extends Options {
4954
transformOptions?: TransformOptions
5055
}
5156

52-
declare const requireFromString: (
53-
code: string,
54-
{ dirname, globals }?: Options
55-
) => any
56-
57-
declare const importFromString: (
58-
code: string,
59-
{ dirname, globals, transformOptions }?: ImportOptions
60-
) => Promise<any>
57+
declare const importFromString: (code: string, options?: ImportOptions) => Promise<any>
58+
declare const createImportFromString: (options?: ImportOptions) => typeof importFromString
6159

62-
declare const importFromStringSync: (
63-
code: string,
64-
{ dirname, globals, transformOptions }?: ImportOptions
65-
) => any
60+
declare const importFromStringSync: (code: string, options?: ImportOptions) => any
61+
declare const createImportFromStringSync: (options?: ImportOptions) => typeof importFromStringSync
6662
```
6763
6864
### dirname
6965
7066
An absolute path of the directory for resolving `require` or `import` from relative path.
7167
7268
```js
73-
requireFromString("module.exports = require('.')", {
74-
dirname: path.join(__dirname, "../lib")
75-
}) // => require('../lib')
69+
requireFromString(
70+
"module.exports = require('.')",
71+
{ dirname: path.join(__dirname, "../lib") }
72+
) // => require('../lib')
7673
```
7774

7875
If not specified, the default value will be the current file's directory.
@@ -81,7 +78,7 @@ If not specified, the default value will be the current file's directory.
8178

8279
Underneath the hood, `module-from-string` uses Node.js built-in `vm` module to execute code.
8380

84-
```ts
81+
```js
8582
// requireFromString
8683

8784
vm.runInNewContext(
@@ -96,7 +93,9 @@ vm.runInNewContext(
9693
},
9794
```
9895
99-
Take `requireFromString` for example, only the above variables are passed into the `contextObject`. In order to use other global objects and built-in modules you need to add them to option `globals`.
96+
Take `requireFromString` for example, only the above module scope variables are passed into the `contextObject`.
97+
98+
In order to use other [global objects](https://nodejs.org/api/globals.html) that specific to Node.js, they need to be added to option `globals` **or** set option [`useCurrentGlobal`](#usecurrentglobal) to `true`.
10099
101100
```js
102101
requireFromString(
@@ -105,6 +104,17 @@ requireFromString(
105104
) // => $PWD
106105
```
107106
107+
**Note**: by default the [built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) have a different prototype.
108+
109+
```js
110+
const err = requireFromString('module.exports = new Error()')
111+
err instanceof Error // => false
112+
```
113+
114+
### useCurrentGlobal
115+
116+
Default to `false`. If set to `true`, all the available variables from the current `global` (or `globalThis`) are passed into the context.
117+
108118
### transformOptions
109119
110120
Function `importFromString` and `importFromStringSync` can use `esbuild` to transform code syntax. See [esbuild Transform API](https://esbuild.github.io/api/#transform-api) for documentation.

0 commit comments

Comments
 (0)