MCPcopy
hub / github.com/webpack/webpack / applyTemplate

Method applyTemplate

lib/html/HtmlParser.js:792–842  ·  lib/html/HtmlParser.js::HtmlParser.applyTemplate

* Runs the `template` option over the source and returns the transformed * html. Called from HtmlModulesPlugin's `processResult`, where the return * value becomes the module's stored source so the parser (which records * dependency offsets against it) and the generator (which renders from *

(source, module)

Source from the content-addressed store, hash-verified

790 * @returns {string | Buffer} the transformed source
791 */
792 applyTemplate(source, module) {
793 if (!this.template) return source;
794 class="cm">// `processResult` runs after `_doBuild` has initialized these
795 class="cm">// dependency sets, so they are always present here.
796 const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
797 const fileDependencies = /** @type {FileSystemDependencies} */ (
798 buildInfo.fileDependencies
799 );
800 const contextDependencies = /** @type {FileSystemDependencies} */ (
801 buildInfo.contextDependencies
802 );
803 const missingDependencies = /** @type {FileSystemDependencies} */ (
804 buildInfo.missingDependencies
805 );
806 const transformed = this.template(
807 typeof source === class="st">"string" ? source : source.toString(class="st">"utf8"),
808 {
809 module,
810 resource: module.resource,
811 addDependency: (dependency) => {
812 fileDependencies.add(dependency);
813 },
814 addContextDependency: (dependency) => {
815 contextDependencies.add(dependency);
816 },
817 addMissingDependency: (dependency) => {
818 missingDependencies.add(dependency);
819 },
820 addBuildDependency: (dependency) => {
821 if (buildInfo.buildDependencies === undefined) {
822 buildInfo.buildDependencies = new LazySet();
823 }
824 buildInfo.buildDependencies.add(dependency);
825 },
826 emitWarning: (warning) =>
827 module.addWarning(
828 warning instanceof Error ? warning : new WebpackError(warning)
829 ),
830 emitError: (error) =>
831 module.addError(
832 error instanceof Error ? error : new WebpackError(error)
833 )
834 }
835 );
836 if (typeof transformed !== class="st">"string") {
837 throw new Error(
838 class="st">"The `template` html parser option must return a string."
839 );
840 }
841 return transformed;
842 }
843
844 /**
845 * Parses the provided source and updates the parser state.

Callers 2

applyMethod · 0.80

Calls 4

addErrorMethod · 0.80
toStringMethod · 0.45
addMethod · 0.45
addWarningMethod · 0.45

Tested by

no test coverage detected