Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Jan 16, 2025
1 parent 788578b commit 8b49b53
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ export default (await import("astro/config")).defineConfig({
plugins: [
{
name: "crossorigin",
transform(code, id, options) {
return code
.replace(/<script/g, '<script crossorigin="anonymous"')
transform(Code, Identifier, _) {
const crossorigin = Identifier.includes(".js")
? `crossorigin=\\"anonymous\\"`
: 'crossorigin="anonymous"';

return Code.replace(/<script/g, `<script ${crossorigin}`)
.replace(
/<link[^>]*(?=.*rel="preload")(?=.*href="[^"]*\.js")(?=.*as="script")[^>]*/g,
'$& crossorigin="anonymous"',
`$& ${crossorigin}`,
)
.replace(
/<link[^>]*(?=.*rel="preload")(?=.*as="font")[^>]*/g,
'$& crossorigin="anonymous"',
`$& ${crossorigin}`,
)
.replace(
/<link[^>]*(?=.*rel="stylesheet")(?=.*href="https?:\/\/[^"]*")[^>]*/g,
'$& crossorigin="anonymous"',
`$& ${crossorigin}`,
);
},
},
Expand Down

0 comments on commit 8b49b53

Please sign in to comment.