MCPcopy
hub / github.com/webpack/webpack / generateDataUri

Method generateDataUri

lib/asset/AssetGenerator.js:498–552  ·  view source on GitHub ↗

* Generates data uri. * @param {NormalModule} module module for which the code should be generated * @returns {string} DataURI

(module)

Source from the content-addressed store, hash-verified

496 * @returns {string} DataURI
497 */
498 generateDataUri(module) {
499 const source = /** @type {Source} */ (module.originalSource());
500
501 /** @type {string} */
502 let encodedSource;
503
504 if (typeof this.dataUrlOptions === "function") {
505 encodedSource = this.dataUrlOptions.call(null, source.source(), {
506 filename: /** @type {string} */ (module.getResource()),
507 module
508 });
509 } else {
510 let encoding =
511 /** @type {AssetGeneratorDataUrlOptions} */
512 (this.dataUrlOptions).encoding;
513 if (
514 encoding === undefined &&
515 module.resourceResolveData &&
516 module.resourceResolveData.encoding !== undefined
517 ) {
518 encoding = module.resourceResolveData.encoding;
519 }
520 if (encoding === undefined) {
521 encoding = DEFAULT_ENCODING;
522 }
523 const mimeType = this.getMimeType(module);
524
525 /** @type {string} */
526 let encodedContent;
527
528 if (
529 module.resourceResolveData &&
530 module.resourceResolveData.encoding === encoding &&
531 decodeDataUriContent(
532 module.resourceResolveData.encoding,
533 /** @type {string} */ (module.resourceResolveData.encodedContent)
534 ).equals(source.buffer())
535 ) {
536 encodedContent =
537 /** @type {string} */
538 (module.resourceResolveData.encodedContent);
539 } else {
540 encodedContent = encodeDataUri(
541 /** @type {"base64" | false} */ (encoding),
542 source
543 );
544 }
545
546 encodedSource = `data:${mimeType}${
547 encoding ? `;${encoding}` : ""
548 },${encodedContent}`;
549 }
550
551 return encodedSource;
552 }
553
554 /**
555 * Generates generated code for this runtime module.

Callers 1

generateMethod · 0.95

Calls 7

getMimeTypeMethod · 0.95
decodeDataUriContentFunction · 0.85
encodeDataUriFunction · 0.85
sourceMethod · 0.80
getResourceMethod · 0.80
originalSourceMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected