Skip to content

Commit

Permalink
Allow plugins to add and change assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 9, 2022
1 parent cc8d5f3 commit 1fbe2a6
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 18 deletions.
6 changes: 4 additions & 2 deletions browser/src/resolveId.ts
Expand Up @@ -12,7 +12,8 @@ export async function resolveId(
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
isEntry: boolean,
assertions: Record<string, string>
): Promise<ResolveIdResult> {
const pluginResult = await resolveIdViaPlugins(
source,
Expand All @@ -21,7 +22,8 @@ export async function resolveId(
moduleLoaderResolveId,
skip,
customOptions,
isEntry
isEntry,
assertions
);
if (pluginResult == null) {
throwNoFileSystem('path.resolve');
Expand Down
13 changes: 7 additions & 6 deletions src/ModuleLoader.ts
Expand Up @@ -213,7 +213,8 @@ export class ModuleLoader {
this.resolveId,
skip,
customOptions,
typeof isEntry === 'boolean' ? isEntry : !importer
typeof isEntry === 'boolean' ? isEntry : !importer,
assertions
),
importer,
source
Expand Down Expand Up @@ -562,9 +563,8 @@ export class ModuleLoader {
return null;
}
const external = resolvedId.external || false;
// TODO Lukas also consider assertions from resolvedId
return {
assertions,
assertions: resolvedId.assertions || assertions,
external,
id: resolvedId.id,
meta: resolvedId.meta || {},
Expand Down Expand Up @@ -631,7 +631,8 @@ export class ModuleLoader {
this.resolveId,
null,
EMPTY_OBJECT,
true
true,
EMPTY_OBJECT
);
if (resolveIdResult == null) {
return error(
Expand All @@ -651,7 +652,6 @@ export class ModuleLoader {
);
}
return this.fetchModule(
// TODO Lukas use correct assertions from input
this.getResolvedIdWithDefaults(
typeof resolveIdResult === 'object'
? (resolveIdResult as NormalizedResolveIdWithoutDefaults)
Expand All @@ -672,7 +672,8 @@ export class ModuleLoader {
): Promise<ResolvedId | string | null> {
const resolution = await this.pluginDriver.hookFirst('resolveDynamicImport', [
specifier,
importer
importer,
{ assertions }
]);
if (typeof specifier !== 'string') {
if (typeof resolution === 'string') {
Expand Down
5 changes: 3 additions & 2 deletions src/rollup/types.d.ts
Expand Up @@ -221,7 +221,7 @@ export type ResolveIdHook = (
this: PluginContext,
source: string,
importer: string | undefined,
options: { custom?: CustomPluginOptions; isEntry: boolean }
options: { assertions: Record<string, string>; custom?: CustomPluginOptions; isEntry: boolean }
) => ResolveIdResult;

export type ShouldTransformCachedModuleHook = (
Expand Down Expand Up @@ -276,7 +276,8 @@ export type RenderChunkHook = (
export type ResolveDynamicImportHook = (
this: PluginContext,
specifier: string | AcornNode,
importer: string
importer: string,
options: { assertions: Record<string, string> }
) => ResolveIdResult;

export type ResolveImportMetaHook = (
Expand Down
6 changes: 4 additions & 2 deletions src/utils/resolveId.ts
Expand Up @@ -13,7 +13,8 @@ export async function resolveId(
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
isEntry: boolean,
assertions: Record<string, string>
): Promise<ResolveIdResult> {
const pluginResult = await resolveIdViaPlugins(
source,
Expand All @@ -22,7 +23,8 @@ export async function resolveId(
moduleLoaderResolveId,
skip,
customOptions,
isEntry
isEntry,
assertions
);
if (pluginResult != null) return pluginResult;

Expand Down
5 changes: 3 additions & 2 deletions src/utils/resolveIdViaPlugins.ts
Expand Up @@ -10,7 +10,8 @@ export function resolveIdViaPlugins(
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
isEntry: boolean,
assertions: Record<string, string>
): Promise<ResolveIdResult> {
let skipped: Set<Plugin> | null = null;
let replaceContext: ReplaceContext | null = null;
Expand Down Expand Up @@ -38,7 +39,7 @@ export function resolveIdViaPlugins(
}
return pluginDriver.hookFirst(
'resolveId',
[source, importer, { custom: customOptions, isEntry }],
[source, importer, { assertions, custom: customOptions, isEntry }],
replaceContext,
skipped
);
Expand Down
@@ -1,5 +1,4 @@
module.exports = {
// solo: true,
description: 'keep import assertions for dynamic imports',
expectedWarnings: 'UNRESOLVED_IMPORT',
options: {
Expand Down
@@ -1,5 +1,4 @@
module.exports = {
// solo: true,
description: 'keeps any import assertions on input',
expectedWarnings: 'UNRESOLVED_IMPORT',
options: {
Expand Down
@@ -0,0 +1,22 @@
module.exports = {
description: 'allows plugins to read and write import assertions in resolveDynamicImport',
options: {
plugins: [
{
resolveDynamicImport(specifier, importer, { assertions }) {
const resolutionOptions = {
external: true,
assertions: Object.fromEntries(Object.keys(assertions).map(key => [key, 'changed']))
};
if (typeof specifier === 'object') {
if (specifier.type === 'TemplateLiteral') {
return { id: 'resolved-a', ...resolutionOptions };
}
return { id: 'resolved-b', ...resolutionOptions };
}
return { id: specifier, ...resolutionOptions };
}
}
]
}
};
@@ -0,0 +1,4 @@
import('a', { assert: { type: 'changed' } });
import('resolved-b', { assert: { type: 'changed', extra: 'changed' } });
import('b');
import('resolved-a');
@@ -0,0 +1,4 @@
import('a', { assert: { type: 'special' } });
import(globalThis.unknown, { assert: { type: 'special', extra: 'value' } });
import('b');
import(`external-${globalThis.unknown}`);
@@ -0,0 +1,17 @@
module.exports = {
description: 'allows plugins to read and write import assertions in resolveId',
options: {
output: { name: 'bundle' },
plugins: [
{
resolveId(source, importer, { assertions, isEntry }) {
return {
id: source,
external: !isEntry,
assertions: Object.fromEntries(Object.keys(assertions).map(key => [key, 'changed']))
};
}
}
]
}
};
@@ -0,0 +1,9 @@
import { a } from 'a' assert { type: 'changed', extra: 'changed' };
import * as b from 'b' assert { type: 'changed' };
export { c } from 'c' assert { type: 'changed' };
export * from 'd' assert { type: 'changed' };
import 'e';

console.log(a, b, d);
import('f', { assert: { type: 'changed' } });
import('g');
@@ -0,0 +1,9 @@
import { a } from 'a' assert { type: 'a', extra: 'extra' };
import * as b from 'b' assert { type: 'b' };
export { c } from 'c' assert { type: 'c' };
export * from 'd' assert { type: 'd' };
import 'e';

console.log(a, b, d);
import('f', { assert: { type: 'f' } });
import('g');
@@ -1,5 +1,4 @@
module.exports = {
// solo: true,
description: 'keep import assertions for dynamic imports',
expectedWarnings: 'UNRESOLVED_IMPORT',
options: {
Expand Down
@@ -1,5 +1,4 @@
module.exports = {
// solo: true,
description: 'keeps any import assertions on input',
expectedWarnings: 'UNRESOLVED_IMPORT',
options: {
Expand Down

0 comments on commit 1fbe2a6

Please sign in to comment.