/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const HtmlGenerator = require("../html/HtmlGenerator");
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Entrypoint")} Entrypoint */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/**
* Represents an inline `` block in an HTML module. The
* tag's body is bundled as its own entry chunk — the same pipeline that
* processes ``)
* @param {string} entryName name of the entry the inline JS is bundled into
* @param {string=} category dependency category used for resolving and grouping
*/
constructor(request, insertPos, contentRange, entryName, category) {
super(request);
this.insertPos = insertPos;
this.contentRange = contentRange;
this.range = contentRange;
this.entryName = entryName;
/** @type {string} */
this._category = category || "commonjs";
}
get type() {
return "html inline script";
}
get category() {
return this._category;
}
/**
* Serializes this instance into the provided serializer context.
* @param {ObjectSerializerContext} context context
*/
serialize(context) {
const { write } = context;
write(this.insertPos);
write(this.contentRange);
write(this.entryName);
write(this._category);
super.serialize(context);
}
/**
* Restores this instance from the provided deserializer context.
* @param {ObjectDeserializerContext} context context
*/
deserialize(context) {
const { read } = context;
this.insertPos = read();
this.contentRange = read();
this.range = this.contentRange;
this.entryName = read();
this._category = read();
super.deserialize(context);
}
}
HtmlInlineScriptDependency.Template = class HtmlInlineScriptDependencyTemplate 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, templateContext) {
const { runtimeTemplate } = templateContext;
const dep = /** @type {HtmlInlineScriptDependency} */ (dependency);
const compilation = runtimeTemplate.compilation;
const entrypoint = /** @type {Entrypoint | undefined} */ (
compilation.entrypoints.get(dep.entryName)
);
/** @type {string} */
let url = "data:,";
if (entrypoint) {
const chunk = /** @type {Chunk} */ (entrypoint.getEntrypointChunk());
// Defer chunk-URL substitution to renderManifest — chunk hashes aren't ready yet.
url = HtmlGenerator.makeChunkUrlSentinel(chunk, "javascript");
}
// Insert ` src="…"` right after `