| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Alexander Akait @alexander-akait
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { ASSET_URL_TYPE } = require("../ModuleSourceTypeConstants");
|
|---|
| 9 | const RawDataUrlModule = require("../asset/RawDataUrlModule");
|
|---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 11 | const memoize = require("../util/memoize");
|
|---|
| 12 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 15 | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
|
|---|
| 16 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 17 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 18 | /** @typedef {import("../Module")} Module */
|
|---|
| 19 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 20 | /** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
|
|---|
| 21 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 24 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 25 |
|
|---|
| 26 | const getIgnoredRawDataUrlModule = memoize(
|
|---|
| 27 | () => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)")
|
|---|
| 28 | );
|
|---|
| 29 |
|
|---|
| 30 | class HtmlSourceDependency extends ModuleDependency {
|
|---|
| 31 | /**
|
|---|
| 32 | * Creates an instance of HtmlSourceDependency.
|
|---|
| 33 | * @param {string} request request
|
|---|
| 34 | * @param {Range} range range of the argument
|
|---|
| 35 | */
|
|---|
| 36 | constructor(request, range) {
|
|---|
| 37 | super(request);
|
|---|
| 38 | this.range = range;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | get type() {
|
|---|
| 42 | return "html source()";
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | get category() {
|
|---|
| 46 | return "url";
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Creates an ignored module.
|
|---|
| 51 | * @param {string} context context directory
|
|---|
| 52 | * @returns {Module} ignored module
|
|---|
| 53 | */
|
|---|
| 54 | createIgnoredModule(context) {
|
|---|
| 55 | return getIgnoredRawDataUrlModule();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Serializes this instance into the provided serializer context.
|
|---|
| 60 | * @param {ObjectSerializerContext} context context
|
|---|
| 61 | */
|
|---|
| 62 | serialize(context) {
|
|---|
| 63 | super.serialize(context);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Restores this instance from the provided deserializer context.
|
|---|
| 68 | * @param {ObjectDeserializerContext} context context
|
|---|
| 69 | */
|
|---|
| 70 | deserialize(context) {
|
|---|
| 71 | super.deserialize(context);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | HtmlSourceDependency.Template = class HtmlSourceDependencyTemplate extends (
|
|---|
| 76 | ModuleDependency.Template
|
|---|
| 77 | ) {
|
|---|
| 78 | /**
|
|---|
| 79 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 80 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 81 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 82 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 83 | * @returns {void}
|
|---|
| 84 | */
|
|---|
| 85 | apply(
|
|---|
| 86 | dependency,
|
|---|
| 87 | source,
|
|---|
| 88 | { moduleGraph, runtimeTemplate, codeGenerationResults }
|
|---|
| 89 | ) {
|
|---|
| 90 | const dep = /** @type {HtmlSourceDependency} */ (dependency);
|
|---|
| 91 | const module = /** @type {Module} */ (moduleGraph.getModule(dep));
|
|---|
| 92 |
|
|---|
| 93 | /** @type {string | undefined} */
|
|---|
| 94 | const newValue = this.assetUrl({
|
|---|
| 95 | module,
|
|---|
| 96 | codeGenerationResults
|
|---|
| 97 | });
|
|---|
| 98 |
|
|---|
| 99 | source.replace(dep.range[0], dep.range[1] - 1, newValue);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Returns the url of the asset.
|
|---|
| 104 | * @param {object} options options object
|
|---|
| 105 | * @param {Module} options.module the module
|
|---|
| 106 | * @param {RuntimeSpec=} options.runtime runtime
|
|---|
| 107 | * @param {CodeGenerationResults} options.codeGenerationResults the code generation results
|
|---|
| 108 | * @returns {string} the url of the asset
|
|---|
| 109 | */
|
|---|
| 110 | assetUrl({ runtime, module, codeGenerationResults }) {
|
|---|
| 111 | if (!module) {
|
|---|
| 112 | return "data:,";
|
|---|
| 113 | }
|
|---|
| 114 | const codeGen = codeGenerationResults.get(module, runtime);
|
|---|
| 115 | const data = codeGen.data;
|
|---|
| 116 | if (!data) return "data:,";
|
|---|
| 117 | const url = data.get("url");
|
|---|
| 118 | if (!url || !url[ASSET_URL_TYPE]) return "data:,";
|
|---|
| 119 | return url[ASSET_URL_TYPE];
|
|---|
| 120 | }
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | makeSerializable(
|
|---|
| 124 | HtmlSourceDependency,
|
|---|
| 125 | "webpack/lib/dependencies/HtmlSourceDependency"
|
|---|
| 126 | );
|
|---|
| 127 |
|
|---|
| 128 | module.exports = HtmlSourceDependency;
|
|---|