source: frontend/node_modules/webpack/lib/asset/AssetSourceGenerator.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[9af201e]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sergey Melyukov @smelukov
4*/
5
6"use strict";
7
8const { RawSource } = require("webpack-sources");
9const ConcatenationScope = require("../ConcatenationScope");
10const Generator = require("../Generator");
11const {
12 ASSET_URL_TYPE,
13 ASSET_URL_TYPES,
14 CSS_TYPE,
15 HTML_TYPE,
16 JAVASCRIPT_AND_ASSET_URL_TYPES,
17 JAVASCRIPT_TYPE,
18 JAVASCRIPT_TYPES,
19 NO_TYPES
20} = require("../ModuleSourceTypeConstants");
21const RuntimeGlobals = require("../RuntimeGlobals");
22
23/** @typedef {import("webpack-sources").Source} Source */
24/** @typedef {import("../Generator").GenerateContext} GenerateContext */
25/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
26/** @typedef {import("../Module").SourceType} SourceType */
27/** @typedef {import("../Module").SourceTypes} SourceTypes */
28/** @typedef {import("../ModuleGraph")} ModuleGraph */
29/** @typedef {import("../NormalModule")} NormalModule */
30
31class AssetSourceGenerator extends Generator {
32 /**
33 * Creates an instance of AssetSourceGenerator.
34 * @param {ModuleGraph} moduleGraph the module graph
35 */
36 constructor(moduleGraph) {
37 super();
38
39 this._moduleGraph = moduleGraph;
40 }
41
42 /**
43 * Generates generated code for this runtime module.
44 * @param {NormalModule} module module for which the code should be generated
45 * @param {GenerateContext} generateContext context for generate
46 * @returns {Source | null} generated code
47 */
48 generate(
49 module,
50 { type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
51 ) {
52 const originalSource = module.originalSource();
53 const data = getData ? getData() : undefined;
54
55 switch (type) {
56 case JAVASCRIPT_TYPE: {
57 if (!originalSource) {
58 return new RawSource("");
59 }
60
61 const content = originalSource.source();
62 const encodedSource =
63 typeof content === "string" ? content : content.toString("utf8");
64
65 /** @type {string} */
66 let sourceContent;
67 if (concatenationScope) {
68 concatenationScope.registerNamespaceExport(
69 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
70 );
71 sourceContent = `${runtimeTemplate.renderConst()} ${
72 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
73 } = ${JSON.stringify(encodedSource)};`;
74 } else {
75 runtimeRequirements.add(RuntimeGlobals.module);
76 sourceContent = `${module.moduleArgument}.exports = ${JSON.stringify(
77 encodedSource
78 )};`;
79 }
80 return new RawSource(sourceContent);
81 }
82 case ASSET_URL_TYPE: {
83 if (!originalSource) {
84 return null;
85 }
86
87 const content = originalSource.source();
88 const encodedSource =
89 typeof content === "string" ? content : content.toString("utf8");
90
91 if (data) {
92 data.set("url", { [type]: encodedSource });
93 }
94 return null;
95 }
96 default:
97 return null;
98 }
99 }
100
101 /**
102 * Generates fallback output for the provided error condition.
103 * @param {Error} error the error
104 * @param {NormalModule} module module for which the code should be generated
105 * @param {GenerateContext} generateContext context for generate
106 * @returns {Source | null} generated code
107 */
108 generateError(error, module, generateContext) {
109 switch (generateContext.type) {
110 case JAVASCRIPT_TYPE: {
111 return new RawSource(
112 `throw new Error(${JSON.stringify(error.message)});`
113 );
114 }
115 default:
116 return null;
117 }
118 }
119
120 /**
121 * Returns the reason this module cannot be concatenated, when one exists.
122 * @param {NormalModule} module module for which the bailout reason should be determined
123 * @param {ConcatenationBailoutReasonContext} context context
124 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
125 */
126 getConcatenationBailoutReason(module, context) {
127 return undefined;
128 }
129
130 /**
131 * Returns the source types available for this module.
132 * @param {NormalModule} module fresh module
133 * @returns {SourceTypes} available types (do not mutate)
134 */
135 getTypes(module) {
136 /** @type {Set<string>} */
137 const sourceTypes = new Set();
138 const connections = this._moduleGraph.getIncomingConnections(module);
139
140 for (const connection of connections) {
141 if (!connection.originModule) {
142 continue;
143 }
144
145 sourceTypes.add(connection.originModule.type.split("/")[0]);
146 }
147
148 if (sourceTypes.size > 0) {
149 if (
150 sourceTypes.has(JAVASCRIPT_TYPE) &&
151 (sourceTypes.has(CSS_TYPE) || sourceTypes.has(HTML_TYPE))
152 ) {
153 return JAVASCRIPT_AND_ASSET_URL_TYPES;
154 } else if (sourceTypes.has(CSS_TYPE) || sourceTypes.has(HTML_TYPE)) {
155 return ASSET_URL_TYPES;
156 }
157 return JAVASCRIPT_TYPES;
158 }
159
160 return NO_TYPES;
161 }
162
163 /**
164 * Returns the estimated size for the requested source type.
165 * @param {NormalModule} module the module
166 * @param {SourceType=} type source type
167 * @returns {number} estimate size of the module
168 */
169 getSize(module, type) {
170 const originalSource = module.originalSource();
171
172 if (!originalSource) {
173 return 0;
174 }
175
176 // Example: m.exports="abcd"
177 return originalSource.size() + 12;
178 }
179}
180
181module.exports = AssetSourceGenerator;
Note: See TracBrowser for help on using the repository browser.