Skip to content

Commit

Permalink
Working on enhancements for the importmap spec
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Oct 18, 2023
1 parent 753b70c commit 553fea5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/tooling/piral-cli/src/common/importmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { satisfies, validate } from './version';
import { computeHash } from './hash';
import { getHash, readJson, findFile, checkExists, checkIsDirectory } from './io';
import { tryResolvePackage } from './npm';
import { SharedDependency, Importmap, ImportmapVersions } from '../types';
import { SharedDependency, Importmap, ImportmapVersions, ImportmapMode } from '../types';

const shorthandsUrls = ['', '.', '...'];

Expand Down Expand Up @@ -105,13 +105,14 @@ async function getInheritedDependencies(
inheritedImport: string,
dir: string,
excludedDependencies: Array<string>,
inheritanceBehavior: ImportmapMode,
): Promise<Array<SharedDependency>> {
const packageJson = tryResolvePackage(`${inheritedImport}/package.json`, dir);

if (packageJson) {
const packageDir = dirname(packageJson);
const packageDetails = await readJson(packageDir, 'package.json');
return await consumeImportmap(packageDir, packageDetails, true, 'exact', excludedDependencies);
return await consumeImportmap(packageDir, packageDetails, true, 'exact', inheritanceBehavior, excludedDependencies);
} else {
const directImportmap = tryResolvePackage(inheritedImport, dir);

Expand All @@ -120,6 +121,7 @@ async function getInheritedDependencies(
const content = await readJson(baseDir, basename(directImportmap));
return await resolveImportmap(baseDir, content, {
availableSpecs: {},
inheritanceBehavior,
excludedDependencies,
ignoreFailure: true,
versionBehavior: 'exact',
Expand All @@ -134,6 +136,7 @@ interface ImportmapResolutionOptions {
availableSpecs: Record<string, string>;
excludedDependencies: Array<string>;
versionBehavior: ImportmapVersions;
inheritanceBehavior: ImportmapMode;
ignoreFailure: boolean;
}

Expand Down Expand Up @@ -283,7 +286,12 @@ async function resolveImportmap(
const excluded = Array.isArray(excludedImports) ? [...includedImports, ...excludedImports] : includedImports;

for (const inheritedImport of inheritedImports) {
const otherDependencies = await getInheritedDependencies(inheritedImport, dir, excluded);
const otherDependencies = await getInheritedDependencies(
inheritedImport,
dir,
excluded,
options.inheritanceBehavior,
);

for (const dependency of otherDependencies) {
const entry = dependencies.find((dep) => dep.name === dependency.name);
Expand All @@ -307,10 +315,13 @@ async function consumeImportmap(
packageDetails: any,
inherited: boolean,
versionBehavior: ImportmapVersions,
mode: ImportmapMode,
excludedDependencies: Array<string>,
): Promise<Array<SharedDependency>> {
const importmap = packageDetails.importmap;
const availableSpecs = inherited ? packageDetails.devDependencies : {};
const appShell = inherited && mode === 'remote';
const availableSpecs = appShell ? packageDetails.devDependencies : {};
const inheritanceBehavior = appShell ? 'host' : mode;

if (typeof importmap === 'string') {
const notFound = {};
Expand All @@ -326,6 +337,7 @@ async function consumeImportmap(
ignoreFailure: inherited,
excludedDependencies,
versionBehavior,
inheritanceBehavior,
});
} else if (typeof importmap === 'undefined' && inherited) {
// Fall back to sharedDependencies or pilets.external if available
Expand All @@ -348,13 +360,15 @@ async function consumeImportmap(
excludedDependencies,
ignoreFailure: inherited,
versionBehavior,
inheritanceBehavior,
});
}

export function readImportmap(
dir: string,
packageDetails: any,
versionBehavior: ImportmapVersions = 'exact',
inheritanceBehavior: ImportmapMode = 'remote',
): Promise<Array<SharedDependency>> {
return consumeImportmap(dir, packageDetails, false, versionBehavior, []);
return consumeImportmap(dir, packageDetails, false, versionBehavior, inheritanceBehavior, []);
}
8 changes: 4 additions & 4 deletions src/tooling/piral-cli/src/common/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ export function flattenExternals(dependencies: Array<SharedDependency>, disableA
}

export async function retrieveExternals(root: string, packageInfo: any): Promise<Array<SharedDependency>> {
const sharedDependencies = await readImportmap(root, packageInfo);
const importmap = await readImportmap(root, packageInfo, 'exact', 'host');

if (sharedDependencies.length === 0) {
if (importmap.length === 0) {
const allDeps = {
...packageInfo.devDependencies,
...packageInfo.dependencies,
Expand All @@ -577,7 +577,7 @@ export async function retrieveExternals(root: string, packageInfo: any): Promise
}));
}

return sharedDependencies;
return importmap;
}

export async function retrievePiletsInfo(entryFile: string) {
Expand Down Expand Up @@ -819,7 +819,7 @@ export async function retrievePiletData(target: string, app?: string) {
});
}

const importmap = await readImportmap(root, piletPackage, piletDefinition?.importmapVersions);
const importmap = await readImportmap(root, piletPackage, piletDefinition?.importmapVersions, 'remote');

return {
dependencies: piletPackage.dependencies || {},
Expand Down
2 changes: 2 additions & 0 deletions src/tooling/piral-cli/src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export interface BundlerDefinition {

export type ImportmapVersions = 'all' | 'match-major' | 'any-patch' | 'exact';

export type ImportmapMode = 'host' | 'remote';

export type PiletSchemaVersion = 'none' | 'v0' | 'v1' | 'v2' | 'v3';

export type SourceLanguage = 'js' | 'ts';
Expand Down

0 comments on commit 553fea5

Please sign in to comment.