From c421ad92ce5554d2b6b9c0b44f24378759dce5ab Mon Sep 17 00:00:00 2001 From: Chris Sauve Date: Sat, 26 Oct 2024 18:34:07 -0400 Subject: [PATCH] Allow additional entries to be marked as inlined to include their content in the browser asset manifest --- .changeset/pretty-otters-pay.md | 7 + packages/assets/source/manifest/runtime.ts | 34 +++-- packages/assets/source/manifest/types.ts | 2 +- packages/assets/source/types.ts | 1 + .../source/server/components/ScriptAssets.tsx | 22 +-- .../source/server/components/StyleAssets.tsx | 25 ++-- packages/rollup/source/app.ts | 129 +++++++++++++++++- packages/rollup/source/features/assets.ts | 56 ++++++-- packages/rollup/source/features/system-js.ts | 30 ++-- 9 files changed, 242 insertions(+), 64 deletions(-) create mode 100644 .changeset/pretty-otters-pay.md diff --git a/.changeset/pretty-otters-pay.md b/.changeset/pretty-otters-pay.md new file mode 100644 index 000000000..b0cad27a2 --- /dev/null +++ b/.changeset/pretty-otters-pay.md @@ -0,0 +1,7 @@ +--- +'@quilted/preact-browser': patch +'@quilted/assets': patch +'@quilted/rollup': patch +--- + +Allow additional entries to be marked as inlined to include their content in the browser asset manifest diff --git a/packages/assets/source/manifest/runtime.ts b/packages/assets/source/manifest/runtime.ts index 3da96c160..a11a88822 100644 --- a/packages/assets/source/manifest/runtime.ts +++ b/packages/assets/source/manifest/runtime.ts @@ -122,6 +122,8 @@ const NORMALIZED_ASSET_BUILD_MANIFEST_CACHE = new WeakMap< NormalizedAssetBuildManifest >(); +const INTEGRITY_VALUE_REGEXP = /^(sha256|sha384|sha512)-[A-Za-z0-9+/=]{44,}$/; + function normalizeAssetBuildManifest( manifest: AssetBuildManifest, ): NormalizedAssetBuildManifest { @@ -141,18 +143,27 @@ function normalizeAssetBuildManifest( const scriptAttributes = manifest.attributes?.[2] ?? {}; manifest.assets.forEach((asset, index) => { - const [type, source, integrity, attributes] = asset; + const [type, source, integrityOrContent, attributes] = asset; + + const resolvedAsset: Asset = { + source: base + source, + attributes: {}, + }; + + if (integrityOrContent) { + if (INTEGRITY_VALUE_REGEXP.test(integrityOrContent)) { + resolvedAsset.attributes!.integrity = integrityOrContent; + } else { + resolvedAsset.content = integrityOrContent; + } + } if (type === 1) { - assets.styles.set(index, { - source: base + source, - attributes: {integrity: integrity!, ...styleAttributes, ...attributes}, - }); + Object.assign(resolvedAsset.attributes!, styleAttributes, attributes); + assets.styles.set(index, resolvedAsset); } else if (type === 2) { - assets.scripts.set(index, { - source: base + source, - attributes: {integrity: integrity!, ...scriptAttributes, ...attributes}, - }); + Object.assign(resolvedAsset.attributes!, scriptAttributes, attributes); + assets.scripts.set(index, resolvedAsset); } }); @@ -187,7 +198,10 @@ function createBrowserAssetsEntryFromManifest( const scripts = new Set(); if (entry) { - const entryModuleID = manifest.entries[entry]; + // Allow developers to omit the leading ./ from nested entrypoints, so they can pass + // either `entry: 'foo/bar'` or `entry: './foo/bar'` and still get the entry assets + const entryModuleID = + manifest.entries[entry] ?? manifest.entries[`./${entry}`]; const entryModule = entryModuleID ? manifest.modules[entryModuleID] : undefined; diff --git a/packages/assets/source/manifest/types.ts b/packages/assets/source/manifest/types.ts index 980e05ed0..3419d1179 100644 --- a/packages/assets/source/manifest/types.ts +++ b/packages/assets/source/manifest/types.ts @@ -18,6 +18,6 @@ export interface AssetBuildManifest { export type AssetBuildAsset = [ type: AssetBuildAssetType, path: string, - integrity?: string, + integrityOrIntegrity?: string, attributes?: {textContent: string; [key: string]: string | boolean | number}, ]; diff --git a/packages/assets/source/types.ts b/packages/assets/source/types.ts index 1ec3db51d..d2420485b 100644 --- a/packages/assets/source/types.ts +++ b/packages/assets/source/types.ts @@ -1,5 +1,6 @@ export interface Asset { source: string; + content?: string; attributes?: Record; } diff --git a/packages/preact-browser/source/server/components/ScriptAssets.tsx b/packages/preact-browser/source/server/components/ScriptAssets.tsx index e3a8a3a70..e2f0a60e3 100644 --- a/packages/preact-browser/source/server/components/ScriptAssets.tsx +++ b/packages/preact-browser/source/server/components/ScriptAssets.tsx @@ -14,15 +14,19 @@ export function ScriptAssets({ return ( <> - {scripts.map((asset) => ( -