From cfc9c3e10dece10b8c0e74946e9bf412411c85dc Mon Sep 17 00:00:00 2001 From: Linghanchujian <2739069093@qq.com> Date: Thu, 28 Oct 2021 22:33:57 +0800 Subject: [PATCH] fix: modules uses the wrong path, close #33 (#35) --- src/webpack/index.ts | 19 +++++++------------ src/webpack/loaders/load.ts | 3 ++- src/webpack/utils.ts | 7 +++++++ 3 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 src/webpack/utils.ts diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 37a69b7..ad4d296 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -4,15 +4,12 @@ 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' const _dirname = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url)) const TRANSFORM_LOADER = resolve(_dirname, 'webpack/loaders/transform.js') const LOAD_LOADER = resolve(_dirname, 'webpack/loaders/load.js') -function slash (path: string) { - return path.replace(/\\/g, '/') -} - export function getWebpackPlugin ( factory: UnpluginFactory ): UnpluginInstance['webpack'] { @@ -26,10 +23,7 @@ export function getWebpackPlugin ( } } - let virtualModulePrefix = slash(resolve(process.cwd(), '_virtual_')) - if (!virtualModulePrefix.startsWith('/')) { - virtualModulePrefix = '/' + virtualModulePrefix - } + const virtualModulePrefix = resolve(process.cwd(), '_virtual_') const rawPlugin = factory(userOptions, meta) const plugin = Object.assign( @@ -59,7 +53,7 @@ export function getWebpackPlugin ( return false } if (plugin.transformInclude) { - return plugin.transformInclude(slash(id)) + return plugin.transformInclude(id) } else { return true } @@ -92,7 +86,7 @@ export function getWebpackPlugin ( return callback() } - const id = slash(request.request) + const id = backSlash(request.request) // filter out invalid requests if (id.startsWith(plugin.__virtualModulePrefix)) { @@ -100,7 +94,7 @@ export function getWebpackPlugin ( } // call hook - const result = await plugin.resolveId!(id) + const result = await plugin.resolveId!(slash(id)) if (result == null) { return callback() } @@ -113,6 +107,7 @@ export function getWebpackPlugin ( // we treat it as a virtual module if (!fs.existsSync(resolved)) { resolved = plugin.__virtualModulePrefix + id + // webapck virtual module should pass in the correct path plugin.__vfs!.writeModule(resolved, '') plugin.__vfsModules!.add(resolved) } @@ -141,7 +136,7 @@ export function getWebpackPlugin ( if (plugin.load && plugin.__vfsModules) { compiler.options.module.rules.push({ include (id) { - return id != null && plugin.__vfsModules!.has(slash(id)) + return id != null && plugin.__vfsModules!.has(id) }, enforce: plugin.enforce, use: [{ diff --git a/src/webpack/loaders/load.ts b/src/webpack/loaders/load.ts index 59befac..b9cc7ff 100644 --- a/src/webpack/loaders/load.ts +++ b/src/webpack/loaders/load.ts @@ -1,5 +1,6 @@ import type { LoaderContext } from 'webpack' import { UnpluginContext } from '../../types' +import { slash } from '../utils' export default async function load (this: LoaderContext, source: string, map: any) { const callback = this.async() @@ -20,7 +21,7 @@ export default async function load (this: LoaderContext, source: string, ma id = id.slice(plugin.__virtualModulePrefix.length) } - const res = await plugin.load.call(context, id) + const res = await plugin.load.call(context, slash(id)) if (res == null) { callback(null, source, map) diff --git a/src/webpack/utils.ts b/src/webpack/utils.ts new file mode 100644 index 0000000..3295f93 --- /dev/null +++ b/src/webpack/utils.ts @@ -0,0 +1,7 @@ +export function slash (path: string) { + return path.replace(/\\/g, '/') +} + +export function backSlash (path: string) { + return path.replace(/\//g, '\\') +}