| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const { CSS_TEXT_TYPE } = require("../ModuleSourceTypeConstants");
|
|---|
| 8 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 12 | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
|
|---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 15 | /** @typedef {import("../Module")} Module */
|
|---|
| 16 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 19 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Represents an inline `<style>...</style>` block in an HTML module. The
|
|---|
| 23 | * tag's content is fed into webpack's CSS pipeline as a virtual CSS module
|
|---|
| 24 | * with `exportType: "text"` so `url()` and `\@import` references are
|
|---|
| 25 | * resolved relative to the HTML file. At render time the original content
|
|---|
| 26 | * range is replaced with the processed CSS text read from the CSS module's
|
|---|
| 27 | * code generation data.
|
|---|
| 28 | */
|
|---|
| 29 | class HtmlInlineStyleDependency extends ModuleDependency {
|
|---|
| 30 | /**
|
|---|
| 31 | * Creates an instance of HtmlInlineStyleDependency.
|
|---|
| 32 | * @param {string} request virtual request resolving to the inline CSS (data URI)
|
|---|
| 33 | * @param {Range} range range of the inline CSS content (between `<style>` and `</style>`)
|
|---|
| 34 | */
|
|---|
| 35 | constructor(request, range) {
|
|---|
| 36 | super(request);
|
|---|
| 37 | this.range = range;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | get type() {
|
|---|
| 41 | return "html inline style";
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | get category() {
|
|---|
| 45 | return "html-style";
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Serializes this instance into the provided serializer context.
|
|---|
| 50 | * @param {ObjectSerializerContext} context context
|
|---|
| 51 | */
|
|---|
| 52 | serialize(context) {
|
|---|
| 53 | super.serialize(context);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Restores this instance from the provided deserializer context.
|
|---|
| 58 | * @param {ObjectDeserializerContext} context context
|
|---|
| 59 | */
|
|---|
| 60 | deserialize(context) {
|
|---|
| 61 | super.deserialize(context);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | HtmlInlineStyleDependency.Template = class HtmlInlineStyleDependencyTemplate extends (
|
|---|
| 66 | ModuleDependency.Template
|
|---|
| 67 | ) {
|
|---|
| 68 | /**
|
|---|
| 69 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 70 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 71 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 72 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 73 | * @returns {void}
|
|---|
| 74 | */
|
|---|
| 75 | apply(dependency, source, { moduleGraph, runtime, codeGenerationResults }) {
|
|---|
| 76 | const dep = /** @type {HtmlInlineStyleDependency} */ (dependency);
|
|---|
| 77 | const module = /** @type {Module} */ (moduleGraph.getModule(dep));
|
|---|
| 78 |
|
|---|
| 79 | /** @type {string} */
|
|---|
| 80 | let cssText = "";
|
|---|
| 81 |
|
|---|
| 82 | if (module) {
|
|---|
| 83 | const codeGen =
|
|---|
| 84 | /** @type {CodeGenerationResults} */
|
|---|
| 85 | (codeGenerationResults).get(module, runtime);
|
|---|
| 86 | const cssTextSource = codeGen.sources.get(CSS_TEXT_TYPE);
|
|---|
| 87 | if (cssTextSource) {
|
|---|
| 88 | cssText = /** @type {string} */ (cssTextSource.source());
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | source.replace(dep.range[0], dep.range[1] - 1, cssText);
|
|---|
| 93 | }
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | makeSerializable(
|
|---|
| 97 | HtmlInlineStyleDependency,
|
|---|
| 98 | "webpack/lib/dependencies/HtmlInlineStyleDependency"
|
|---|
| 99 | );
|
|---|
| 100 |
|
|---|
| 101 | module.exports = HtmlInlineStyleDependency;
|
|---|