Skip to content

Commit

Permalink
feat: normalizeid and use it to support from as path
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 22, 2021
1 parent 0725634 commit 14af3d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -149,11 +149,11 @@ console.log(await toDataURL(`
`))
```
## Path utils
## Other utils
### `fileURLToPath`
Similar to [url.fileURLToPath](https://nodejs.org/api/url.html#url_url_fileurltopath_url) but also converts windows backslash `\` to unix slash `/`
Similar to [url.fileURLToPath](https://nodejs.org/api/url.html#url_url_fileurltopath_url) but also converts windows backslash `\` to unix slash `/` and handles if input is already a path.
```js
import { fileURLToPath } from 'mlly'
Expand All @@ -165,6 +165,17 @@ console.log(fileURLToPath('file:///foo/bar.js'))
console.log(fileURLToPath('file:///C:/path/'))
```
### `normalizeid`
Ensures id has either of `node:`, `data:`, `http:`, `https:` or `file:` protocols.
```js
import { ensureProtocol } from 'mlly'

// file:///foo/bar.js
console.log(normalizeid('/foo/bar.js'))
```
## License
MIT
3 changes: 2 additions & 1 deletion lib/index.d.ts
Expand Up @@ -31,6 +31,7 @@ export function evalModule (code: string, opts?: EvaluateOptions) : Promise<any>
export function readModule (id: string, opts?: ResolveOptions) : Promise<any>
export function toDataURL(code: string, opts?: ResolveOptions) : Promise<string>

// Path Utils
// Utils

export function fileURLToPath (id: URL | string) : string
export function normalizeid (id: URL | string) : string
15 changes: 14 additions & 1 deletion lib/index.mjs
Expand Up @@ -38,7 +38,7 @@ function _resolve (id, opts = {}) {
return 'node:' + id
}
const conditionsSet = opts.conditions ? new Set(opts.conditions) : DEFAULT_CONDITIONS_SET
const resolved = moduleResolve(id, opts.from || DEFAULT_FROM, conditionsSet)
const resolved = moduleResolve(id, opts.from ? normalizeid(opts.from) : DEFAULT_FROM, conditionsSet)
const realPath = realpathSync(fileURLToPath(resolved))
return pathToFileURL(realPath).toString()
}
Expand Down Expand Up @@ -104,6 +104,19 @@ export function fileURLToPath (id) {
return normalizeSlash(_fileURLToPath(id))
}

export function normalizeid (id) {
if (typeof id !== 'string') {
id = id.toString()
}
if (/(node|data|http|https|file):/.test(id)) {
return id
}
if (BUILُTIN_MODULES.has(id)) {
return 'node:' + id
}
return 'file://' + normalizeSlash(id)
}

function normalizeSlash (str) {
return str.replace(/\\/g, '/')
}
Expand Down
4 changes: 2 additions & 2 deletions test/eval.mjs
@@ -1,12 +1,12 @@
import { evalModule, loadModule } from 'mlly'
import { evalModule, loadModule, fileURLToPath } from 'mlly'

await evalModule('console.log("Eval works!")')

await evalModule(`
import { reverse } from './utils.mjs'
console.log(reverse('!emosewa si sj'))
`, {
from: import.meta.url
from: fileURLToPath(import.meta.url)
})

await loadModule('./hello.mjs', { from: import.meta.url })

0 comments on commit 14af3d1

Please sign in to comment.