Skip to content

Commit

Permalink
Expose normalizePath helper
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 17, 2021
1 parent 88ed7e6 commit 9d6ae35
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/new-falcons-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@wmr-plugins/directory-import': patch
'@wmr-plugins/service-worker': patch
'wmr': patch
---

Expose `normalizePath` function from `wmr` to ensure normalization is applied the same way across plugins and core.
5 changes: 2 additions & 3 deletions packages/directory-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { promises as fs } from 'fs';
import { normalizePath } from 'wmr';
import path from 'path';

const pathToPosix = p => p.split(path.sep).join(path.posix.sep);

/**
* @param {object} [options]
* @param {string} [options.cwd]
Expand All @@ -17,7 +16,7 @@ function directoryPlugin(options) {
const resolved = await this.resolve(id.slice(4) + '\0', importer, { skipSelf: true });

if (resolved) {
return '\0dir:' + pathToPosix(resolved.id).replace(/\0$/, '');
return '\0dir:' + normalizePath(resolved.id).replace(/\0$/, '');
}
},
async load(id) {
Expand Down
7 changes: 3 additions & 4 deletions packages/sw-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path';
import { request } from 'http';

const pathToPosix = p => p.split(path.sep).join(path.posix.sep);
import { normalizePath } from 'wmr';

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

const wmrProxyPlugin = {
resolveId(id) {
const normalizedId = id[1] + id[2] === ':\\' ? pathToPosix(id.slice(2)) : id;
const normalizedId = id[1] + id[2] === ':\\' ? normalizePath(id.slice(2)) : id;
if (id.startsWith('/@npm/')) return id;
if (!/^\.*\//.test(normalizedId)) return '/@npm/' + id;
},
Expand Down Expand Up @@ -49,7 +48,7 @@ export default function swPlugin(options) {
async resolveId(id, importer) {
if (!id.startsWith('sw:')) return;
const resolved = await this.resolve(id.slice(3), importer);
if (resolved) return `\0sw:${pathToPosix(resolved.id)}`;
if (resolved) return `\0sw:${normalizePath(resolved.id)}`;
},
async load(id) {
if (!id.startsWith('\0sw:')) return;
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "wmr",
"version": "1.3.2",
"bin": "wmr.cjs",
"main": "./src/index.js",
"type": "module",
"scripts": {
"demo": "cd demo && node --experimental-modules ../src/cli.js 2>&1",
Expand Down
10 changes: 4 additions & 6 deletions packages/wmr/src/bundler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { relative, sep, posix, resolve, dirname } from 'path';
import { relative, posix, resolve, dirname } from 'path';
import * as rollup from 'rollup';
import htmPlugin from './plugins/htm-plugin.js';
import sucrasePlugin from './plugins/sucrase-plugin.js';
import wmrPlugin from './plugins/wmr/plugin.js';
import wmrStylesPlugin from './plugins/wmr/styles-plugin.js';
import { normalizePath } from './utils.js';
import sassPlugin from './plugins/sass-plugin.js';
import terser from './plugins/fast-minify.js';
import npmPlugin from './plugins/npm-plugin/index.js';
Expand All @@ -24,9 +25,6 @@ import copyAssetsPlugin from './plugins/copy-assets-plugin.js';
import nodeBuiltinsPlugin from './plugins/node-builtins-plugin.js';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';

/** @param {string} p */
const pathToPosix = p => p.split(sep).join(posix.sep);

/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {OutputOptions | ((opts: OutputOptions) => OutputOptions)} Output */

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

const bundle = await rollup.rollup({
Expand Down Expand Up @@ -150,7 +148,7 @@ export async function bundleProd({
plugins: [minify && terser({ compress: true, sourcemap })],
sourcemap,
sourcemapPathTransform(p, mapPath) {
let url = pathToPosix(relative(cwd, resolve(dirname(mapPath), p)));
let url = normalizePath(relative(cwd, resolve(dirname(mapPath), p)));
// strip leading relative path
url = url.replace(/^\.\//g, '');
// replace internal npm prefix
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { normalizePath } from './utils.js';
8 changes: 8 additions & 0 deletions packages/wmr/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from 'path';

/**
* Normalize a file path across OSes.
* @param {string} file
* @returns {string}
*/
export const normalizePath = file => file.split(path.sep).join(path.posix.sep);
2 changes: 2 additions & 0 deletions packages/wmr/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ declare module 'wmr' {
output: OutputOption[];
features: Features;
}

export function normalizePath(path: string): string;
}

// Declarations used by WMR-based applications
Expand Down

0 comments on commit 9d6ae35

Please sign in to comment.