Skip to content

Commit

Permalink
Some extractions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 8, 2022
1 parent 0db8a36 commit cc8d5f3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 57 deletions.
17 changes: 3 additions & 14 deletions browser/src/resolveId.ts
@@ -1,9 +1,5 @@
import type {
CustomPluginOptions,
Plugin,
ResolvedId,
ResolveIdResult
} from '../../src/rollup/types';
import { ModuleLoaderResolveId } from '../../src/ModuleLoader';
import type { CustomPluginOptions, Plugin, ResolveIdResult } from '../../src/rollup/types';
import type { PluginDriver } from '../../src/utils/PluginDriver';
import { resolveIdViaPlugins } from '../../src/utils/resolveIdViaPlugins';
import { throwNoFileSystem } from './error';
Expand All @@ -13,14 +9,7 @@ export async function resolveId(
importer: string | undefined,
_preserveSymlinks: boolean,
pluginDriver: PluginDriver,
moduleLoaderResolveId: (
source: string,
importer: string | undefined,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean | undefined,
assertions: Record<string, string>,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null
) => Promise<ResolvedId | null>,
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
Expand Down
36 changes: 21 additions & 15 deletions src/ModuleLoader.ts
Expand Up @@ -43,6 +43,15 @@ export interface UnresolvedModule {
name: string | null;
}

export type ModuleLoaderResolveId = (
source: string,
importer: string | undefined,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean | undefined,
assertions: Record<string, string>,
skip?: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null
) => Promise<ResolvedId | null>;

type NormalizedResolveIdWithoutDefaults = Partial<PartialNull<ModuleOptions>> & {
external?: boolean | 'absolute';
id: string;
Expand Down Expand Up @@ -184,14 +193,14 @@ export class ModuleLoader {
return module.info;
}

resolveId = async (
source: string,
importer: string | undefined,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean | undefined,
assertions: Record<string, string>,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null = null
): Promise<ResolvedId | null> =>
resolveId: ModuleLoaderResolveId = async (
source,
importer,
customOptions,
isEntry,
assertions,
skip = null
) =>
this.getResolvedIdWithDefaults(
this.getNormalizedResolvedIdWithoutDefaults(
this.options.external(source, importer, false)
Expand Down Expand Up @@ -672,13 +681,10 @@ export class ModuleLoader {
if (!resolution) {
return null;
}
// TODO Lukas use withDefaults logic instead? Merge with other resolution case?
return {
assertions,
external: false,
moduleSideEffects: true,
...resolution
} as ResolvedId;
return this.getResolvedIdWithDefaults(
resolution as NormalizedResolveIdWithoutDefaults,
assertions
);
}
if (resolution == null) {
// TODO Lukas handle existing resolved id conflicts
Expand Down
2 changes: 1 addition & 1 deletion src/utils/PluginContext.ts
Expand Up @@ -99,7 +99,7 @@ export function getPluginContext(
importer,
custom,
isEntry,
// TODO Lukas use correct assertions
// TODO Lukas allow to provide assertions
EMPTY_OBJECT,
skipSelf ? [{ importer, plugin, source }] : null
);
Expand Down
1 change: 0 additions & 1 deletion src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -48,7 +48,6 @@ export function normalizeOutputOptions(
esModule: config.esModule ?? 'if-default-prop',
exports: getExports(config, unsetOptions),
extend: config.extend || false,
// TODO Lukas for truly dynamic imports, setting `false` should still prevent assertions
externalImportAssertions: config.externalImportAssertions ?? true,
externalLiveBindings: config.externalLiveBindings ?? true,
file,
Expand Down
13 changes: 3 additions & 10 deletions src/utils/resolveId.ts
@@ -1,4 +1,5 @@
import type { CustomPluginOptions, Plugin, ResolvedId, ResolveIdResult } from '../rollup/types';
import { ModuleLoaderResolveId } from '../ModuleLoader';
import type { CustomPluginOptions, Plugin, ResolveIdResult } from '../rollup/types';
import type { PluginDriver } from './PluginDriver';
import { promises as fs } from './fs';
import { basename, dirname, isAbsolute, resolve } from './path';
Expand All @@ -9,15 +10,7 @@ export async function resolveId(
importer: string | undefined,
preserveSymlinks: boolean,
pluginDriver: PluginDriver,
// TODO Lukas extract/reuse type
moduleLoaderResolveId: (
source: string,
importer: string | undefined,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean | undefined,
assertions: Record<string, string>,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null
) => Promise<ResolvedId | null>,
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
Expand Down
19 changes: 3 additions & 16 deletions src/utils/resolveIdViaPlugins.ts
@@ -1,26 +1,13 @@
import type {
CustomPluginOptions,
Plugin,
PluginContext,
ResolvedId,
ResolveIdResult
} from '../rollup/types';
import { ModuleLoaderResolveId } from '../ModuleLoader';
import type { CustomPluginOptions, Plugin, PluginContext, ResolveIdResult } from '../rollup/types';
import type { PluginDriver, ReplaceContext } from './PluginDriver';
import { BLANK, EMPTY_OBJECT } from './blank';

export function resolveIdViaPlugins(
source: string,
importer: string | undefined,
pluginDriver: PluginDriver,
// TODO Lukas extract/reuse type
moduleLoaderResolveId: (
source: string,
importer: string | undefined,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean | undefined,
assertions: Record<string, string>,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null
) => Promise<ResolvedId | null>,
moduleLoaderResolveId: ModuleLoaderResolveId,
skip: readonly { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
Expand Down
1 change: 1 addition & 0 deletions test/function/samples/output-options-hook/_config.js
Expand Up @@ -29,6 +29,7 @@ module.exports = {
esModule: 'if-default-prop',
exports: 'auto',
extend: false,
externalImportAssertions: true,
externalLiveBindings: true,
format: 'cjs',
freeze: true,
Expand Down

0 comments on commit cc8d5f3

Please sign in to comment.