Skip to content

Commit

Permalink
Fix inline asset paths on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Sep 7, 2021
1 parent 2c8521c commit ee17e85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/wmr/src/lib/fs-utils.js
Expand Up @@ -37,3 +37,13 @@ export function hasCustomPrefix(id) {
// Windows disk letters are not prefixes: C:/foo
return !/^\0?(?:file|https?):\/\//.test(id) && /^\0?[-\w]{2,}:/.test(id);
}

/**
* Convert a file path to a valid URL path. For example, it replaces windows
* path separators with URL path separators.
* @param {string} p
* @returns {string}
*/
export function pathToUrl(p) {
return p.replace(/\\/g, '/');
}
3 changes: 2 additions & 1 deletion packages/wmr/src/plugins/url-plugin.js
Expand Up @@ -3,6 +3,7 @@ import { promises as fs } from 'fs';
import * as kl from 'kolorist';
import { matchAlias } from '../lib/aliasing.js';
import { debug } from '../lib/output-utils.js';
import { pathToUrl } from '../lib/fs-utils.js';

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

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

0 comments on commit ee17e85

Please sign in to comment.