Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LayoutPlugin): allow to set and pass crossOrigin for styles and scripts #46

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/plugins/layout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only allows you to change the default to "use-credentials", right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I left "anonymous" as default. But for consistency added scriptsCrossOrigin as possible option.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using crossOrigin from options in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where else crossOrigin may be needed. We encountered situation, that we need it only for stylesheets.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fair enough, i've added the same settings for this case

})),
);

renderContent.styleSheets.push(
...cssAssets.map((css) => ({
href: getAbsoluteUrl(publicPath, css, options?.prefix),
crossOrigin: options.stylesCrossOrigin,
})),
);
} else {
Expand All @@ -80,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,
},
);
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/layout/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export type Manifest = Record<string, string> & {
export interface LayoutPluginOptions {
name: string;
prefix?: string;
scriptsCrossOrigin?: '' | 'anonymous' | 'use-credentials';
stylesCrossOrigin?: '' | 'anonymous' | 'use-credentials';
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

export interface Stylesheet {
href: string;
crossOrigin?: '' | 'anonymous' | 'use-credentials';
}

export interface Link {
Expand Down Expand Up @@ -69,7 +70,7 @@
renderLink(link: Link): string;
attrs(obj: Attributes): string;
}
export interface Plugin<Options = any, Name extends string = string> {

Check warning on line 73 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
name: Name;
apply: (params: {
options: Options | undefined;
Expand Down Expand Up @@ -109,7 +110,7 @@
? {[K in Name & string]: Options}
: {};

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void

Check warning on line 113 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
? I
: never;

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export function getRenderHelpers(params: {nonce?: string}): RenderHelpers {
function renderInlineScript(content: string) {
return `<script ${attrs({nonce: params.nonce})}>${content}</script>`;
}
function renderStyle({href}: Stylesheet) {
return href ? `<link ${attrs({rel: 'stylesheet', href})}>` : '';
function renderStyle({href, crossOrigin}: Stylesheet) {
return href ? `<link ${attrs({rel: 'stylesheet', crossorigin: crossOrigin, href})}>` : '';
}
function renderInlineStyle(content: string) {
return `<style ${attrs({nonce: params.nonce})}>${content}</style>`;
Expand Down
Loading