@purinton/path
Advanced tools
+12
-1
@@ -1,2 +0,2 @@ | ||
| const { fileURLToPath } = require('url'); | ||
| const { fileURLToPath, pathToFileURL } = require('url'); | ||
| const { dirname: pathDirname, join: pathJoin } = require('path'); | ||
@@ -47,2 +47,12 @@ | ||
| /** | ||
| * Converts a resolved path to a file URL for dynamic import compatibility. | ||
| * @param {any} metaOrDir - import.meta, a string (dirname), or undefined | ||
| * @param {...string} segments - Additional path segments to join | ||
| * @returns {URL} The file URL | ||
| */ | ||
| const pathUrl = (metaOrDir, ...segments) => { | ||
| return pathToFileURL(path(metaOrDir, ...segments)).href; | ||
| }; | ||
| module.exports = path; | ||
@@ -53,1 +63,2 @@ module.exports.default = path; | ||
| module.exports.getCurrentDirname = getCurrentDirname; | ||
| module.exports.pathUrl = pathUrl; |
+8
-0
@@ -30,2 +30,10 @@ /** | ||
| /** | ||
| * Converts a resolved path to a file URL href string for dynamic import compatibility. | ||
| * @param metaOrDir import.meta (ESM), a string (dirname, e.g. __dirname), or undefined | ||
| * @param segments Path segments to join. | ||
| * @returns File URL href string. | ||
| */ | ||
| export function pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string; | ||
| export default path; |
+11
-1
@@ -1,2 +0,2 @@ | ||
| import { fileURLToPath } from 'url'; | ||
| import { fileURLToPath, pathToFileURL } from 'url'; | ||
| import { dirname as pathDirname, join as pathJoin } from 'path'; | ||
@@ -47,3 +47,13 @@ | ||
| /** | ||
| * Converts a resolved path to a file URL for dynamic import compatibility. | ||
| * @param {any} metaOrDir - import.meta, a string (dirname), or undefined | ||
| * @param {...string} segments - Additional path segments to join | ||
| * @returns {string} The file URL | ||
| */ | ||
| export const pathUrl = (metaOrDir, ...segments) => { | ||
| return pathToFileURL(path(metaOrDir, ...segments)).href; | ||
| }; | ||
| export default path; | ||
| export { path }; |
+1
-1
| { | ||
| "name": "@purinton/path", | ||
| "type": "module", | ||
| "version": "1.0.9", | ||
| "version": "1.0.10", | ||
| "description": "An ESM/Jest friendly path utility.", | ||
@@ -6,0 +6,0 @@ "main": "index.cjs", |
+31
-8
@@ -16,2 +16,3 @@ # [](https://discord.gg/QSBxQnX7PF) | ||
| - [CommonJS Example](#commonjs-example) | ||
| - [Dynamic Import Example](#dynamic-import-example) | ||
| - [API](#api) | ||
@@ -27,2 +28,3 @@ - [TypeScript](#typescript) | ||
| - Simple, dependency-free, and well-tested | ||
| - **NEW:** `pathUrl` helper for cross-platform dynamic import | ||
@@ -40,6 +42,3 @@ ## Installation | ||
| ```js | ||
| // Example for ESM (module JS) usage | ||
| // Import the path utility from the installed package | ||
| import path from '@purinton/path'; | ||
| // or import { path } from '@purinton/path'; // identical | ||
| import path, { pathUrl } from '@purinton/path'; | ||
@@ -49,2 +48,7 @@ // for ESM, we need to pass import.meta | ||
| console.log(envFile); | ||
| // Get a file URL href for dynamic import | ||
| const envFileUrl = pathUrl(import.meta, ".env"); | ||
| console.log(envFileUrl); | ||
| // import(envFileUrl).then(mod => ...); | ||
| ``` | ||
@@ -55,6 +59,3 @@ | ||
| ```js | ||
| // Example for CommonJS usage | ||
| // Import the path utility from the installed package | ||
| const path = require('@purinton/path'); | ||
| // or const { path } = require('@purinton/path'); // identical | ||
| const { path, pathUrl } = require('@purinton/path'); | ||
@@ -64,4 +65,21 @@ // for CommonJS, we need to pass __dirname | ||
| console.log(envFile); | ||
| // Get a file URL href for dynamic import | ||
| const envFileUrl = pathUrl(__dirname, '.env'); | ||
| console.log(envFileUrl); | ||
| // import(envFileUrl).then(mod => ...); // if using ESM loader in CJS | ||
| ``` | ||
| ### Dynamic Import Example | ||
| ```js | ||
| // ESM | ||
| import { pathUrl } from '@purinton/path'; | ||
| const mod = await import(pathUrl(import.meta, './my-module.mjs')); | ||
| // CommonJS (with ESM loader) | ||
| const { pathUrl } = require('@purinton/path'); | ||
| const mod = await import(pathUrl(__dirname, './my-module.mjs')); | ||
| ``` | ||
| ## API | ||
@@ -81,2 +99,6 @@ | ||
| ### `pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string` | ||
| Returns a file URL href string for the resolved path, suitable for use with dynamic `import()` on all platforms. | ||
| ## TypeScript | ||
@@ -90,2 +112,3 @@ | ||
| export const path: (metaOrDir: ImportMeta | string, ...segments: string[]) => string; | ||
| export function pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string; | ||
| export default path; | ||
@@ -92,0 +115,0 @@ ``` |
13023
17.84%144
22.03%126
22.33%