| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 8 | const CssUrlDependency = require("./CssUrlDependency");
|
|---|
| 9 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 15 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
|---|
| 16 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Represents an inline `<script>...</script>` block in an HTML module. The
|
|---|
| 22 | * tag's body is bundled as its own entry chunk — the same pipeline that
|
|---|
| 23 | * processes `<script src>` — and the inline body is replaced with a
|
|---|
| 24 | * `src` attribute pointing at the emitted chunk URL.
|
|---|
| 25 | */
|
|---|
| 26 | class HtmlInlineScriptDependency extends ModuleDependency {
|
|---|
| 27 | /**
|
|---|
| 28 | * Creates an instance of HtmlInlineScriptDependency.
|
|---|
| 29 | * @param {string} request virtual request resolving to the inline JS (data URI)
|
|---|
| 30 | * @param {number} insertPos position right after `<script` where ` src="…"` is inserted
|
|---|
| 31 | * @param {Range} contentRange range of the inline JS body (between `<script>` and `</script>`)
|
|---|
| 32 | * @param {string} entryName name of the entry the inline JS is bundled into
|
|---|
| 33 | * @param {string=} category dependency category used for resolving and grouping
|
|---|
| 34 | */
|
|---|
| 35 | constructor(request, insertPos, contentRange, entryName, category) {
|
|---|
| 36 | super(request);
|
|---|
| 37 | this.insertPos = insertPos;
|
|---|
| 38 | this.contentRange = contentRange;
|
|---|
| 39 | this.range = contentRange;
|
|---|
| 40 | this.entryName = entryName;
|
|---|
| 41 | /** @type {string} */
|
|---|
| 42 | this._category = category || "commonjs";
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | get type() {
|
|---|
| 46 | return "html inline script";
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | get category() {
|
|---|
| 50 | return this._category;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Serializes this instance into the provided serializer context.
|
|---|
| 55 | * @param {ObjectSerializerContext} context context
|
|---|
| 56 | */
|
|---|
| 57 | serialize(context) {
|
|---|
| 58 | const { write } = context;
|
|---|
| 59 | write(this.insertPos);
|
|---|
| 60 | write(this.contentRange);
|
|---|
| 61 | write(this.entryName);
|
|---|
| 62 | write(this._category);
|
|---|
| 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 | const { read } = context;
|
|---|
| 72 | this.insertPos = read();
|
|---|
| 73 | this.contentRange = read();
|
|---|
| 74 | this.range = this.contentRange;
|
|---|
| 75 | this.entryName = read();
|
|---|
| 76 | this._category = read();
|
|---|
| 77 | super.deserialize(context);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | HtmlInlineScriptDependency.Template = class HtmlInlineScriptDependencyTemplate extends (
|
|---|
| 82 | ModuleDependency.Template
|
|---|
| 83 | ) {
|
|---|
| 84 | /**
|
|---|
| 85 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 86 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 87 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 88 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 89 | * @returns {void}
|
|---|
| 90 | */
|
|---|
| 91 | apply(dependency, source, templateContext) {
|
|---|
| 92 | const { runtimeTemplate } = templateContext;
|
|---|
| 93 | const dep = /** @type {HtmlInlineScriptDependency} */ (dependency);
|
|---|
| 94 | const compilation = runtimeTemplate.compilation;
|
|---|
| 95 | const entrypoint = /** @type {Entrypoint | undefined} */ (
|
|---|
| 96 | compilation.entrypoints.get(dep.entryName)
|
|---|
| 97 | );
|
|---|
| 98 |
|
|---|
| 99 | /** @type {string} */
|
|---|
| 100 | let url = "data:,";
|
|---|
| 101 |
|
|---|
| 102 | if (entrypoint) {
|
|---|
| 103 | const chunk = /** @type {Chunk} */ (entrypoint.getEntrypointChunk());
|
|---|
| 104 | const outputOptions = runtimeTemplate.outputOptions;
|
|---|
| 105 | const filenameTemplate =
|
|---|
| 106 | chunk.filenameTemplate ||
|
|---|
| 107 | (chunk.canBeInitial()
|
|---|
| 108 | ? outputOptions.filename
|
|---|
| 109 | : outputOptions.chunkFilename);
|
|---|
| 110 |
|
|---|
| 111 | const filename = compilation.getPath(filenameTemplate, {
|
|---|
| 112 | chunk,
|
|---|
| 113 | contentHashType: "javascript"
|
|---|
| 114 | });
|
|---|
| 115 |
|
|---|
| 116 | url = `${CssUrlDependency.PUBLIC_PATH_AUTO}${filename}`;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // Insert ` src="…"` right after `<script` so the inline body is
|
|---|
| 120 | // served from the emitted chunk instead. The browser ignores the
|
|---|
| 121 | // remaining inline body once `src` is present, but we still clear
|
|---|
| 122 | // it below so the unprocessed JS doesn't ride along.
|
|---|
| 123 | source.insert(dep.insertPos, ` src="${url}"`);
|
|---|
| 124 | source.replace(dep.contentRange[0], dep.contentRange[1] - 1, "");
|
|---|
| 125 | }
|
|---|
| 126 | };
|
|---|
| 127 |
|
|---|
| 128 | makeSerializable(
|
|---|
| 129 | HtmlInlineScriptDependency,
|
|---|
| 130 | "webpack/lib/dependencies/HtmlInlineScriptDependency"
|
|---|
| 131 | );
|
|---|
| 132 |
|
|---|
| 133 | module.exports = HtmlInlineScriptDependency;
|
|---|