Skip to content

Commit 919a9ff

Browse files
committed
fix(core): Resolve issue wih invalid module prefix
1 parent cd3d930 commit 919a9ff

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

packages/core/src/lib/unplugin/module-resolution.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import { isSetObject } from "@stryke/type-checks/is-set-object";
2020
import { isSetString } from "@stryke/type-checks/is-set-string";
2121
import { LoadResult } from "rollup";
2222
import type {
23-
ExternalIdResult,
2423
UnpluginBuildContext,
2524
UnpluginContext,
2625
UnpluginOptions
2726
} from "unplugin";
2827
import { UNSAFE_PluginContext } from "../../types/_internal";
29-
import { PluginContext } from "../../types/context";
28+
import { PluginContext, ResolveResult } from "../../types/context";
3029

3130
export interface CreateUnpluginModuleResolutionFunctionsOptions {
3231
/**
@@ -75,7 +74,7 @@ export function createUnpluginModuleResolutionFunctions<
7574
opts: {
7675
isEntry: boolean;
7776
} = { isEntry: false }
78-
): Promise<string | ExternalIdResult | null | undefined> {
77+
): Promise<string | ResolveResult | null | undefined> {
7978
let result = await ctx.$$internal.callHook(
8079
"resolveId",
8180
{
@@ -88,11 +87,11 @@ export function createUnpluginModuleResolutionFunctions<
8887
opts
8988
);
9089
if (isSetString(result)) {
91-
return `${prefix}${result}`;
90+
return result;
9291
} else if (isSetObject(result)) {
9392
return {
9493
...result,
95-
id: `${prefix}${result.id}`
94+
id: result.virtual ? `${prefix}${result.id}` : result.id
9695
};
9796
}
9897

@@ -108,21 +107,19 @@ export function createUnpluginModuleResolutionFunctions<
108107
opts
109108
);
110109
if (isSetString(result)) {
111-
return `${prefix}${result}`;
110+
return result;
112111
} else if (isSetObject(result)) {
113112
return {
114113
...result,
115-
id: `${prefix}${result.id}`
114+
id: result.virtual ? `${prefix}${result.id}` : result.id
116115
};
117116
}
118117

119118
result = await ctx.resolve(id, importer, { isFile: true, ...opts });
120-
if (isSetString(result)) {
121-
return `${prefix}${result}`;
122-
} else if (isSetObject(result)) {
119+
if (isSetObject(result)) {
123120
return {
124121
...result,
125-
id: `${prefix}${result.id}`
122+
id: result.virtual ? `${prefix}${result.id}` : result.id
126123
};
127124
}
128125

@@ -138,11 +135,11 @@ export function createUnpluginModuleResolutionFunctions<
138135
opts
139136
);
140137
if (isSetString(result)) {
141-
return `${prefix}${result}`;
138+
return result;
142139
} else if (isSetObject(result)) {
143140
return {
144141
...result,
145-
id: `${prefix}${result.id}`
142+
id: result.virtual ? `${prefix}${result.id}` : result.id
146143
};
147144
}
148145

packages/core/src/types/context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ export interface EmitOptions extends WriteOptions {
193193
export type EmitEntryOptions = EmitOptions &
194194
Omit<ResolvedEntryTypeDefinition, "file">;
195195

196+
export interface ResolveResult extends ExternalIdResult {
197+
/**
198+
* A flag indicating whether the resolved module is part of the generated runtime modules
199+
*/
200+
virtual?: boolean;
201+
}
202+
196203
/**
197204
* The unresolved Powerlines context.
198205
*
@@ -423,7 +430,7 @@ export interface UnresolvedContext<
423430
id: string,
424431
importer?: string,
425432
options?: ResolveOptions
426-
) => Promise<ExternalIdResult | undefined>;
433+
) => Promise<ResolveResult | undefined>;
427434

428435
/**
429436
* A helper function to load modules using the Jiti resolver

packages/core/src/types/plugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import type { ArrayValues } from "@stryke/types/array";
2020
import type { AnyFunction, MaybePromise } from "@stryke/types/base";
2121
import { LoadResult } from "rollup";
22-
import type { ExternalIdResult, HookFilter, TransformResult } from "unplugin";
22+
import type { HookFilter, TransformResult } from "unplugin";
2323
import {
2424
KNOWN_PLUGIN_FIELDS,
2525
PLUGIN_NON_HOOK_FIELDS
@@ -34,6 +34,7 @@ import type {
3434
import type {
3535
BuildPluginContext,
3636
PluginContext,
37+
ResolveResult,
3738
UnresolvedContext
3839
} from "./context";
3940
import type { BuilderVariant, UnpluginBuilderVariant } from "./unplugin";
@@ -192,7 +193,7 @@ export interface Hooks<TContext extends PluginContext> {
192193
id: string,
193194
importer: string | undefined,
194195
options: { isEntry: boolean }
195-
) => MaybePromise<string | ExternalIdResult | null | undefined>;
196+
) => MaybePromise<string | ResolveResult | null | undefined>;
196197

197198
/**
198199
* A hook that is called to write the bundle to disk.

packages/powerlines/src/context/context.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ import {
6262
Response,
6363
setGlobalDispatcher
6464
} from "undici";
65-
import {
66-
ExternalIdResult,
67-
UnpluginBuildContext,
68-
UnpluginMessage
69-
} from "unplugin";
65+
import { UnpluginBuildContext, UnpluginMessage } from "unplugin";
7066
import { getPrefixedRootHash } from "../_internal/helpers/meta";
7167
import {
7268
createResolver,
@@ -101,6 +97,7 @@ import type {
10197
ResolvedEntryTypeDefinition,
10298
ResolveOptions,
10399
Resolver,
100+
ResolveResult,
104101
TransformResult,
105102
VirtualFile,
106103
VirtualFileSystemInterface,
@@ -792,7 +789,7 @@ export class PowerlinesContext<
792789
id: string,
793790
importer?: string,
794791
options: ResolveOptions = {}
795-
): Promise<ExternalIdResult | undefined> {
792+
): Promise<ResolveResult | undefined> {
796793
let moduleId = id;
797794
if (this.config.resolve.alias) {
798795
if (Array.isArray(this.config.resolve.alias)) {
@@ -841,7 +838,8 @@ export class PowerlinesContext<
841838
(this.fs.isVirtual(moduleId) &&
842839
this.config.projectType !== "application") ||
843840
(this.config.resolve.skipNodeModulesBundle &&
844-
!/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId)))
841+
!/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId))),
842+
virtual: this.fs.isVirtual(moduleId)
845843
};
846844
}
847845

@@ -857,14 +855,15 @@ export class PowerlinesContext<
857855
match(moduleId, this.config.resolve.external) ||
858856
moduleId.startsWith("node:")
859857
) {
860-
return { id: moduleId, external: true };
858+
return { id: moduleId, external: true, virtual: false };
861859
}
862860

863861
// Exclude any other import that looks like a Node module
864862
if (!/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId)) {
865863
return {
866864
id: moduleId,
867-
external: true
865+
external: true,
866+
virtual: false
868867
};
869868
}
870869
} else {
@@ -876,7 +875,7 @@ export class PowerlinesContext<
876875
match(moduleId, this.config.resolve.external) ||
877876
moduleId.startsWith("node:")
878877
) {
879-
return { id: moduleId, external: true };
878+
return { id: moduleId, external: true, virtual: false };
880879
}
881880
}
882881

0 commit comments

Comments
 (0)