From ffb0d87ce68560e2b301a090d257c105f60a969a Mon Sep 17 00:00:00 2001 From: Chikara Chan Date: Mon, 26 Nov 2018 19:39:43 +0800 Subject: [PATCH] feat: add crossOriginLoading option support (#313) --- src/index.js | 12 ++++++++++++ test/manual/index.html | 4 ++++ test/manual/src/crossorigin.css | 3 +++ test/manual/src/crossorigin.js | 1 + test/manual/src/index.js | 14 ++++++++++++++ test/manual/webpack.config.js | 4 ++++ 6 files changed, 38 insertions(+) create mode 100644 test/manual/src/crossorigin.css create mode 100644 test/manual/src/crossorigin.js diff --git a/src/index.js b/src/index.js index 60e909d2..3e2a9001 100644 --- a/src/index.js +++ b/src/index.js @@ -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), { @@ -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);', ]), diff --git a/test/manual/index.html b/test/manual/index.html index 8b29a314..8158d36a 100644 --- a/test/manual/index.html +++ b/test/manual/index.html @@ -47,6 +47,10 @@

Preloaded inlined CSS: Must be green.

displays an alert and should turn red.

+
+

CrossOriginLoading Option: Must be red.

+

loads chunks with crossorigin attribute and should turn green.

+
diff --git a/test/manual/src/crossorigin.css b/test/manual/src/crossorigin.css new file mode 100644 index 00000000..2b7fc030 --- /dev/null +++ b/test/manual/src/crossorigin.css @@ -0,0 +1,3 @@ +.crossorigin { + background: lightgreen; +} diff --git a/test/manual/src/crossorigin.js b/test/manual/src/crossorigin.js new file mode 100644 index 00000000..bd9683af --- /dev/null +++ b/test/manual/src/crossorigin.js @@ -0,0 +1 @@ +import './crossorigin.css'; diff --git a/test/manual/src/index.js b/test/manual/src/index.js index c6b9b72f..d6309e46 100644 --- a/test/manual/src/index.js +++ b/test/manual/src/index.js @@ -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; +}); diff --git a/test/manual/webpack.config.js b/test/manual/webpack.config.js index b1027612..c7959539 100644 --- a/test/manual/webpack.config.js +++ b/test/manual/webpack.config.js @@ -5,6 +5,7 @@ module.exports = { output: { chunkFilename: "[contenthash].js", publicPath: '/dist/', + crossOriginLoading: 'anonymous', }, module: { rules: [ @@ -25,5 +26,8 @@ module.exports = { ], devServer: { contentBase: __dirname, + headers: { + "Access-Control-Allow-Origin": "*", + } }, };