From 71161c8a8371f7b2f6e017b2efe71eb07c5c2aee Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Tue, 19 Sep 2023 20:43:55 +0800 Subject: [PATCH] fix: remove query namespace and pluginData in onResolve hook, use suffix instead --- .../module-tools/src/builder/esbuild/adapter.ts | 17 +++++------------ .../src/builder/feature/redirect.ts | 4 +--- .../src/builder/feature/style/index.ts | 11 +++++++---- .../solutions/module-tools/src/types/esbuild.ts | 3 +-- .../solutions/module-tools/src/utils/builder.ts | 2 ++ 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/solutions/module-tools/src/builder/esbuild/adapter.ts b/packages/solutions/module-tools/src/builder/esbuild/adapter.ts index 5c4394417364..2c8e0e98c721 100644 --- a/packages/solutions/module-tools/src/builder/esbuild/adapter.ts +++ b/packages/solutions/module-tools/src/builder/esbuild/adapter.ts @@ -28,11 +28,6 @@ import { initWatcher } from './watch'; */ const globalNamespace = 'globals'; -/** - * some path have query string, we transform it to pluginData - */ -const queryNamespace = 'query'; - const HTTP_PATTERNS = /^(https?:)?\/\//; const DATAURL_PATTERNS = /^data:/; const HASH_PATTERNS = /#[^#]+$/; @@ -191,21 +186,20 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => { : compiler.node_resolve(id, dir, kind); }; - const { originalFilePath, query } = resolvePathAndQuery(args.path); + const { originalFilePath, rawQuery } = resolvePathAndQuery(args.path); + const suffix = (rawQuery ?? '').length > 0 ? `?${rawQuery}` : ''; const isExternal = getIsExternal(originalFilePath); const dir = args.resolveDir ?? (args.importer ? dirname(args.importer) : root); const sideEffects = await getSideEffects(originalFilePath, isExternal); - const namespace = - Object.keys(query).length > 0 ? queryNamespace : 'file'; return { path: isExternal ? args.path : getResultPath(originalFilePath, dir, args.kind), external: isExternal, - namespace: isExternal ? undefined : namespace, + namespace: isExternal ? undefined : 'file', sideEffects, - pluginData: query, + suffix, }; }); build.onLoad({ filter: /.*/ }, async args => { @@ -224,7 +218,7 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => { args.path += args.suffix; } - if (args.namespace !== 'file' && args.namespace !== queryNamespace) { + if (args.namespace !== 'file') { return; } @@ -253,7 +247,6 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => { code: result.contents.toString(), path: args.path, loader: result.loader, - pluginData: args.pluginData, }); const ext = extname(args.path); diff --git a/packages/solutions/module-tools/src/builder/feature/redirect.ts b/packages/solutions/module-tools/src/builder/feature/redirect.ts index d43070e99fc4..2c822466cde0 100644 --- a/packages/solutions/module-tools/src/builder/feature/redirect.ts +++ b/packages/solutions/module-tools/src/builder/feature/redirect.ts @@ -39,7 +39,6 @@ async function redirectImport( aliasRecord: Record, filePath: string, outputDir: string, - pluginData: any, matchPath?: MatchPath, ): Promise { const str: MagicString = new MagicString(code); @@ -177,7 +176,7 @@ export const redirect = { if (!isJsExt(args.path) && !isJsLoader(args.loader)) { return args; } - const { code, path: id, pluginData } = args; + const { code, path: id } = args; const { format, alias, sourceDir, outDir } = compiler.config; if (!code || format === 'iife' || format === 'umd') { @@ -246,7 +245,6 @@ export const redirect = { absoluteAlias, id, dirname(outputPath), - pluginData, matchPath, ); return { diff --git a/packages/solutions/module-tools/src/builder/feature/style/index.ts b/packages/solutions/module-tools/src/builder/feature/style/index.ts index 5a12ddf50fc9..4a59fc5c85fd 100644 --- a/packages/solutions/module-tools/src/builder/feature/style/index.ts +++ b/packages/solutions/module-tools/src/builder/feature/style/index.ts @@ -1,6 +1,6 @@ import { readFileSync } from 'fs'; import { identifier } from 'safe-identifier'; -import { isStyleExt } from '../../../utils'; +import { isStyleExt, resolvePathAndQuery } from '../../../utils'; import { Source, ICompiler } from '../../../types'; import { transformStyle } from './transformStyle'; @@ -10,8 +10,10 @@ export const css = { hooks(compiler: ICompiler) { compiler.hooks.load.tapPromise({ name }, async args => { if (isStyleExt(args.path)) { - if (args.pluginData?.css_virtual) { - const key = args.pluginData.hash as string; + const { query } = resolvePathAndQuery(args.path); + + if (query?.css_virtual) { + const key = query.hash as string; const contents = compiler.virtualModule.get(key)!; return { contents, @@ -29,7 +31,8 @@ export const css = { async (source): Promise => { if (isStyleExt(source.path)) { let { code, loader = 'css' } = source; - if (!source.pluginData?.css_virtual) { + const { query } = resolvePathAndQuery(source.path); + if (!query?.css_virtual) { ({ code, loader } = await transformStyle.apply(compiler, [source])); } const { style, buildType } = compiler.config; diff --git a/packages/solutions/module-tools/src/types/esbuild.ts b/packages/solutions/module-tools/src/types/esbuild.ts index 9b10381d38bd..b8eade3a4ad0 100644 --- a/packages/solutions/module-tools/src/types/esbuild.ts +++ b/packages/solutions/module-tools/src/types/esbuild.ts @@ -21,7 +21,7 @@ export interface SourceMap { export type LoadResult = Pick< OnLoadResult, - 'contents' | 'loader' | 'resolveDir' | 'pluginData' + 'contents' | 'loader' | 'resolveDir' > & { map?: SourceMap; }; @@ -103,7 +103,6 @@ export type Source = { map?: SourceMap; path: string; loader?: string; - pluginData: any; }; export interface CacheValue extends Source { diff --git a/packages/solutions/module-tools/src/utils/builder.ts b/packages/solutions/module-tools/src/utils/builder.ts index 0a260aebde12..399fb979f231 100644 --- a/packages/solutions/module-tools/src/utils/builder.ts +++ b/packages/solutions/module-tools/src/utils/builder.ts @@ -8,6 +8,7 @@ type Query = Record; type ResolveResult = { originalFilePath: string; query: Query; + rawQuery?: string; }; export const getAllDeps = async ( appDirectory: string, @@ -88,5 +89,6 @@ export const resolvePathAndQuery = (originalPath: string): ResolveResult => { return { query, originalFilePath: filePath, + rawQuery: queryStr, }; };