Skip to content

Commit 87cfc7b

Browse files
Merge pull request #854 from preactjs/fix-windows-asset-paths
Fix inline asset paths on windows
2 parents 2c8521c + ee17e85 commit 87cfc7b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/wmr/src/lib/fs-utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ export function hasCustomPrefix(id) {
3737
// Windows disk letters are not prefixes: C:/foo
3838
return !/^\0?(?:file|https?):\/\//.test(id) && /^\0?[-\w]{2,}:/.test(id);
3939
}
40+
41+
/**
42+
* Convert a file path to a valid URL path. For example, it replaces windows
43+
* path separators with URL path separators.
44+
* @param {string} p
45+
* @returns {string}
46+
*/
47+
export function pathToUrl(p) {
48+
return p.replace(/\\/g, '/');
49+
}

packages/wmr/src/plugins/url-plugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { promises as fs } from 'fs';
33
import * as kl from 'kolorist';
44
import { matchAlias } from '../lib/aliasing.js';
55
import { debug } from '../lib/output-utils.js';
6+
import { pathToUrl } from '../lib/fs-utils.js';
67

78
export const IMPLICIT_URL = /\.(?:png|jpe?g|gif|webp|svg|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf)$/i;
89

@@ -34,7 +35,7 @@ export default function urlPlugin({ inline, root, alias }) {
3435
// In dev mode, we turn the import into an inline module that avoids a network request:
3536
if (inline) {
3637
const aliased = matchAlias(alias, resolved.id);
37-
const url = (aliased || '/' + relative(root, resolved.id)) + '?asset';
38+
const url = pathToUrl(aliased || '/' + relative(root, resolved.id)) + '?asset';
3839
log(`${kl.green('inline')} ${kl.dim(url)} <- ${kl.dim(resolved.id)}`);
3940
return {
4041
id: escapeUrl(`data:text/javascript,export default${JSON.stringify(url)}`),

0 commit comments

Comments
 (0)