From 7bd82ee0ce4c4e4052f895d68476d8f4398fc2a6 Mon Sep 17 00:00:00 2001 From: liuq Date: Sat, 7 May 2022 21:44:14 +0800 Subject: [PATCH] fix: keep the same slash with system --- src/webpack/index.ts | 6 +++--- src/webpack/utils.ts | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 0f3133c2..ac6b984a 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -4,7 +4,7 @@ import { resolve, dirname } from 'path' import VirtualModulesPlugin from 'webpack-virtual-modules' import type { Resolver, ResolveRequest } from 'enhanced-resolve' import type { UnpluginContextMeta, UnpluginInstance, UnpluginFactory, WebpackCompiler, ResolvedUnpluginOptions } from '../types' -import { slash, backSlash } from './utils' +import { slash } from './utils' import genContext from './genContext' const _dirname = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url)) const TRANSFORM_LOADER = resolve(_dirname, 'webpack/loaders/transform.js') @@ -86,7 +86,7 @@ export function getWebpackPlugin ( return callback() } - const id = backSlash(request.request) + const id = slash(request.request) // filter out invalid requests if (id.startsWith(plugin.__virtualModulePrefix)) { @@ -106,7 +106,7 @@ export function getWebpackPlugin ( // if the resolved module is not exists, // we treat it as a virtual module if (!fs.existsSync(resolved)) { - resolved = plugin.__virtualModulePrefix + backSlash(resolved) + resolved = plugin.__virtualModulePrefix + slash(resolved) // webpack virtual module should pass in the correct path plugin.__vfs!.writeModule(resolved, '') plugin.__vfsModules!.add(resolved) diff --git a/src/webpack/utils.ts b/src/webpack/utils.ts index 3295f93c..6deec05f 100644 --- a/src/webpack/utils.ts +++ b/src/webpack/utils.ts @@ -1,7 +1,5 @@ -export function slash (path: string) { - return path.replace(/\\/g, '/') -} +import { sep } from 'path' -export function backSlash (path: string) { - return path.replace(/\//g, '\\') +export function slash (path: string) { + return path.replace(/[\\/]/g, sep) }