Skip to content

Commit ffb0d87

Browse files
chikara-chanevilebottnawi
authored andcommitted
feat: add crossOriginLoading option support (#313)
1 parent c12ddcb commit ffb0d87

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

src/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class MiniCssExtractPlugin {
272272
const chunkMap = this.getCssChunkObject(chunk);
273273
if (Object.keys(chunkMap).length > 0) {
274274
const chunkMaps = chunk.getChunkMaps();
275+
const { crossOriginLoading } = mainTemplate.outputOptions;
275276
const linkHrefPath = mainTemplate.getAssetPath(
276277
JSON.stringify(this.options.chunkFilename),
277278
{
@@ -365,6 +366,17 @@ class MiniCssExtractPlugin {
365366
]),
366367
'};',
367368
'linkTag.href = fullhref;',
369+
crossOriginLoading
370+
? Template.asString([
371+
`if (linkTag.href.indexOf(window.location.origin + '/') !== 0) {`,
372+
Template.indent(
373+
`linkTag.crossOrigin = ${JSON.stringify(
374+
crossOriginLoading
375+
)};`
376+
),
377+
'}',
378+
])
379+
: '',
368380
'var head = document.getElementsByTagName("head")[0];',
369381
'head.appendChild(linkTag);',
370382
]),

test/manual/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
<p>Preloaded inlined CSS: Must be green.</p>
4848
<p><button class="preloaded-button2">Pressing this button</button> displays an alert and should turn red.</p>
4949
</div>
50+
<div class="test crossorigin">
51+
<p>CrossOriginLoading Option: Must be red.</p>
52+
<p><button>Pressing this button</button> loads chunks with crossorigin attribute and should turn green.</p>
53+
</div>
5054
<div class="errors"></div>
5155
<script async defer src="/dist/main.js"></script>
5256
</body>

test/manual/src/crossorigin.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.crossorigin {
2+
background: lightgreen;
3+
}

test/manual/src/crossorigin.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './crossorigin.css';

test/manual/src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,17 @@ makeButton(".preloaded-button1", () => import(/* webpackChunkName: "preloaded1"
2424
makeButton(".preloaded-button2", () => import(/* webpackChunkName: "preloaded2" */ './preloaded2'));
2525

2626
makeButton(".lazy-failure-button", () => import('./lazy-failure.js'), false);
27+
28+
makeButton(".crossorigin", () => {
29+
const originalPublicPath = __webpack_public_path__;
30+
__webpack_public_path__ = "http://0.0.0.0:8080/dist/";
31+
const promise = import("./crossorigin").then(() => {
32+
const lastTwoElements = Array.from(document.head.children).slice(-2);
33+
const hasCrossorigin = lastTwoElements.every(element => element.crossOrigin === "anonymous");
34+
if (!hasCrossorigin) {
35+
throw new Error('Chunks miss crossorigin="anonymous" attribute.')
36+
}
37+
});
38+
__webpack_public_path__ = originalPublicPath;
39+
return promise;
40+
});

test/manual/webpack.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
output: {
66
chunkFilename: "[contenthash].js",
77
publicPath: '/dist/',
8+
crossOriginLoading: 'anonymous',
89
},
910
module: {
1011
rules: [
@@ -25,5 +26,8 @@ module.exports = {
2526
],
2627
devServer: {
2728
contentBase: __dirname,
29+
headers: {
30+
"Access-Control-Allow-Origin": "*",
31+
}
2832
},
2933
};

0 commit comments

Comments
 (0)