Comparing version 0.1.0 to 0.1.1
@@ -1,6 +0,1 @@ | ||
export interface ImportMeta { | ||
url: string | ||
[key: string]: any | ||
} | ||
export interface CommonjsContext { | ||
@@ -10,3 +5,26 @@ __filename: string | ||
} | ||
export type createCommonJS = (importMeta: ImportMeta) => CommonjsContext | ||
export type createCommonJS = (importMeta: ImportMeta) => CommonjsContext | ||
export interface ResolveOptions { | ||
from?: string | URL | ||
conditions?: string[] | ||
} | ||
export type ResolveFn<T> = (id: string, opts: ResolveOptions) => T | ||
export type resolve = ResolveFn<Promise<string>> | ||
export type resolveSync = ResolveFn<string> | ||
export type resolvePath = ResolveFn<Promise<string>> | ||
export type resolvePathSync = ResolveFn<string> | ||
export type createResolve = (from: ImportMeta|string) => ResolveFn<Promise<string>> | ||
export interface EvaluateOptions extends ResolveOptions { | ||
from?: URL | string | ||
} | ||
export interface ReadOptions extends ResolveOptions { | ||
} | ||
export type loadModule = (id: string, opts?: EvaluateOptions) => Promise<any> | ||
export type evalModule = (code: string, opts?: EvaluateOptions) => Promise<any> | ||
export type readModule = (id: string, opts?: ReadOptions) => Promise<any> | ||
export type toDataURL = (code: string) => string |
{ | ||
"name": "mlly", | ||
"version": "0.1.0", | ||
"description": "ECMAScript module utils for Node.js", | ||
"version": "0.1.1", | ||
"description": "Missing ECMAScript module utils for Node.js", | ||
"repository": "unjs/mlly", | ||
@@ -16,3 +16,2 @@ "license": "MIT", | ||
"scripts": { | ||
"dev": "node ./test/test.mjs", | ||
"lint": "eslint --ext .mjs lib", | ||
@@ -22,3 +21,5 @@ "release": "yarn test && standard-version && npm publish && git push --follow-tags", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"import-meta-resolve": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
@@ -25,0 +26,0 @@ "@nuxtjs/eslint-config": "latest", |
118
README.md
# 🤝 mlly | ||
> [ECMAScript module](https://nodejs.org/api/esm.html) utils for Node.js | ||
> Missing [ECMAScript module](https://nodejs.org/api/esm.html) utils for Node.js | ||
## Usage | ||
### Install | ||
## Install | ||
> This package is ESM only. Node.js 12+ is needed to use it and it must be imported instead of required. | ||
Install npm package: | ||
@@ -19,4 +20,12 @@ | ||
### CommonJS Context | ||
Import utils: | ||
```js | ||
import {} from 'mlly' | ||
``` | ||
## CommonJS Context | ||
### `createCommonJS` | ||
This utility creates a compatible context that we loose when migrating to ECMA modules. | ||
@@ -30,4 +39,105 @@ | ||
## Resolving Modules | ||
There are several utils exposed allow resolving another module URL or Path. (internally using [wooorm/import-meta-resolve](https://github.com/wooorm/import-meta-resolve) that re-exports Node.js code). | ||
- **`resolve(id, resolveOptions?)`** | ||
- **`resolvePath(id, resolveOptions?)`** | ||
- **`createResolve(import.meta)`** | **`createResolve(base)`** | ||
- `resolveSync(id, resolveOptions?)` | ||
- `resolvePathSync(id, resolveOptions?)` | ||
It is recommended to use `resolve` and `createResolve` since module resolution spec allows aync resolution. | ||
```js | ||
import { resolve, resolvePath, createResolve } from 'mlly' | ||
// //home/user/project/module.mjs | ||
console.log(await resolvePath('./module.mjs', { from: import.meta.url })) | ||
// file:///home/user/project/module.mjs | ||
console.log(await resolve('./module.mjs', { from: import.meta.url })) | ||
// file:///home/user/project/module.mjs | ||
const _resolve = createResolve(import.meta) | ||
console.log(await _resolve('./module.mjs')) | ||
``` | ||
**Resolve options:** | ||
- `from`: URL or string (default is `pwd()`) | ||
- `conditions`: Array of conditions used for resolution algorithm (default is `['node', 'import']`) | ||
## Evaluating Moduls | ||
### `loadModule` | ||
Dynamically loads a module by evaluating source code. (useful to bypass import cache) | ||
```js | ||
import { loadModule } from 'mlly' | ||
await loadModule('./hello.mjs', { from: import.meta.url }) | ||
``` | ||
### `evalModule` | ||
Evaluates JavaScript module code using dynamic [`data:`](https://nodejs.org/api/esm.html#esm_data_imports) import. | ||
```js | ||
import { evalModule } from 'mlly' | ||
await evalModule(`console.log("Hello World!")`) | ||
await evalModule(` | ||
import { reverse } from './utils.mjs' | ||
console.log(reverse('!emosewa si sj')) | ||
`, { | ||
from: import.meta.url | ||
}) | ||
``` | ||
### `readModule` | ||
Resolves id using `resolve` and reads source code. | ||
```js | ||
import { readModule } from 'mlly' | ||
console.log(await readModule('./index.mjs', import.meta.url)) | ||
``` | ||
### `toDataURL` | ||
Convert code to [`data:`](https://nodejs.org/api/esm.html#esm_data_imports) URL using base64 encoding. | ||
```js | ||
import { toDataURL } from 'mlly' | ||
console.log(await toDataURL(` | ||
// This is an example | ||
console.log('Hello world') | ||
`)) | ||
``` | ||
## Path utils | ||
### `fileURLToPath` | ||
Similar to [url.fileURLToPath](https://nodejs.org/api/url.html#url_url_fileurltopath_url) but also converts windows backslash `\` to unix slash `/` | ||
```js | ||
import { fileURLToPath } from 'mlly' | ||
// /foo/bar.js | ||
console.log(fileURLToPath('file:///foo/bar.js')) | ||
// C:/path | ||
console.log(fileURLToPath('file:///C:/path/')) | ||
``` | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9374
6
111
142
1
4
+ Addedimport-meta-resolve@^1.1.1
+ Addedbuiltins@4.1.0(transitive)
+ Addedimport-meta-resolve@1.1.1(transitive)
+ Addedsemver@7.6.3(transitive)