| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- */
- "use strict";
- const { CSS_TEXT_TYPE } = require("../ModuleSourceTypeConstants");
- const makeSerializable = require("../util/makeSerializable");
- const ModuleDependency = require("./ModuleDependency");
- /** @import { ReplaceSource } from "webpack-sources" */
- /** @import CodeGenerationResults from "../CodeGenerationResults" */
- /** @import Dependency, { UpdateHashContext } from "../Dependency" */
- /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
- /** @import Module from "../Module" */
- /** @import { Range } from "../javascript/JavascriptParser" */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<boolean[]>} ObjectDeserializerContext */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<boolean[]>} ObjectSerializerContext */
- /** @import Hash from "../util/Hash" */
- const TRAILING_WHITESPACE_REGEXP = /\s+$/;
- // One pass over the source: each char is replaced once and replacements aren't
- // re-scanned, so this matches the `&`-first sequential escape chain exactly
- // while avoiding two extra full-string scans + intermediate strings.
- const ATTR_ESCAPE_REGEXP = /[&"']/g;
- /** @type {Record<string, string>} */
- const ATTR_ESCAPES = { "&": "&", '"': """, "'": "'" };
- /**
- * @param {string} c matched character
- * @returns {string} the HTML-attribute-escaped entity
- */
- const escapeAttrChar = (c) => ATTR_ESCAPES[c];
- /**
- * Represents inline CSS in an HTML module — either a `<style>...</style>`
- * block (a stylesheet) or an element's `style="..."` attribute (a CSS
- * block's contents). The content is fed into webpack's CSS pipeline as a
- * virtual CSS module with `exportType: "text"` so `url()` and `\@import`
- * references are resolved relative to the HTML file. At render time the
- * original content range is replaced with the processed CSS text read from
- * the CSS module's code generation data.
- */
- class HtmlInlineStyleDependency extends ModuleDependency {
- /**
- * Creates an instance of HtmlInlineStyleDependency.
- * @param {string} request virtual request resolving to the inline CSS (data URI)
- * @param {Range} range range of the inline CSS content (between `<style>` and `</style>`, or the `style` attribute value)
- * @param {boolean=} attribute true when the source is a `style="..."` attribute (a block's contents) rather than a `<style>` block (stylesheet)
- * @param {boolean=} inAttribute true when the range is an attribute value (decoded on parse, so the processed CSS must be re-escaped on write-back); defaults to `attribute` — differs only for attribute values parsed as full stylesheets
- */
- constructor(request, range, attribute = false, inAttribute = attribute) {
- super(request);
- this.range = range;
- /** @type {boolean} */
- this.attribute = attribute;
- /** @type {boolean} */
- this.inAttribute = inAttribute;
- }
- get type() {
- return "html inline style";
- }
- get category() {
- return this.attribute ? "html-style-attribute" : "html-style";
- }
- /**
- * 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) {
- // Recurse so the inline CSS's transitive deps (e.g. `url(asset)`) propagate up.
- const { chunkGraph } = context;
- const module = chunkGraph.moduleGraph.getModule(this);
- if (!module) return;
- module.updateHash(hash, context);
- }
- /**
- * Serializes this instance into the provided serializer context.
- * @param {ObjectSerializerContext} context context
- */
- serialize(context) {
- super.serialize(context);
- context.write(this.attribute);
- context.write(this.inAttribute);
- }
- /**
- * Restores this instance from the provided deserializer context.
- * @param {ObjectDeserializerContext} context context
- */
- deserialize(context) {
- super.deserialize(context);
- this.attribute = context.read();
- this.inAttribute = context.read();
- }
- }
- HtmlInlineStyleDependency.Template = class HtmlInlineStyleDependencyTemplate 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, runtime, codeGenerationResults }) {
- const dep = /** @type {HtmlInlineStyleDependency} */ (dependency);
- const module = /** @type {Module} */ (moduleGraph.getModule(dep));
- /** @type {string} */
- let cssText = "";
- if (module) {
- const codeGen =
- /** @type {CodeGenerationResults} */
- (codeGenerationResults).get(module, runtime);
- const cssTextSource = codeGen.sources.get(CSS_TEXT_TYPE);
- if (cssTextSource) {
- cssText = /** @type {string} */ (cssTextSource.source());
- }
- }
- // An attribute value was entity-decoded before parsing, so re-escape
- // the processed CSS to keep the surrounding markup valid in any
- // quoting context, and drop the trailing newline the CSS generator
- // appends so the rewritten attribute stays on one line.
- if (dep.inAttribute) {
- cssText = cssText
- .replace(TRAILING_WHITESPACE_REGEXP, "")
- .replace(ATTR_ESCAPE_REGEXP, escapeAttrChar);
- }
- source.replace(dep.range[0], dep.range[1] - 1, cssText);
- }
- };
- makeSerializable(
- HtmlInlineStyleDependency,
- "webpack/lib/dependencies/HtmlInlineStyleDependency"
- );
- module.exports = HtmlInlineStyleDependency;
|