Skip to content

Commit

Permalink
feat: add url.pathToFileURL to bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Nov 9, 2024
1 parent 182760f commit 100389f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bundler/modules/url.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const urlLib = require('url/url.js')

function pathToFileURL(path) {
const url = urlLib.format({ protocol: 'file:', pathname: path, slashes: true })
return globalThis.URL ? new globalThis.URL(url) : urlLib.parse(url)
}

function fileURLToPath(url, options) {
if (options?.windows) throw new Error('Windows mode not supported')
if (typeof url === 'string') {
Expand All @@ -10,7 +15,7 @@ function fileURLToPath(url, options) {
}

if (url.protocol !== 'file:' || url.host !== '') throw new Error('Input is not a file URL')
const { path } = url
const path = url.pathname
for (let n = 0; n < path.length; n++) {
if (path[n] !== '%' && path[n + 1] === '2' && (path.codePointAt(n + 2) | 0x20) === 102) {
throw new Error('must not include encoded / characters')
Expand All @@ -20,4 +25,4 @@ function fileURLToPath(url, options) {
return path
}

module.exports = { ...urlLib, fileURLToPath }
module.exports = { ...urlLib, pathToFileURL, fileURLToPath }

0 comments on commit 100389f

Please sign in to comment.