UNPKG

2.01 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
8
9/** @typedef {import("webpack-sources").Source} Source */
10/** @typedef {import("./Module")} Module */
11
12module.exports = class ModuleTemplate extends Tapable {
13 constructor(runtimeTemplate, type) {
14 super();
15 this.runtimeTemplate = runtimeTemplate;
16 this.type = type;
17 this.hooks = {
18 content: new SyncWaterfallHook([
19 "source",
20 "module",
21 "options",
22 "dependencyTemplates"
23 ]),
24 module: new SyncWaterfallHook([
25 "source",
26 "module",
27 "options",
28 "dependencyTemplates"
29 ]),
30 render: new SyncWaterfallHook([
31 "source",
32 "module",
33 "options",
34 "dependencyTemplates"
35 ]),
36 package: new SyncWaterfallHook([
37 "source",
38 "module",
39 "options",
40 "dependencyTemplates"
41 ]),
42 hash: new SyncHook(["hash"])
43 };
44 }
45
46 /**
47 * @param {Module} module the module
48 * @param {TODO} dependencyTemplates templates for dependencies
49 * @param {TODO} options render options
50 * @returns {Source} the source
51 */
52 render(module, dependencyTemplates, options) {
53 try {
54 const moduleSource = module.source(
55 dependencyTemplates,
56 this.runtimeTemplate,
57 this.type
58 );
59 const moduleSourcePostContent = this.hooks.content.call(
60 moduleSource,
61 module,
62 options,
63 dependencyTemplates
64 );
65 const moduleSourcePostModule = this.hooks.module.call(
66 moduleSourcePostContent,
67 module,
68 options,
69 dependencyTemplates
70 );
71 const moduleSourcePostRender = this.hooks.render.call(
72 moduleSourcePostModule,
73 module,
74 options,
75 dependencyTemplates
76 );
77 return this.hooks.package.call(
78 moduleSourcePostRender,
79 module,
80 options,
81 dependencyTemplates
82 );
83 } catch (e) {
84 e.message = `${module.identifier()}\n${e.message}`;
85 throw e;
86 }
87 }
88
89 updateHash(hash) {
90 hash.update("1");
91 this.hooks.hash.call(hash);
92 }
93};