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: add crossOriginLoading option support #313

Merged
merged 2 commits into from Nov 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/index.js
Expand Up @@ -272,6 +272,7 @@ class MiniCssExtractPlugin {
const chunkMap = this.getCssChunkObject(chunk);
if (Object.keys(chunkMap).length > 0) {
const chunkMaps = chunk.getChunkMaps();
const { crossOriginLoading } = mainTemplate.outputOptions;
const linkHrefPath = mainTemplate.getAssetPath(
JSON.stringify(this.options.chunkFilename),
{
Expand Down Expand Up @@ -365,6 +366,17 @@ class MiniCssExtractPlugin {
]),
'};',
'linkTag.href = fullhref;',
crossOriginLoading
? Template.asString([
`if (linkTag.href.indexOf(window.location.origin + '/') !== 0) {`,
Template.indent(
`linkTag.crossOrigin = ${JSON.stringify(
crossOriginLoading
)};`
),
'}',
])
: '',
'var head = document.getElementsByTagName("head")[0];',
'head.appendChild(linkTag);',
]),
Expand Down
4 changes: 4 additions & 0 deletions test/manual/index.html
Expand Up @@ -47,6 +47,10 @@
<p>Preloaded inlined CSS: Must be green.</p>
<p><button class="preloaded-button2">Pressing this button</button> displays an alert and should turn red.</p>
</div>
<div class="test crossorigin">
<p>CrossOriginLoading Option: Must be red.</p>
<p><button>Pressing this button</button> loads chunks with crossorigin attribute and should turn green.</p>
</div>
<div class="errors"></div>
<script async defer src="/dist/main.js"></script>
</body>
Expand Down
3 changes: 3 additions & 0 deletions test/manual/src/crossorigin.css
@@ -0,0 +1,3 @@
.crossorigin {
background: lightgreen;
}
1 change: 1 addition & 0 deletions test/manual/src/crossorigin.js
@@ -0,0 +1 @@
import './crossorigin.css';
14 changes: 14 additions & 0 deletions test/manual/src/index.js
Expand Up @@ -24,3 +24,17 @@ makeButton(".preloaded-button1", () => import(/* webpackChunkName: "preloaded1"
makeButton(".preloaded-button2", () => import(/* webpackChunkName: "preloaded2" */ './preloaded2'));

makeButton(".lazy-failure-button", () => import('./lazy-failure.js'), false);

makeButton(".crossorigin", () => {
const originalPublicPath = __webpack_public_path__;
__webpack_public_path__ = "http://0.0.0.0:8080/dist/";
const promise = import("./crossorigin").then(() => {
const lastTwoElements = Array.from(document.head.children).slice(-2);
const hasCrossorigin = lastTwoElements.every(element => element.crossOrigin === "anonymous");
if (!hasCrossorigin) {
throw new Error('Chunks miss crossorigin="anonymous" attribute.')
}
});
__webpack_public_path__ = originalPublicPath;
return promise;
});
4 changes: 4 additions & 0 deletions test/manual/webpack.config.js
Expand Up @@ -5,6 +5,7 @@ module.exports = {
output: {
chunkFilename: "[contenthash].js",
publicPath: '/dist/',
crossOriginLoading: 'anonymous',
},
module: {
rules: [
Expand All @@ -25,5 +26,8 @@ module.exports = {
],
devServer: {
contentBase: __dirname,
headers: {
"Access-Control-Allow-Origin": "*",
}
},
};