Skip to content

Commit

Permalink
fix: remove query namespace and pluginData in onResolve hook, use suf…
Browse files Browse the repository at this point in the history
…fix instead
  • Loading branch information
10Derozan committed Sep 19, 2023
1 parent 79bc57b commit 71161c8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
17 changes: 5 additions & 12 deletions packages/solutions/module-tools/src/builder/esbuild/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /#[^#]+$/;
Expand Down Expand Up @@ -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 => {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ async function redirectImport(
aliasRecord: Record<string, string>,
filePath: string,
outputDir: string,
pluginData: any,
matchPath?: MatchPath,
): Promise<MagicString> {
const str: MagicString = new MagicString(code);
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -246,7 +245,6 @@ export const redirect = {
absoluteAlias,
id,
dirname(outputPath),
pluginData,
matchPath,
);
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand All @@ -29,7 +31,8 @@ export const css = {
async (source): Promise<Source> => {
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;
Expand Down
3 changes: 1 addition & 2 deletions packages/solutions/module-tools/src/types/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SourceMap {

export type LoadResult = Pick<
OnLoadResult,
'contents' | 'loader' | 'resolveDir' | 'pluginData'
'contents' | 'loader' | 'resolveDir'
> & {
map?: SourceMap;
};
Expand Down Expand Up @@ -103,7 +103,6 @@ export type Source = {
map?: SourceMap;
path: string;
loader?: string;
pluginData: any;
};

export interface CacheValue extends Source {
Expand Down
2 changes: 2 additions & 0 deletions packages/solutions/module-tools/src/utils/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query = Record<string, string | boolean>;
type ResolveResult = {
originalFilePath: string;
query: Query;
rawQuery?: string;
};
export const getAllDeps = async <T>(
appDirectory: string,
Expand Down Expand Up @@ -88,5 +89,6 @@ export const resolvePathAndQuery = (originalPath: string): ResolveResult => {
return {
query,
originalFilePath: filePath,
rawQuery: queryStr,
};
};

0 comments on commit 71161c8

Please sign in to comment.