| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Alexander Akait @alexander-akait
- */
- "use strict";
- const { ASSET_URL_TYPE } = require("../ModuleSourceTypeConstants");
- const RawDataUrlModule = require("../asset/RawDataUrlModule");
- const makeSerializable = require("../util/makeSerializable");
- const memoize = require("../util/memoize");
- const ModuleDependency = require("./ModuleDependency");
- /** @import { ReplaceSource } from "webpack-sources" */
- /** @import CodeGenerationResults from "../CodeGenerationResults" */
- /** @import Dependency, { UpdateHashContext } from "../Dependency" */
- /**
- * @import {
- * CssDependencyTemplateContext as DependencyTemplateContext
- * } from "../DependencyTemplate"
- */
- /** @import Module, { BuildInfo } from "../Module" */
- /** @import { Range } from "../javascript/JavascriptParser" */
- /**
- * @import {
- * ObjectDeserializerContext,
- * ObjectSerializerContext
- * } from "../serialization/ObjectMiddleware"
- */
- /** @import Hash from "../util/Hash" */
- /** @import { RuntimeSpec } from "../util/runtime" */
- const getIgnoredRawDataUrlModule = memoize(
- () => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)")
- );
- class HtmlSourceDependency extends ModuleDependency {
- /**
- * Creates an instance of HtmlSourceDependency.
- * @param {string} request request
- * @param {Range} range range of the argument
- */
- constructor(request, range) {
- super(request);
- this.range = range;
- /** @type {true | undefined} */
- this.prefetch = undefined;
- /** @type {true | undefined} */
- this.preload = undefined;
- /** @type {"low" | "high" | "auto" | undefined} */
- this.fetchPriority = undefined;
- /** @type {string | undefined} */
- this.asAttribute = undefined;
- /** @type {string | undefined} */
- this.typeAttribute = undefined;
- /** @type {string | undefined} */
- this.mediaAttribute = undefined;
- }
- get type() {
- return "html source()";
- }
- get category() {
- return "url";
- }
- get referencedSourceType() {
- return ASSET_URL_TYPE;
- }
- /**
- * Creates an ignored module.
- * @param {string} context context directory
- * @returns {Module} ignored module
- */
- createIgnoredModule(context) {
- return getIgnoredRawDataUrlModule();
- }
- /**
- * Updates the hash with the data contributed by this instance.
- * @param {Hash} hash hash to be updated
- * @param {UpdateHashContext} context context
- * @returns {void}
- */
- updateHash(hash, context) {
- // Fold in the asset's hash so the HTML invalidates when the embedded URL changes.
- const { chunkGraph } = context;
- const module = chunkGraph.moduleGraph.getModule(this);
- if (!module) return;
- const { hash: buildHash } = /** @type {BuildInfo} */ (module.buildInfo);
- if (buildHash) hash.update(buildHash);
- }
- /**
- * Serializes this instance into the provided serializer context.
- * @param {ObjectSerializerContext} context context
- */
- serialize(context) {
- const { write } = context;
- write(this.prefetch);
- write(this.preload);
- write(this.fetchPriority);
- write(this.asAttribute);
- write(this.typeAttribute);
- write(this.mediaAttribute);
- super.serialize(context);
- }
- /**
- * Restores this instance from the provided deserializer context.
- * @param {ObjectDeserializerContext} context context
- */
- deserialize(context) {
- const { read } = context;
- this.prefetch = read();
- this.preload = read();
- this.fetchPriority = read();
- this.asAttribute = read();
- this.typeAttribute = read();
- this.mediaAttribute = read();
- super.deserialize(context);
- }
- }
- HtmlSourceDependency.Template = class HtmlSourceDependencyTemplate extends (
- ModuleDependency.Template
- ) {
- /**
- * Applies the plugin by registering its hooks on the compiler.
- * @param {Dependency} dependency the dependency for which the template should be applied
- * @param {ReplaceSource} source the current replace source which can be modified
- * @param {DependencyTemplateContext} templateContext the context object
- * @returns {void}
- */
- apply(
- dependency,
- source,
- { moduleGraph, runtimeTemplate, codeGenerationResults }
- ) {
- const dep = /** @type {HtmlSourceDependency} */ (dependency);
- const module = /** @type {Module} */ (moduleGraph.getModule(dep));
- /** @type {string | undefined} */
- const newValue = this.assetUrl({
- module,
- codeGenerationResults
- });
- source.replace(dep.range[0], dep.range[1] - 1, newValue);
- }
- /**
- * Returns the url of the asset.
- * @param {object} options options object
- * @param {Module} options.module the module
- * @param {RuntimeSpec=} options.runtime runtime
- * @param {CodeGenerationResults} options.codeGenerationResults the code generation results
- * @returns {string} the url of the asset
- */
- assetUrl({ runtime, module, codeGenerationResults }) {
- if (!module) {
- return "data:,";
- }
- const codeGen = codeGenerationResults.get(module, runtime);
- const data = codeGen.data;
- if (!data) return "data:,";
- const url = data.get("url");
- if (!url || !url[ASSET_URL_TYPE]) return "data:,";
- return url[ASSET_URL_TYPE];
- }
- };
- makeSerializable(
- HtmlSourceDependency,
- "webpack/lib/dependencies/HtmlSourceDependency"
- );
- module.exports = HtmlSourceDependency;
|