[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { SyncWaterfallHook } = require("tapable");
|
---|
| 9 | const Compilation = require("../Compilation");
|
---|
| 10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 11 | const RuntimeModule = require("../RuntimeModule");
|
---|
| 12 | const Template = require("../Template");
|
---|
| 13 | const compileBooleanMatcher = require("../util/compileBooleanMatcher");
|
---|
| 14 | const { chunkHasCss } = require("./CssModulesPlugin");
|
---|
| 15 |
|
---|
| 16 | /** @typedef {import("../../declarations/WebpackOptions").Environment} Environment */
|
---|
| 17 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 18 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 19 | /** @typedef {import("../Compilation").RuntimeRequirementsContext} RuntimeRequirementsContext */
|
---|
| 20 | /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @typedef {object} CssLoadingRuntimeModulePluginHooks
|
---|
| 24 | * @property {SyncWaterfallHook<[string, Chunk]>} createStylesheet
|
---|
| 25 | * @property {SyncWaterfallHook<[string, Chunk]>} linkPreload
|
---|
| 26 | * @property {SyncWaterfallHook<[string, Chunk]>} linkPrefetch
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @type {WeakMap<Compilation, CssLoadingRuntimeModulePluginHooks>} */
|
---|
| 30 | const compilationHooksMap = new WeakMap();
|
---|
| 31 |
|
---|
| 32 | class CssLoadingRuntimeModule extends RuntimeModule {
|
---|
| 33 | /**
|
---|
| 34 | * @param {Compilation} compilation the compilation
|
---|
| 35 | * @returns {CssLoadingRuntimeModulePluginHooks} hooks
|
---|
| 36 | */
|
---|
| 37 | static getCompilationHooks(compilation) {
|
---|
| 38 | if (!(compilation instanceof Compilation)) {
|
---|
| 39 | throw new TypeError(
|
---|
| 40 | "The 'compilation' argument must be an instance of Compilation"
|
---|
| 41 | );
|
---|
| 42 | }
|
---|
| 43 | let hooks = compilationHooksMap.get(compilation);
|
---|
| 44 | if (hooks === undefined) {
|
---|
| 45 | hooks = {
|
---|
| 46 | createStylesheet: new SyncWaterfallHook(["source", "chunk"]),
|
---|
| 47 | linkPreload: new SyncWaterfallHook(["source", "chunk"]),
|
---|
| 48 | linkPrefetch: new SyncWaterfallHook(["source", "chunk"])
|
---|
| 49 | };
|
---|
| 50 | compilationHooksMap.set(compilation, hooks);
|
---|
| 51 | }
|
---|
| 52 | return hooks;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
|
---|
| 57 | */
|
---|
| 58 | constructor(runtimeRequirements) {
|
---|
| 59 | super("css loading", 10);
|
---|
| 60 |
|
---|
| 61 | this._runtimeRequirements = runtimeRequirements;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * @returns {string | null} runtime code
|
---|
| 66 | */
|
---|
| 67 | generate() {
|
---|
| 68 | const { _runtimeRequirements } = this;
|
---|
| 69 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 70 | const chunk = /** @type {Chunk} */ (this.chunk);
|
---|
| 71 | const {
|
---|
| 72 | chunkGraph,
|
---|
| 73 | runtimeTemplate,
|
---|
| 74 | outputOptions: {
|
---|
| 75 | crossOriginLoading,
|
---|
| 76 | uniqueName,
|
---|
| 77 | chunkLoadTimeout: loadTimeout
|
---|
| 78 | }
|
---|
| 79 | } = compilation;
|
---|
| 80 | const fn = RuntimeGlobals.ensureChunkHandlers;
|
---|
| 81 | const conditionMap = chunkGraph.getChunkConditionMap(
|
---|
| 82 | /** @type {Chunk} */ (chunk),
|
---|
| 83 | /**
|
---|
| 84 | * @param {Chunk} chunk the chunk
|
---|
| 85 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 86 | * @returns {boolean} true, if the chunk has css
|
---|
| 87 | */
|
---|
| 88 | (chunk, chunkGraph) =>
|
---|
| 89 | Boolean(chunkGraph.getChunkModulesIterableBySourceType(chunk, "css"))
|
---|
| 90 | );
|
---|
| 91 | const hasCssMatcher = compileBooleanMatcher(conditionMap);
|
---|
| 92 |
|
---|
| 93 | const withLoading =
|
---|
| 94 | _runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) &&
|
---|
| 95 | hasCssMatcher !== false;
|
---|
| 96 | /** @type {boolean} */
|
---|
| 97 | const withHmr = _runtimeRequirements.has(
|
---|
| 98 | RuntimeGlobals.hmrDownloadUpdateHandlers
|
---|
| 99 | );
|
---|
| 100 | /** @type {Set<number | string | null>} */
|
---|
| 101 | const initialChunkIds = new Set();
|
---|
| 102 | for (const c of /** @type {Chunk} */ (chunk).getAllInitialChunks()) {
|
---|
| 103 | if (chunkHasCss(c, chunkGraph)) {
|
---|
| 104 | initialChunkIds.add(c.id);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if (!withLoading && !withHmr) {
|
---|
| 109 | return null;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | const environment =
|
---|
| 113 | /** @type {Environment} */
|
---|
| 114 | (compilation.outputOptions.environment);
|
---|
| 115 | const isNeutralPlatform = runtimeTemplate.isNeutralPlatform();
|
---|
| 116 | const withPrefetch =
|
---|
| 117 | this._runtimeRequirements.has(RuntimeGlobals.prefetchChunkHandlers) &&
|
---|
| 118 | (environment.document || isNeutralPlatform) &&
|
---|
| 119 | chunk.hasChildByOrder(chunkGraph, "prefetch", true, chunkHasCss);
|
---|
| 120 | const withPreload =
|
---|
| 121 | this._runtimeRequirements.has(RuntimeGlobals.preloadChunkHandlers) &&
|
---|
| 122 | (environment.document || isNeutralPlatform) &&
|
---|
| 123 | chunk.hasChildByOrder(chunkGraph, "preload", true, chunkHasCss);
|
---|
| 124 |
|
---|
| 125 | const { linkPreload, linkPrefetch } =
|
---|
| 126 | CssLoadingRuntimeModule.getCompilationHooks(compilation);
|
---|
| 127 |
|
---|
| 128 | const withFetchPriority = _runtimeRequirements.has(
|
---|
| 129 | RuntimeGlobals.hasFetchPriority
|
---|
| 130 | );
|
---|
| 131 |
|
---|
| 132 | const { createStylesheet } =
|
---|
| 133 | CssLoadingRuntimeModule.getCompilationHooks(compilation);
|
---|
| 134 |
|
---|
| 135 | const stateExpression = withHmr
|
---|
| 136 | ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_css`
|
---|
| 137 | : undefined;
|
---|
| 138 |
|
---|
| 139 | const code = Template.asString([
|
---|
| 140 | "link = document.createElement('link');",
|
---|
| 141 | `if (${RuntimeGlobals.scriptNonce}) {`,
|
---|
| 142 | Template.indent(
|
---|
| 143 | `link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
|
---|
| 144 | ),
|
---|
| 145 | "}",
|
---|
| 146 | uniqueName
|
---|
| 147 | ? 'link.setAttribute("data-webpack", uniqueName + ":" + key);'
|
---|
| 148 | : "",
|
---|
| 149 | withFetchPriority
|
---|
| 150 | ? Template.asString([
|
---|
| 151 | "if(fetchPriority) {",
|
---|
| 152 | Template.indent(
|
---|
| 153 | 'link.setAttribute("fetchpriority", fetchPriority);'
|
---|
| 154 | ),
|
---|
| 155 | "}"
|
---|
| 156 | ])
|
---|
| 157 | : "",
|
---|
| 158 | "link.setAttribute(loadingAttribute, 1);",
|
---|
| 159 | 'link.rel = "stylesheet";',
|
---|
| 160 | "link.href = url;",
|
---|
| 161 | crossOriginLoading
|
---|
| 162 | ? crossOriginLoading === "use-credentials"
|
---|
| 163 | ? 'link.crossOrigin = "use-credentials";'
|
---|
| 164 | : Template.asString([
|
---|
| 165 | "if (link.href.indexOf(window.location.origin + '/') !== 0) {",
|
---|
| 166 | Template.indent(
|
---|
| 167 | `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
|
---|
| 168 | ),
|
---|
| 169 | "}"
|
---|
| 170 | ])
|
---|
| 171 | : ""
|
---|
| 172 | ]);
|
---|
| 173 |
|
---|
| 174 | return Template.asString([
|
---|
| 175 | "// object to store loaded and loading chunks",
|
---|
| 176 | "// undefined = chunk not loaded, null = chunk preloaded/prefetched",
|
---|
| 177 | "// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded",
|
---|
| 178 | `var installedChunks = ${
|
---|
| 179 | stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
|
---|
| 180 | }{`,
|
---|
| 181 | Template.indent(
|
---|
| 182 | Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 0`).join(
|
---|
| 183 | ",\n"
|
---|
| 184 | )
|
---|
| 185 | ),
|
---|
| 186 | "};",
|
---|
| 187 | "",
|
---|
| 188 | uniqueName
|
---|
| 189 | ? `var uniqueName = ${JSON.stringify(
|
---|
| 190 | runtimeTemplate.outputOptions.uniqueName
|
---|
| 191 | )};`
|
---|
| 192 | : "// data-webpack is not used as build has no uniqueName",
|
---|
| 193 | withLoading || withHmr
|
---|
| 194 | ? Template.asString([
|
---|
| 195 | 'var loadingAttribute = "data-webpack-loading";',
|
---|
| 196 | `var loadStylesheet = ${runtimeTemplate.basicFunction(
|
---|
| 197 | `chunkId, url, done${
|
---|
| 198 | withFetchPriority ? ", fetchPriority" : ""
|
---|
| 199 | }${withHmr ? ", hmr" : ""}`,
|
---|
| 200 | [
|
---|
| 201 | 'var link, needAttach, key = "chunk-" + chunkId;',
|
---|
| 202 | withHmr ? "if(!hmr) {" : "",
|
---|
| 203 | 'var links = document.getElementsByTagName("link");',
|
---|
| 204 | "for(var i = 0; i < links.length; i++) {",
|
---|
| 205 | Template.indent([
|
---|
| 206 | "var l = links[i];",
|
---|
| 207 | `if(l.rel == "stylesheet" && (${
|
---|
| 208 | withHmr
|
---|
| 209 | ? 'l.href.startsWith(url) || l.getAttribute("href").startsWith(url)'
|
---|
| 210 | : 'l.href == url || l.getAttribute("href") == url'
|
---|
| 211 | }${
|
---|
| 212 | uniqueName
|
---|
| 213 | ? ' || l.getAttribute("data-webpack") == uniqueName + ":" + key'
|
---|
| 214 | : ""
|
---|
| 215 | })) { link = l; break; }`
|
---|
| 216 | ]),
|
---|
| 217 | "}",
|
---|
| 218 | "if(!done) return link;",
|
---|
| 219 | withHmr ? "}" : "",
|
---|
| 220 | "if(!link) {",
|
---|
| 221 | Template.indent([
|
---|
| 222 | "needAttach = true;",
|
---|
| 223 | createStylesheet.call(code, /** @type {Chunk} */ (this.chunk))
|
---|
| 224 | ]),
|
---|
| 225 | "}",
|
---|
| 226 | `var onLinkComplete = ${runtimeTemplate.basicFunction(
|
---|
| 227 | "prev, event",
|
---|
| 228 | Template.asString([
|
---|
| 229 | "link.onerror = link.onload = null;",
|
---|
| 230 | "link.removeAttribute(loadingAttribute);",
|
---|
| 231 | "clearTimeout(timeout);",
|
---|
| 232 | 'if(event && event.type != "load") link.parentNode.removeChild(link)',
|
---|
| 233 | "done(event);",
|
---|
| 234 | "if(prev) return prev(event);"
|
---|
| 235 | ])
|
---|
| 236 | )};`,
|
---|
| 237 | "if(link.getAttribute(loadingAttribute)) {",
|
---|
| 238 | Template.indent([
|
---|
| 239 | `var timeout = setTimeout(onLinkComplete.bind(null, undefined, { type: 'timeout', target: link }), ${loadTimeout});`,
|
---|
| 240 | "link.onerror = onLinkComplete.bind(null, link.onerror);",
|
---|
| 241 | "link.onload = onLinkComplete.bind(null, link.onload);"
|
---|
| 242 | ]),
|
---|
| 243 | "} else onLinkComplete(undefined, { type: 'load', target: link });", // We assume any existing stylesheet is render blocking
|
---|
| 244 | withHmr && withFetchPriority
|
---|
| 245 | ? 'if (hmr && hmr.getAttribute("fetchpriority")) link.setAttribute("fetchpriority", hmr.getAttribute("fetchpriority"));'
|
---|
| 246 | : "",
|
---|
| 247 | withHmr ? "hmr ? document.head.insertBefore(link, hmr) :" : "",
|
---|
| 248 | "needAttach && document.head.appendChild(link);",
|
---|
| 249 | "return link;"
|
---|
| 250 | ]
|
---|
| 251 | )};`
|
---|
| 252 | ])
|
---|
| 253 | : "",
|
---|
| 254 | withLoading
|
---|
| 255 | ? Template.asString([
|
---|
| 256 | `${fn}.css = ${runtimeTemplate.basicFunction(
|
---|
| 257 | `chunkId, promises${withFetchPriority ? " , fetchPriority" : ""}`,
|
---|
| 258 | [
|
---|
| 259 | "// css chunk loading",
|
---|
| 260 | `var installedChunkData = ${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;`,
|
---|
| 261 | 'if(installedChunkData !== 0) { // 0 means "already installed".',
|
---|
| 262 | Template.indent([
|
---|
| 263 | "",
|
---|
| 264 | '// a Promise means "currently loading".',
|
---|
| 265 | "if(installedChunkData) {",
|
---|
| 266 | Template.indent(["promises.push(installedChunkData[2]);"]),
|
---|
| 267 | "} else {",
|
---|
| 268 | Template.indent([
|
---|
| 269 | hasCssMatcher === true
|
---|
| 270 | ? "if(true) { // all chunks have CSS"
|
---|
| 271 | : `if(${hasCssMatcher("chunkId")}) {`,
|
---|
| 272 | Template.indent([
|
---|
| 273 | "// setup Promise in chunk cache",
|
---|
| 274 | `var promise = new Promise(${runtimeTemplate.expressionFunction(
|
---|
| 275 | "installedChunkData = installedChunks[chunkId] = [resolve, reject]",
|
---|
| 276 | "resolve, reject"
|
---|
| 277 | )});`,
|
---|
| 278 | "promises.push(installedChunkData[2] = promise);",
|
---|
| 279 | "",
|
---|
| 280 | "// start chunk loading",
|
---|
| 281 | `var url = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(chunkId);`,
|
---|
| 282 | "// create error before stack unwound to get useful stacktrace later",
|
---|
| 283 | "var error = new Error();",
|
---|
| 284 | `var loadingEnded = ${runtimeTemplate.basicFunction(
|
---|
| 285 | "event",
|
---|
| 286 | [
|
---|
| 287 | `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId)) {`,
|
---|
| 288 | Template.indent([
|
---|
| 289 | "installedChunkData = installedChunks[chunkId];",
|
---|
| 290 | "if(installedChunkData !== 0) installedChunks[chunkId] = undefined;",
|
---|
| 291 | "if(installedChunkData) {",
|
---|
| 292 | Template.indent([
|
---|
| 293 | 'if(event.type !== "load") {',
|
---|
| 294 | Template.indent([
|
---|
| 295 | "var errorType = event && event.type;",
|
---|
| 296 | "var realHref = event && event.target && event.target.href;",
|
---|
| 297 | "error.message = 'Loading css chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realHref + ')';",
|
---|
| 298 | "error.name = 'ChunkLoadError';",
|
---|
| 299 | "error.type = errorType;",
|
---|
| 300 | "error.request = realHref;",
|
---|
| 301 | "installedChunkData[1](error);"
|
---|
| 302 | ]),
|
---|
| 303 | "} else {",
|
---|
| 304 | Template.indent([
|
---|
| 305 | "installedChunks[chunkId] = 0;",
|
---|
| 306 | "installedChunkData[0]();"
|
---|
| 307 | ]),
|
---|
| 308 | "}"
|
---|
| 309 | ]),
|
---|
| 310 | "}"
|
---|
| 311 | ]),
|
---|
| 312 | "}"
|
---|
| 313 | ]
|
---|
| 314 | )};`,
|
---|
| 315 | isNeutralPlatform
|
---|
| 316 | ? "if (typeof document !== 'undefined') {"
|
---|
| 317 | : "",
|
---|
| 318 | Template.indent([
|
---|
| 319 | `loadStylesheet(chunkId, url, loadingEnded${
|
---|
| 320 | withFetchPriority ? ", fetchPriority" : ""
|
---|
| 321 | });`
|
---|
| 322 | ]),
|
---|
| 323 | isNeutralPlatform
|
---|
| 324 | ? "} else { loadingEnded({ type: 'load' }); }"
|
---|
| 325 | : ""
|
---|
| 326 | ]),
|
---|
| 327 | "} else installedChunks[chunkId] = 0;"
|
---|
| 328 | ]),
|
---|
| 329 | "}"
|
---|
| 330 | ]),
|
---|
| 331 | "}"
|
---|
| 332 | ]
|
---|
| 333 | )};`
|
---|
| 334 | ])
|
---|
| 335 | : "// no chunk loading",
|
---|
| 336 | "",
|
---|
| 337 | withPrefetch && hasCssMatcher !== false
|
---|
| 338 | ? `${
|
---|
| 339 | RuntimeGlobals.prefetchChunkHandlers
|
---|
| 340 | }.s = ${runtimeTemplate.basicFunction("chunkId", [
|
---|
| 341 | `if((!${
|
---|
| 342 | RuntimeGlobals.hasOwnProperty
|
---|
| 343 | }(installedChunks, chunkId) || installedChunks[chunkId] === undefined) && ${
|
---|
| 344 | hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
|
---|
| 345 | }) {`,
|
---|
| 346 | Template.indent([
|
---|
| 347 | "installedChunks[chunkId] = null;",
|
---|
| 348 | isNeutralPlatform
|
---|
| 349 | ? "if (typeof document === 'undefined') return;"
|
---|
| 350 | : "",
|
---|
| 351 | linkPrefetch.call(
|
---|
| 352 | Template.asString([
|
---|
| 353 | "var link = document.createElement('link');",
|
---|
| 354 | crossOriginLoading
|
---|
| 355 | ? `link.crossOrigin = ${JSON.stringify(
|
---|
| 356 | crossOriginLoading
|
---|
| 357 | )};`
|
---|
| 358 | : "",
|
---|
| 359 | `if (${RuntimeGlobals.scriptNonce}) {`,
|
---|
| 360 | Template.indent(
|
---|
| 361 | `link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
|
---|
| 362 | ),
|
---|
| 363 | "}",
|
---|
| 364 | 'link.rel = "prefetch";',
|
---|
| 365 | 'link.as = "style";',
|
---|
| 366 | `link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(chunkId);`
|
---|
| 367 | ]),
|
---|
| 368 | chunk
|
---|
| 369 | ),
|
---|
| 370 | "document.head.appendChild(link);"
|
---|
| 371 | ]),
|
---|
| 372 | "}"
|
---|
| 373 | ])};`
|
---|
| 374 | : "// no prefetching",
|
---|
| 375 | "",
|
---|
| 376 | withPreload && hasCssMatcher !== false
|
---|
| 377 | ? `${
|
---|
| 378 | RuntimeGlobals.preloadChunkHandlers
|
---|
| 379 | }.s = ${runtimeTemplate.basicFunction("chunkId", [
|
---|
| 380 | `if((!${
|
---|
| 381 | RuntimeGlobals.hasOwnProperty
|
---|
| 382 | }(installedChunks, chunkId) || installedChunks[chunkId] === undefined) && ${
|
---|
| 383 | hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")
|
---|
| 384 | }) {`,
|
---|
| 385 | Template.indent([
|
---|
| 386 | "installedChunks[chunkId] = null;",
|
---|
| 387 | isNeutralPlatform
|
---|
| 388 | ? "if (typeof document === 'undefined') return;"
|
---|
| 389 | : "",
|
---|
| 390 | linkPreload.call(
|
---|
| 391 | Template.asString([
|
---|
| 392 | "var link = document.createElement('link');",
|
---|
| 393 | "link.charset = 'utf-8';",
|
---|
| 394 | `if (${RuntimeGlobals.scriptNonce}) {`,
|
---|
| 395 | Template.indent(
|
---|
| 396 | `link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
|
---|
| 397 | ),
|
---|
| 398 | "}",
|
---|
| 399 | 'link.rel = "preload";',
|
---|
| 400 | 'link.as = "style";',
|
---|
| 401 | `link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(chunkId);`,
|
---|
| 402 | crossOriginLoading
|
---|
| 403 | ? crossOriginLoading === "use-credentials"
|
---|
| 404 | ? 'link.crossOrigin = "use-credentials";'
|
---|
| 405 | : Template.asString([
|
---|
| 406 | "if (link.href.indexOf(window.location.origin + '/') !== 0) {",
|
---|
| 407 | Template.indent(
|
---|
| 408 | `link.crossOrigin = ${JSON.stringify(
|
---|
| 409 | crossOriginLoading
|
---|
| 410 | )};`
|
---|
| 411 | ),
|
---|
| 412 | "}"
|
---|
| 413 | ])
|
---|
| 414 | : ""
|
---|
| 415 | ]),
|
---|
| 416 | chunk
|
---|
| 417 | ),
|
---|
| 418 | "document.head.appendChild(link);"
|
---|
| 419 | ]),
|
---|
| 420 | "}"
|
---|
| 421 | ])};`
|
---|
| 422 | : "// no preloaded",
|
---|
| 423 | withHmr
|
---|
| 424 | ? Template.asString([
|
---|
| 425 | "var oldTags = [];",
|
---|
| 426 | "var newTags = [];",
|
---|
| 427 | `var applyHandler = ${runtimeTemplate.basicFunction("options", [
|
---|
| 428 | `return { dispose: ${runtimeTemplate.basicFunction("", [
|
---|
| 429 | "while(oldTags.length) {",
|
---|
| 430 | Template.indent([
|
---|
| 431 | "var oldTag = oldTags.pop();",
|
---|
| 432 | "if(oldTag.parentNode) oldTag.parentNode.removeChild(oldTag);"
|
---|
| 433 | ]),
|
---|
| 434 | "}"
|
---|
| 435 | ])}, apply: ${runtimeTemplate.basicFunction("", [
|
---|
| 436 | "while(newTags.length) {",
|
---|
| 437 | Template.indent([
|
---|
| 438 | "var newTag = newTags.pop();",
|
---|
| 439 | "newTag.sheet.disabled = false"
|
---|
| 440 | ]),
|
---|
| 441 | "}"
|
---|
| 442 | ])} };`
|
---|
| 443 | ])}`,
|
---|
| 444 | `var cssTextKey = ${runtimeTemplate.returningFunction(
|
---|
| 445 | `Array.from(link.sheet.cssRules, ${runtimeTemplate.returningFunction(
|
---|
| 446 | "r.cssText",
|
---|
| 447 | "r"
|
---|
| 448 | )}).join()`,
|
---|
| 449 | "link"
|
---|
| 450 | )};`,
|
---|
| 451 | `${
|
---|
| 452 | RuntimeGlobals.hmrDownloadUpdateHandlers
|
---|
| 453 | }.css = ${runtimeTemplate.basicFunction(
|
---|
| 454 | "chunkIds, removedChunks, removedModules, promises, applyHandlers, updatedModulesList",
|
---|
| 455 | [
|
---|
| 456 | isNeutralPlatform
|
---|
| 457 | ? "if (typeof document === 'undefined') return;"
|
---|
| 458 | : "",
|
---|
| 459 | "applyHandlers.push(applyHandler);",
|
---|
| 460 | `chunkIds.forEach(${runtimeTemplate.basicFunction("chunkId", [
|
---|
| 461 | `var filename = ${RuntimeGlobals.getChunkCssFilename}(chunkId);`,
|
---|
| 462 | `var url = ${RuntimeGlobals.publicPath} + filename;`,
|
---|
| 463 | "var oldTag = loadStylesheet(chunkId, url);",
|
---|
| 464 | "if(!oldTag) return;",
|
---|
| 465 | `promises.push(new Promise(${runtimeTemplate.basicFunction(
|
---|
| 466 | "resolve, reject",
|
---|
| 467 | [
|
---|
| 468 | `var link = loadStylesheet(chunkId, url + (url.indexOf("?") < 0 ? "?" : "&") + "hmr=" + Date.now(), ${runtimeTemplate.basicFunction(
|
---|
| 469 | "event",
|
---|
| 470 | [
|
---|
| 471 | 'if(event.type !== "load") {',
|
---|
| 472 | Template.indent([
|
---|
| 473 | "var errorType = event && event.type;",
|
---|
| 474 | "var realHref = event && event.target && event.target.href;",
|
---|
| 475 | "error.message = 'Loading css hot update chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realHref + ')';",
|
---|
| 476 | "error.name = 'ChunkLoadError';",
|
---|
| 477 | "error.type = errorType;",
|
---|
| 478 | "error.request = realHref;",
|
---|
| 479 | "reject(error);"
|
---|
| 480 | ]),
|
---|
| 481 | "} else {",
|
---|
| 482 | Template.indent([
|
---|
| 483 | "try { if(cssTextKey(oldTag) == cssTextKey(link)) { if(link.parentNode) link.parentNode.removeChild(link); return resolve(); } } catch(e) {}",
|
---|
| 484 | "link.sheet.disabled = true;",
|
---|
| 485 | "oldTags.push(oldTag);",
|
---|
| 486 | "newTags.push(link);",
|
---|
| 487 | "resolve();"
|
---|
| 488 | ]),
|
---|
| 489 | "}"
|
---|
| 490 | ]
|
---|
| 491 | )}, ${withFetchPriority ? "undefined," : ""} oldTag);`
|
---|
| 492 | ]
|
---|
| 493 | )}));`
|
---|
| 494 | ])});`
|
---|
| 495 | ]
|
---|
| 496 | )}`
|
---|
| 497 | ])
|
---|
| 498 | : "// no hmr"
|
---|
| 499 | ]);
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | module.exports = CssLoadingRuntimeModule;
|
---|