2018-04-28 00:53:07 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2018-04-28 00:53:07 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
|
|
|
/** @typedef {import("./NormalModule")} NormalModule */
|
|
|
|
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
2018-04-28 00:53:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Generator {
|
|
|
|
static byType(map) {
|
|
|
|
return new ByTypeGenerator(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
2018-07-21 00:17:51 +08:00
|
|
|
* @param {NormalModule} module module for which the code should be generated
|
2018-07-11 19:05:13 +08:00
|
|
|
* @param {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
|
2018-04-28 00:53:07 +08:00
|
|
|
* @param {RuntimeTemplate} runtimeTemplate the runtime template
|
|
|
|
* @param {string} type which kind of code should be generated
|
|
|
|
* @returns {Source} generated code
|
|
|
|
*/
|
|
|
|
generate(module, dependencyTemplates, runtimeTemplate, type) {
|
2018-07-10 04:48:12 +08:00
|
|
|
throw new Error("Generator.generate: must be overridden");
|
2018-04-28 00:53:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ByTypeGenerator extends Generator {
|
|
|
|
constructor(map) {
|
|
|
|
super();
|
|
|
|
this.map = map;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:17:51 +08:00
|
|
|
/**
|
|
|
|
* @param {NormalModule} module module for which the code should be generated
|
2018-07-23 18:19:16 +08:00
|
|
|
* @param {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
|
2018-07-21 00:17:51 +08:00
|
|
|
* @param {RuntimeTemplate} runtimeTemplate the runtime template
|
|
|
|
* @param {string} type which kind of code should be generated
|
|
|
|
* @returns {Source} generated code
|
|
|
|
*/
|
2018-04-28 00:53:07 +08:00
|
|
|
generate(module, dependencyTemplates, runtimeTemplate, type) {
|
|
|
|
const generator = this.map[type];
|
|
|
|
if (!generator) {
|
|
|
|
throw new Error(`Generator.byType: no generator specified for ${type}`);
|
|
|
|
}
|
|
|
|
return generator.generate(
|
|
|
|
module,
|
|
|
|
dependencyTemplates,
|
|
|
|
runtimeTemplate,
|
|
|
|
type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Generator;
|