Skip to content

Commit 9d6ae35

Browse files
Expose normalizePath helper
1 parent 88ed7e6 commit 9d6ae35

File tree

8 files changed

+28
-13
lines changed

8 files changed

+28
-13
lines changed

.changeset/new-falcons-approve.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@wmr-plugins/directory-import': patch
3+
'@wmr-plugins/service-worker': patch
4+
'wmr': patch
5+
---
6+
7+
Expose `normalizePath` function from `wmr` to ensure normalization is applied the same way across plugins and core.

packages/directory-plugin/src/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { promises as fs } from 'fs';
2+
import { normalizePath } from 'wmr';
23
import path from 'path';
34

4-
const pathToPosix = p => p.split(path.sep).join(path.posix.sep);
5-
65
/**
76
* @param {object} [options]
87
* @param {string} [options.cwd]
@@ -17,7 +16,7 @@ function directoryPlugin(options) {
1716
const resolved = await this.resolve(id.slice(4) + '\0', importer, { skipSelf: true });
1817

1918
if (resolved) {
20-
return '\0dir:' + pathToPosix(resolved.id).replace(/\0$/, '');
19+
return '\0dir:' + normalizePath(resolved.id).replace(/\0$/, '');
2120
}
2221
},
2322
async load(id) {

packages/sw-plugin/src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path';
22
import { request } from 'http';
3-
4-
const pathToPosix = p => p.split(path.sep).join(path.posix.sep);
3+
import { normalizePath } from 'wmr';
54

65
/**
76
* Service Worker plugin for WMR.
@@ -19,7 +18,7 @@ export default function swPlugin(options) {
1918

2019
const wmrProxyPlugin = {
2120
resolveId(id) {
22-
const normalizedId = id[1] + id[2] === ':\\' ? pathToPosix(id.slice(2)) : id;
21+
const normalizedId = id[1] + id[2] === ':\\' ? normalizePath(id.slice(2)) : id;
2322
if (id.startsWith('/@npm/')) return id;
2423
if (!/^\.*\//.test(normalizedId)) return '/@npm/' + id;
2524
},
@@ -49,7 +48,7 @@ export default function swPlugin(options) {
4948
async resolveId(id, importer) {
5049
if (!id.startsWith('sw:')) return;
5150
const resolved = await this.resolve(id.slice(3), importer);
52-
if (resolved) return `\0sw:${pathToPosix(resolved.id)}`;
51+
if (resolved) return `\0sw:${normalizePath(resolved.id)}`;
5352
},
5453
async load(id) {
5554
if (!id.startsWith('\0sw:')) return;

packages/wmr/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "wmr",
33
"version": "1.3.2",
44
"bin": "wmr.cjs",
5+
"main": "./src/index.js",
56
"type": "module",
67
"scripts": {
78
"demo": "cd demo && node --experimental-modules ../src/cli.js 2>&1",

packages/wmr/src/bundler.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { relative, sep, posix, resolve, dirname } from 'path';
1+
import { relative, posix, resolve, dirname } from 'path';
22
import * as rollup from 'rollup';
33
import htmPlugin from './plugins/htm-plugin.js';
44
import sucrasePlugin from './plugins/sucrase-plugin.js';
55
import wmrPlugin from './plugins/wmr/plugin.js';
66
import wmrStylesPlugin from './plugins/wmr/styles-plugin.js';
7+
import { normalizePath } from './utils.js';
78
import sassPlugin from './plugins/sass-plugin.js';
89
import terser from './plugins/fast-minify.js';
910
import npmPlugin from './plugins/npm-plugin/index.js';
@@ -24,9 +25,6 @@ import copyAssetsPlugin from './plugins/copy-assets-plugin.js';
2425
import nodeBuiltinsPlugin from './plugins/node-builtins-plugin.js';
2526
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
2627

27-
/** @param {string} p */
28-
const pathToPosix = p => p.split(sep).join(posix.sep);
29-
3028
/** @typedef {import('rollup').OutputOptions} OutputOptions */
3129
/** @typedef {OutputOptions | ((opts: OutputOptions) => OutputOptions)} Output */
3230

@@ -84,7 +82,7 @@ export async function bundleProd({
8482
await totalist(cwd, (rel, abs) => {
8583
if (ignore.test(abs)) return;
8684
if (!/\.html?/.test(rel)) return;
87-
input.push('./' + pathToPosix(relative(root, abs)));
85+
input.push('./' + normalizePath(relative(root, abs)));
8886
});
8987

9088
const bundle = await rollup.rollup({
@@ -150,7 +148,7 @@ export async function bundleProd({
150148
plugins: [minify && terser({ compress: true, sourcemap })],
151149
sourcemap,
152150
sourcemapPathTransform(p, mapPath) {
153-
let url = pathToPosix(relative(cwd, resolve(dirname(mapPath), p)));
151+
let url = normalizePath(relative(cwd, resolve(dirname(mapPath), p)));
154152
// strip leading relative path
155153
url = url.replace(/^\.\//g, '');
156154
// replace internal npm prefix

packages/wmr/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { normalizePath } from './utils.js';

packages/wmr/src/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import path from 'path';
2+
3+
/**
4+
* Normalize a file path across OSes.
5+
* @param {string} file
6+
* @returns {string}
7+
*/
8+
export const normalizePath = file => file.split(path.sep).join(path.posix.sep);

packages/wmr/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ declare module 'wmr' {
3232
output: OutputOption[];
3333
features: Features;
3434
}
35+
36+
export function normalizePath(path: string): string;
3537
}
3638

3739
// Declarations used by WMR-based applications

0 commit comments

Comments
 (0)