1 | /*
|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
3 | Author Tobias Koppers @sokra
|
4 | */
|
5 | ;
|
6 |
|
7 | /** @typedef {import("./NormalModule")} NormalModule */
|
8 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
9 | /** @typedef {import("webpack-sources").Source} Source */
|
10 | /** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate */
|
11 |
|
12 | /**
|
13 | *
|
14 | */
|
15 | class Generator {
|
16 | static byType(map) {
|
17 | return new ByTypeGenerator(map);
|
18 | }
|
19 |
|
20 | /**
|
21 | * @abstract
|
22 | * @param {NormalModule} module module for which the code should be generated
|
23 | * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
|
24 | * @param {RuntimeTemplate} runtimeTemplate the runtime template
|
25 | * @param {string} type which kind of code should be generated
|
26 | * @returns {Source} generated code
|
27 | */
|
28 | generate(module, dependencyTemplates, runtimeTemplate, type) {
|
29 | throw new Error("Generator.generate: must be overridden");
|
30 | }
|
31 | }
|
32 |
|
33 | class ByTypeGenerator extends Generator {
|
34 | constructor(map) {
|
35 | super();
|
36 | this.map = map;
|
37 | }
|
38 |
|
39 | /**
|
40 | * @param {NormalModule} module module for which the code should be generated
|
41 | * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
|
42 | * @param {RuntimeTemplate} runtimeTemplate the runtime template
|
43 | * @param {string} type which kind of code should be generated
|
44 | * @returns {Source} generated code
|
45 | */
|
46 | generate(module, dependencyTemplates, runtimeTemplate, type) {
|
47 | const generator = this.map[type];
|
48 | if (!generator) {
|
49 | throw new Error(`Generator.byType: no generator specified for ${type}`);
|
50 | }
|
51 | return generator.generate(
|
52 | module,
|
53 | dependencyTemplates,
|
54 | runtimeTemplate,
|
55 | type
|
56 | );
|
57 | }
|
58 | }
|
59 |
|
60 | module.exports = Generator;
|