@ms-cloudpack/import-map
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -1,2 +0,2 @@ | ||
import type { ResolveMap } from '@ms-cloudpack/package-utilities'; | ||
import type { ResolveMap, ResolveMapEntry } from '@ms-cloudpack/package-utilities'; | ||
import type { ImportMap } from './types/ImportMap.js'; | ||
@@ -6,4 +6,2 @@ import type { PackageImportPaths } from './types/PackageImportPaths.js'; | ||
* Adds hash to import map for a package. | ||
* @param options - Options object containing package name, version, import map, resolve map, and optionally hash | ||
* @param context - Context object containing package import paths | ||
*/ | ||
@@ -13,6 +11,6 @@ export declare function addImportMapHash(options: { | ||
version: string; | ||
hash: string | undefined; | ||
}, context: { | ||
importMap: ImportMap | undefined; | ||
resolveMap: ResolveMap; | ||
hash?: string; | ||
}, context: { | ||
packageImportPaths: PackageImportPaths; | ||
@@ -23,8 +21,11 @@ }): void; | ||
* If hash already exists, replaces it. | ||
* @param url URL to add hash to | ||
* @param packageUrl Package name and version | ||
* @param hash Hash to add | ||
* @returns URL with hash | ||
*/ | ||
export declare function addHashUrl(url: string, packageUrl: string, hash: string): string; | ||
export declare function addHashUrl(params: { | ||
/** URL to add hash to */ | ||
url: string; | ||
/** Package info */ | ||
entry: Pick<ResolveMapEntry, 'name' | 'version'>; | ||
/** Hash to add */ | ||
hash: string; | ||
}): string; | ||
//# sourceMappingURL=addImportMapHash.d.ts.map |
@@ -0,13 +1,12 @@ | ||
import { makeUrl } from '@ms-cloudpack/path-string-parsing'; | ||
/** | ||
* Adds hash to import map for a package. | ||
* @param options - Options object containing package name, version, import map, resolve map, and optionally hash | ||
* @param context - Context object containing package import paths | ||
*/ | ||
export function addImportMapHash(options, context) { | ||
const { packageName, version, importMap, resolveMap, hash = 'pending' } = options; | ||
const { packageImportPaths } = context; | ||
const resolveEntry = resolveMap[packageName]; | ||
const { packageName, version, hash = 'pending' } = options; | ||
const { importMap, resolveMap, packageImportPaths } = context; | ||
if (!importMap) { | ||
return; | ||
} | ||
const resolveEntry = resolveMap[packageName]; | ||
if (!resolveEntry) { | ||
@@ -19,25 +18,23 @@ throw new Error(`Could not find package ${packageName} in the resolve map.`); | ||
const entry = resolveEntry; | ||
const packageUrl = `${entry.name}@${entry.version}`; | ||
const importPaths = packageImportPaths.get(entry.path); | ||
importPaths?.forEach((importPath) => { | ||
importMap.imports[importPath] = addHashUrl(importMap.imports[importPath], packageUrl, hash); | ||
importMap.imports[importPath] = addHashUrl({ url: importMap.imports[importPath], entry, hash }); | ||
}); | ||
} | ||
else if (resolveEntry.scopedVersions?.[version] !== undefined && importMap.scopes) { | ||
else if (resolveEntry.scopedVersions?.[version] && importMap.scopes) { | ||
// Package is scoped | ||
const entry = resolveEntry.scopedVersions?.[version]; | ||
if (!entry) { | ||
throw new Error(`Could not find package ${packageName}@${version} in the resolve map.`); | ||
} | ||
const packageUrl = `${entry.name}@${entry.version}`; | ||
const entry = resolveEntry.scopedVersions[version]; | ||
const importPaths = packageImportPaths.get(entry.path); | ||
const bundleServerUrl = new URL(Object.values(importMap.imports)[0]).origin; | ||
for (const requiredById of Object.keys(entry.requiredBy)) { | ||
const key = `${bundleServerUrl}/${requiredById}/`; | ||
importPaths?.forEach((importPath) => { | ||
if (!importMap.scopes) { | ||
return; | ||
if (importPaths) { | ||
const bundleServerUrl = makeUrl(Object.values(importMap.imports)[0]).origin; | ||
for (const requiredById of Object.keys(entry.requiredBy)) { | ||
const key = `${bundleServerUrl}/${requiredById}/`; | ||
for (const importPath of importPaths) { | ||
importMap.scopes[key][importPath] = addHashUrl({ | ||
url: importMap.scopes[key][importPath], | ||
entry, | ||
hash, | ||
}); | ||
} | ||
importMap.scopes[key][importPath] = addHashUrl(importMap.scopes[key][importPath], packageUrl, hash); | ||
}); | ||
} | ||
} | ||
@@ -49,11 +46,10 @@ } | ||
* If hash already exists, replaces it. | ||
* @param url URL to add hash to | ||
* @param packageUrl Package name and version | ||
* @param hash Hash to add | ||
* @returns URL with hash | ||
*/ | ||
export function addHashUrl(url, packageUrl, hash) { | ||
const search = url.indexOf(packageUrl); | ||
if (search !== -1) { | ||
const hashIndex = search + packageUrl.length; | ||
export function addHashUrl(params) { | ||
// TODO make and use a unified helper | ||
const { url, entry, hash } = params; | ||
const packageId = `${entry.name}@${entry.version}`; | ||
const packageUrlIndex = url.indexOf(packageId); | ||
if (packageUrlIndex !== -1) { | ||
const hashIndex = packageUrlIndex + packageId.length; | ||
const prefix = 'h-'; | ||
@@ -63,8 +59,6 @@ if (url.startsWith(prefix, hashIndex + 1)) { | ||
const hashEnd = url.indexOf('/', hashIndex + prefix.length + 1); | ||
url = url.slice(0, hashIndex) + `/h-${hash}` + url.slice(hashEnd); | ||
return url.slice(0, hashIndex) + `/h-${hash}` + url.slice(hashEnd); | ||
} | ||
else { | ||
// Add hash after name and version | ||
url = url.slice(0, hashIndex) + `/h-${hash}` + url.slice(hashIndex); | ||
} | ||
// Add hash after name and version | ||
return url.slice(0, hashIndex) + `/h-${hash}` + url.slice(hashIndex); | ||
} | ||
@@ -71,0 +65,0 @@ return url; |
@@ -1,13 +0,7 @@ | ||
import type { ResolveMap } from '@ms-cloudpack/package-utilities'; | ||
import type { ImportMap } from './types/ImportMap.js'; | ||
import type { CreateImportMapContext } from './types/CreateImportMapContext.js'; | ||
import type { ImportMap } from './types/ImportMap.js'; | ||
/** | ||
* Given a resolve map and a bundleServerUrl, returns an import map to be used in the browser. | ||
*/ | ||
export declare function createImportMap(options: { | ||
resolveMap: ResolveMap; | ||
bundleServerUrl: string; | ||
sessionVersion?: number; | ||
targetVersions?: Record<string, number>; | ||
}, context: CreateImportMapContext): Promise<ImportMap>; | ||
export declare function createImportMap(context: CreateImportMapContext): Promise<ImportMap>; | ||
//# sourceMappingURL=createImportMap.d.ts.map |
@@ -0,19 +1,16 @@ | ||
import { getExportsMap, isExternalPackage } from '@ms-cloudpack/package-utilities'; | ||
import { merge } from 'merge'; | ||
import { getRuntimeEntryPaths } from './getRuntimeEntryPaths.js'; | ||
import { merge } from 'merge'; | ||
import { getExportsMap, isExternalPackage } from '@ms-cloudpack/package-utilities'; | ||
import { makeUrl } from '@ms-cloudpack/path-string-parsing'; | ||
/** | ||
* Given a resolve map and a bundleServerUrl, returns an import map to be used in the browser. | ||
*/ | ||
export async function createImportMap(options, context) { | ||
const { resolveMap, bundleServerUrl, sessionVersion = 0, targetVersions } = options; | ||
const { packageImportPaths } = context; | ||
export async function createImportMap(context) { | ||
const { resolveMap, urls, packageImportPaths } = context; | ||
const importMap = { | ||
imports: {}, | ||
}; | ||
packageImportPaths?.clear(); | ||
packageImportPaths.clear(); | ||
for (const resolveEntry of Object.values(resolveMap)) { | ||
merge(importMap.imports, await getImportMapFromEntry({ | ||
options: { entry: resolveEntry, bundleServerUrl, targetVersions, sessionVersion }, | ||
context, | ||
})); | ||
merge(importMap.imports, await getImportMapFromEntry(resolveEntry, context)); | ||
if (resolveEntry.scopedVersions) { | ||
@@ -23,8 +20,5 @@ // Initialize scopes | ||
for (const scopedEntry of Object.values(resolveEntry.scopedVersions)) { | ||
const importMapForEntry = await getImportMapFromEntry({ | ||
options: { entry: scopedEntry, bundleServerUrl, sessionVersion, targetVersions }, | ||
context, | ||
}); | ||
const importMapForEntry = await getImportMapFromEntry(scopedEntry, context); | ||
for (const requiredById of Object.keys(scopedEntry.requiredBy)) { | ||
const scopes = (importMap.scopes[`${bundleServerUrl}/${requiredById}/`] ??= {}); | ||
const scopes = (importMap.scopes[`${urls.bundleServer}/${requiredById}/`] ??= {}); | ||
merge(scopes, importMapForEntry); | ||
@@ -37,7 +31,5 @@ } | ||
} | ||
async function getImportMapFromEntry(params) { | ||
const { options, context } = params; | ||
const { entry, bundleServerUrl, sessionVersion, targetVersions } = options; | ||
async function getImportMapFromEntry(entry, context) { | ||
const { config, sessionVersion = 0, targetVersions, urls, packages, packageImportPaths, packageHashes } = context; | ||
const { name } = entry; | ||
const { packages, packageImportPaths, getPackageHash, config } = context; | ||
const importMap = {}; | ||
@@ -54,7 +46,9 @@ const definition = await packages.get(entry.path); | ||
const packagePrefix = `${name}@${entry.version}/`; | ||
const baseUrl = `${bundleServerUrl}/${packagePrefix}`; | ||
const baseUrl = `${urls.bundleServer}/${packagePrefix}`; | ||
const isExternal = isExternalPackage(entry.path); | ||
const targetVersion = targetVersions?.[entry.path]; | ||
const cacheBuster = `v${sessionVersion ?? 0}${targetVersion ? `.${targetVersion}` : ''}/`; | ||
const hash = isExternal ? `h-${await getPackageHash(entry.path)}/` : 'h-pending/'; | ||
const hash = isExternal | ||
? `h-${await packageHashes.get({ packagePath: entry.path, isSourceHashingEnabled: false })}/` | ||
: 'h-pending/'; | ||
// Add the bundled entries to the import map, but prefer unbundled if the entry exists. | ||
@@ -74,3 +68,3 @@ const entryPaths = getRuntimeEntryPaths({ | ||
packageImportPaths?.get(entry.path)?.push(resolvedImportPath); | ||
importMap[resolvedImportPath] = new URL(actualEntryPath, baseUrl).href; | ||
importMap[resolvedImportPath] = makeUrl(actualEntryPath, baseUrl).href; | ||
} | ||
@@ -77,0 +71,0 @@ // Finally add a fallback for all other unexpected bundle entries. (Note, this should point to a script |
@@ -9,3 +9,2 @@ export { createImportMap } from './createImportMap.js'; | ||
export { parseRequestInfo } from './parseRequestInfo.js'; | ||
export { parseRequestUrl } from './parseRequestUrl.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -8,3 +8,2 @@ // ImportMap creation | ||
export { parseRequestInfo } from './parseRequestInfo.js'; | ||
export { parseRequestUrl } from './parseRequestUrl.js'; | ||
//# sourceMappingURL=index.js.map |
import type { CloudpackConfig, PackageDefinitionsCache } from '@ms-cloudpack/common-types'; | ||
import type { ResolveMap } from '@ms-cloudpack/package-utilities'; | ||
import type { PackageImportPaths } from './PackageImportPaths.js'; | ||
/** | ||
* Context for creating an import map. | ||
* Context for creating an import map. (Subset of properties from `Context` and `Session` in `api-server`.) | ||
*/ | ||
export type CreateImportMapContext = { | ||
export interface CreateImportMapContext { | ||
config: CloudpackConfig; | ||
resolveMap: ResolveMap; | ||
sessionVersion: number; | ||
targetVersions: Record<string, number>; | ||
urls: { | ||
bundleServer: string; | ||
}; | ||
packages: PackageDefinitionsCache; | ||
packageImportPaths?: PackageImportPaths; | ||
getPackageHash: (packagePath: string) => Promise<string>; | ||
config: CloudpackConfig; | ||
}; | ||
/** This matches `PackageHashes`. */ | ||
packageHashes: { | ||
get: (input: { | ||
packagePath: string; | ||
isSourceHashingEnabled: boolean; | ||
}) => Promise<string>; | ||
}; | ||
packageImportPaths: PackageImportPaths; | ||
} | ||
//# sourceMappingURL=CreateImportMapContext.d.ts.map |
{ | ||
"name": "@ms-cloudpack/import-map", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Utilities for creating/updating import maps.", | ||
@@ -18,5 +18,5 @@ "license": "MIT", | ||
"@ms-cloudpack/common-types": "^0.5.0", | ||
"@ms-cloudpack/package-utilities": "^7.4.1", | ||
"@ms-cloudpack/path-string-parsing": "^1.2.1", | ||
"@ms-cloudpack/path-utilities": "^2.7.6", | ||
"@ms-cloudpack/package-utilities": "^7.4.2", | ||
"@ms-cloudpack/path-string-parsing": "^1.2.2", | ||
"@ms-cloudpack/path-utilities": "^2.7.7", | ||
"merge": "^2.1.1" | ||
@@ -23,0 +23,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44941
39
374