From 14af3d18392c0bcb10577f70808dda6cdeca1fb5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 22 Jul 2021 16:48:10 +0200 Subject: [PATCH] feat: normalizeid and use it to support `from` as path --- README.md | 15 +++++++++++++-- lib/index.d.ts | 3 ++- lib/index.mjs | 15 ++++++++++++++- test/eval.mjs | 4 ++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bec605a..32ae8ef 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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 diff --git a/lib/index.d.ts b/lib/index.d.ts index 1f2ef5d..f39d3b6 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -31,6 +31,7 @@ export function evalModule (code: string, opts?: EvaluateOptions) : Promise export function readModule (id: string, opts?: ResolveOptions) : Promise export function toDataURL(code: string, opts?: ResolveOptions) : Promise -// Path Utils +// Utils export function fileURLToPath (id: URL | string) : string +export function normalizeid (id: URL | string) : string diff --git a/lib/index.mjs b/lib/index.mjs index 15472c0..aa5a94b 100644 --- a/lib/index.mjs +++ b/lib/index.mjs @@ -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() } @@ -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, '/') } diff --git a/test/eval.mjs b/test/eval.mjs index d962d9f..fe94bdb 100644 --- a/test/eval.mjs +++ b/test/eval.mjs @@ -1,4 +1,4 @@ -import { evalModule, loadModule } from 'mlly' +import { evalModule, loadModule, fileURLToPath } from 'mlly' await evalModule('console.log("Eval works!")') @@ -6,7 +6,7 @@ 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 })