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";
|
|
|
|
|
2019-09-28 01:50:23 +08:00
|
|
|
const AbstractMethodError = require("./AbstractMethodError");
|
|
|
|
|
2018-04-28 00:53:07 +08:00
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2018-08-23 02:17:49 +08:00
|
|
|
/** @typedef {import("./ChunkGraph")} ChunkGraph */
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("./ModuleGraph")} ModuleGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./NormalModule")} NormalModule */
|
|
|
|
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
2018-04-28 00:53:07 +08:00
|
|
|
|
2018-07-18 00:57:03 +08:00
|
|
|
/**
|
|
|
|
* @typedef {Object} GenerateContext
|
|
|
|
* @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
|
|
|
|
* @property {RuntimeTemplate} runtimeTemplate the runtime template
|
2018-07-24 23:35:36 +08:00
|
|
|
* @property {ModuleGraph} moduleGraph the module graph
|
2018-08-23 02:17:49 +08:00
|
|
|
* @property {ChunkGraph} chunkGraph the chunk graph
|
2018-11-15 00:31:32 +08:00
|
|
|
* @property {Set<string>} runtimeRequirements the requirements for runtime
|
2018-07-18 00:57:03 +08:00
|
|
|
* @property {string} type which kind of code should be generated
|
|
|
|
*/
|
|
|
|
|
2018-04-28 00:53:07 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Generator {
|
|
|
|
static byType(map) {
|
|
|
|
return new ByTypeGenerator(map);
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:00:32 +08:00
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @returns {Set<string>} available types (do not mutate)
|
|
|
|
*/
|
|
|
|
getTypes() {
|
2019-09-28 01:50:23 +08:00
|
|
|
throw new AbstractMethodError();
|
2018-12-03 22:00:32 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 18:23:40 +08:00
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @param {NormalModule} module the module
|
|
|
|
* @param {string=} type source type
|
|
|
|
* @returns {number} estimate size of the module
|
|
|
|
*/
|
|
|
|
getSize(module, type) {
|
2019-09-28 01:50:23 +08:00
|
|
|
throw new AbstractMethodError();
|
2018-12-04 18:23:40 +08:00
|
|
|
}
|
|
|
|
|
2018-04-28 00:53:07 +08:00
|
|
|
/**
|
|
|
|
* @abstract
|
2018-07-21 00:17:51 +08:00
|
|
|
* @param {NormalModule} module module for which the code should be generated
|
2018-07-18 00:57:03 +08:00
|
|
|
* @param {GenerateContext} generateContext context for generate
|
2018-04-28 00:53:07 +08:00
|
|
|
* @returns {Source} generated code
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
generate(
|
|
|
|
module,
|
|
|
|
{ dependencyTemplates, runtimeTemplate, moduleGraph, type }
|
|
|
|
) {
|
2019-09-28 01:50:23 +08:00
|
|
|
throw new AbstractMethodError();
|
2018-04-28 00:53:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ByTypeGenerator extends Generator {
|
|
|
|
constructor(map) {
|
|
|
|
super();
|
|
|
|
this.map = map;
|
2018-12-03 22:00:32 +08:00
|
|
|
this._types = new Set(Object.keys(map));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Set<string>} available types (do not mutate)
|
|
|
|
*/
|
|
|
|
getTypes() {
|
|
|
|
return this._types;
|
2018-04-28 00:53:07 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 18:23:40 +08:00
|
|
|
/**
|
|
|
|
* @param {NormalModule} module the module
|
|
|
|
* @param {string=} type source type
|
|
|
|
* @returns {number} estimate size of the module
|
|
|
|
*/
|
|
|
|
getSize(module, type) {
|
|
|
|
const t = type || "javascript";
|
|
|
|
const generator = this.map[t];
|
|
|
|
return generator ? generator.getSize(module, t) : 0;
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:17:51 +08:00
|
|
|
/**
|
|
|
|
* @param {NormalModule} module module for which the code should be generated
|
2018-07-18 00:57:03 +08:00
|
|
|
* @param {GenerateContext} generateContext context for generate
|
2018-07-21 00:17:51 +08:00
|
|
|
* @returns {Source} generated code
|
|
|
|
*/
|
2018-07-18 00:57:03 +08:00
|
|
|
generate(module, generateContext) {
|
|
|
|
const type = generateContext.type;
|
2018-04-28 00:53:07 +08:00
|
|
|
const generator = this.map[type];
|
|
|
|
if (!generator) {
|
|
|
|
throw new Error(`Generator.byType: no generator specified for ${type}`);
|
|
|
|
}
|
2018-07-18 00:57:03 +08:00
|
|
|
return generator.generate(module, generateContext);
|
2018-04-28 00:53:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Generator;
|