Skip to content

Commit

Permalink
feat: css support crossorigin (#50)
Browse files Browse the repository at this point in the history
Support cross-origin for style

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced stylesheet and script loading with configurable cross-origin
settings.
- Added support for dynamically setting `crossorigin` attribute for
external resources.
- **Tests**
- Expanded test coverage to verify the inclusion of the `crossorigin`
attribute in generated HTML for styles and scripts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: shiluo.dwt <[email protected]>
  • Loading branch information
WynterDing and shiluo.dwt authored Jan 23, 2025
1 parent 2a4fd2b commit bbb2794
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/assets_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class Assets {

getStyle(entry) {
entry = entry || this.entryCss;
return linkTpl({ url: this.getURL(entry) });
return linkTpl({
url: this.getURL(entry),
crossorigin: this.crossorigin,
});
}

getScript(entry) {
Expand Down Expand Up @@ -84,8 +87,8 @@ class Assets {

module.exports = Assets;

function linkTpl({ url }) {
return `<link rel="stylesheet" href="${url}" />`;
function linkTpl({ url, crossorigin }) {
return crossorigin ? `<link rel="stylesheet" href="${url}" crossorigin="anonymous" />` : `<link rel="stylesheet" href="${url}" />`;
}

function scriptTpl({ url, crossorigin }) {
Expand Down
2 changes: 2 additions & 0 deletions test/assets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ describe('test/assets.test.js', () => {
const ctx = app.mockContext();
ctx.helper.assets.setEntry('index.js');
const script = ctx.helper.assets.getScript();
const style = ctx.helper.assets.getStyle();
assert(style.includes('crossorigin'));
assert(script.includes('crossorigin'));
});
});
Expand Down

0 comments on commit bbb2794

Please sign in to comment.