From e58931f07a680148277fc06cdf60b36436175c40 Mon Sep 17 00:00:00 2001 From: Artem Panchuk Date: Sat, 14 Sep 2024 23:11:42 +0300 Subject: [PATCH 1/2] feat(LayoutPlugin): allow to set and pass crossOrigin for styles and scripts --- src/plugins/layout/index.ts | 3 ++- src/plugins/layout/types.ts | 2 ++ src/types.ts | 1 + src/utils.ts | 4 ++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/plugins/layout/index.ts b/src/plugins/layout/index.ts index f75e1e9..e016413 100644 --- a/src/plugins/layout/index.ts +++ b/src/plugins/layout/index.ts @@ -65,13 +65,14 @@ export function createLayoutPlugin({ ...jsAssets.map((js) => ({ src: getAbsoluteUrl(publicPath, js, options?.prefix), defer: true, - crossOrigin: 'anonymous' as const, + crossOrigin: options.scriptsCrossOrigin || 'anonymous', })), ); renderContent.styleSheets.push( ...cssAssets.map((css) => ({ href: getAbsoluteUrl(publicPath, css, options?.prefix), + crossOrigin: options.stylesCrossOrigin, })), ); } else { diff --git a/src/plugins/layout/types.ts b/src/plugins/layout/types.ts index 3a2a4c9..6164567 100644 --- a/src/plugins/layout/types.ts +++ b/src/plugins/layout/types.ts @@ -13,4 +13,6 @@ export type Manifest = Record & { export interface LayoutPluginOptions { name: string; prefix?: string; + scriptsCrossOrigin?: '' | 'anonymous' | 'use-credentials'; + stylesCrossOrigin?: '' | 'anonymous' | 'use-credentials'; } diff --git a/src/types.ts b/src/types.ts index a8d21df..191f01f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,6 +10,7 @@ export interface Script { export interface Stylesheet { href: string; + crossOrigin?: '' | 'anonymous' | 'use-credentials'; } export interface Link { diff --git a/src/utils.ts b/src/utils.ts index 6202a4e..2264a9f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,8 +25,8 @@ export function getRenderHelpers(params: {nonce?: string}): RenderHelpers { function renderInlineScript(content: string) { return ``; } - function renderStyle({href}: Stylesheet) { - return href ? `` : ''; + function renderStyle({href, crossOrigin}: Stylesheet) { + return href ? `` : ''; } function renderInlineStyle(content: string) { return ``; From 57765cf66ede62dd7f8a20bd0603c23b004dd683 Mon Sep 17 00:00:00 2001 From: Artem Panchuk Date: Mon, 16 Sep 2024 13:21:30 +0300 Subject: [PATCH 2/2] feat(LayoutPlugin): add same settings for second case --- src/plugins/layout/index.ts | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/plugins/layout/index.ts b/src/plugins/layout/index.ts index e016413..7e5e5cc 100644 --- a/src/plugins/layout/index.ts +++ b/src/plugins/layout/index.ts @@ -81,19 +81,40 @@ export function createLayoutPlugin({ return getAbsoluteUrl(publicPath, manifestEntries[name], options?.prefix); }; renderContent.scripts.push( - {src: getWebpackAssetUrl('runtime.js'), defer: true, crossOrigin: 'anonymous'}, - {src: getWebpackAssetUrl('vendors.js'), defer: true, crossOrigin: 'anonymous'}, - {src: getWebpackAssetUrl('commons.js'), defer: true, crossOrigin: 'anonymous'}, + { + src: getWebpackAssetUrl('runtime.js'), + defer: true, + crossOrigin: options.scriptsCrossOrigin || 'anonymous', + }, + { + src: getWebpackAssetUrl('vendors.js'), + defer: true, + crossOrigin: options.scriptsCrossOrigin || 'anonymous', + }, + { + src: getWebpackAssetUrl('commons.js'), + defer: true, + crossOrigin: options.scriptsCrossOrigin || 'anonymous', + }, { src: getWebpackAssetUrl(`${options.name}.js`), defer: true, - crossOrigin: 'anonymous', + crossOrigin: options.scriptsCrossOrigin || 'anonymous', }, ); renderContent.styleSheets.push( - {href: getWebpackAssetUrl('vendors.css')}, - {href: getWebpackAssetUrl('commons.css')}, - {href: getWebpackAssetUrl(`${options.name}.css`)}, + { + href: getWebpackAssetUrl('vendors.css'), + crossOrigin: options.stylesCrossOrigin, + }, + { + href: getWebpackAssetUrl('commons.css'), + crossOrigin: options.stylesCrossOrigin, + }, + { + href: getWebpackAssetUrl(`${options.name}.css`), + crossOrigin: options.stylesCrossOrigin, + }, ); } },