123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- "use strict";
- const { ReplaceSource } = require("webpack-sources");
- const Generator = require("../Generator");
- const InitFragment = require("../InitFragment");
- const RuntimeGlobals = require("../RuntimeGlobals");
- const TYPES = new Set(["css"]);
- class CssGenerator extends Generator {
-
- constructor(convention, localIdentName, esModule) {
- super();
-
- this.convention = convention;
-
- this.localIdentName = localIdentName;
-
- this.esModule = esModule;
- }
-
- generate(module, generateContext) {
- const originalSource = (module.originalSource());
- const source = new ReplaceSource(originalSource);
-
- const initFragments = [];
-
- const cssExportsData = {
- esModule: this.esModule,
- exports: new Map()
- };
- generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
- let chunkInitFragments;
-
- const templateContext = {
- runtimeTemplate: generateContext.runtimeTemplate,
- dependencyTemplates: generateContext.dependencyTemplates,
- moduleGraph: generateContext.moduleGraph,
- chunkGraph: generateContext.chunkGraph,
- module,
- runtime: generateContext.runtime,
- runtimeRequirements: generateContext.runtimeRequirements,
- concatenationScope: generateContext.concatenationScope,
- codeGenerationResults: generateContext.codeGenerationResults,
- initFragments,
- cssExportsData,
- get chunkInitFragments() {
- if (!chunkInitFragments) {
- const data = generateContext.getData();
- chunkInitFragments = data.get("chunkInitFragments");
- if (!chunkInitFragments) {
- chunkInitFragments = [];
- data.set("chunkInitFragments", chunkInitFragments);
- }
- }
- return chunkInitFragments;
- }
- };
-
- const handleDependency = dependency => {
- const constructor = /** @type {new (...args: any[]) => Dependency} */ (
- dependency.constructor
- );
- const template = generateContext.dependencyTemplates.get(constructor);
- if (!template) {
- throw new Error(
- "No template for dependency: " + dependency.constructor.name
- );
- }
- template.apply(dependency, source, templateContext);
- };
- module.dependencies.forEach(handleDependency);
- if (module.presentationalDependencies !== undefined)
- module.presentationalDependencies.forEach(handleDependency);
- const data = generateContext.getData();
- data.set("css-exports", cssExportsData);
- return InitFragment.addToSource(source, initFragments, generateContext);
- }
- /**
- * @param {NormalModule} module fresh module
- * @returns {Set<string>} available types (do not mutate)
- */
- getTypes(module) {
- return TYPES;
- }
- /**
- * @param {NormalModule} module the module
- * @param {string=} type source type
- * @returns {number} estimate size of the module
- */
- getSize(module, type) {
- const originalSource = module.originalSource();
- if (!originalSource) {
- return 0;
- }
- return originalSource.size();
- }
- /**
- * @param {Hash} hash hash that will be modified
- * @param {UpdateHashContext} updateHashContext context for updating hash
- */
- updateHash(hash, { module }) {
- hash.update(this.esModule.toString());
- }
- }
- module.exports = CssGenerator;
|