source: frontend/node_modules/webpack/lib/asset/AssetBytesGenerator.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.1 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Alexander Akait @alexander-akait
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 encodedSource = originalSource.buffer().toString("base64");
62
63 runtimeRequirements.add(RuntimeGlobals.requireScope);
64 runtimeRequirements.add(RuntimeGlobals.toBinary);
65
66 /** @type {string} */
67 let sourceContent;
68 if (concatenationScope) {
69 concatenationScope.registerNamespaceExport(
70 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
71 );
72 sourceContent = `${runtimeTemplate.renderConst()} ${
73 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
74 } = ${RuntimeGlobals.toBinary}(${JSON.stringify(encodedSource)});`;
75 } else {
76 runtimeRequirements.add(RuntimeGlobals.module);
77 sourceContent = `${module.moduleArgument}.exports = ${RuntimeGlobals.toBinary}(${JSON.stringify(
78 encodedSource
79 )});`;
80 }
81 return new RawSource(sourceContent);
82 }
83 case ASSET_URL_TYPE: {
84 if (!originalSource) {
85 return null;
86 }
87
88 const encodedSource = originalSource.buffer().toString("base64");
89
90 if (data) {
91 data.set("url", {
92 [type]: `data:application/octet-stream;base64,${encodedSource}`
93 });
94 }
95 return null;
96 }
97 default:
98 return null;
99 }
100 }
101
102 /**
103 * Generates fallback output for the provided error condition.
104 * @param {Error} error the error
105 * @param {NormalModule} module module for which the code should be generated
106 * @param {GenerateContext} generateContext context for generate
107 * @returns {Source | null} generated code
108 */
109 generateError(error, module, generateContext) {
110 switch (generateContext.type) {
111 case JAVASCRIPT_TYPE: {
112 return new RawSource(
113 `throw new Error(${JSON.stringify(error.message)});`
114 );
115 }
116 default:
117 return null;
118 }
119 }
120
121 /**
122 * Returns the reason this module cannot be concatenated, when one exists.
123 * @param {NormalModule} module module for which the bailout reason should be determined
124 * @param {ConcatenationBailoutReasonContext} context context
125 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
126 */
127 getConcatenationBailoutReason(module, context) {
128 return undefined;
129 }
130
131 /**
132 * Returns the source types available for this module.
133 * @param {NormalModule} module fresh module
134 * @returns {SourceTypes} available types (do not mutate)
135 */
136 getTypes(module) {
137 /** @type {Set<string>} */
138 const sourceTypes = new Set();
139 const connections = this._moduleGraph.getIncomingConnections(module);
140
141 for (const connection of connections) {
142 if (!connection.originModule) {
143 continue;
144 }
145
146 sourceTypes.add(connection.originModule.type.split("/")[0]);
147 }
148
149 if (sourceTypes.size > 0) {
150 if (
151 sourceTypes.has(JAVASCRIPT_TYPE) &&
152 (sourceTypes.has(CSS_TYPE) || sourceTypes.has(HTML_TYPE))
153 ) {
154 return JAVASCRIPT_AND_ASSET_URL_TYPES;
155 } else if (sourceTypes.has(CSS_TYPE) || sourceTypes.has(HTML_TYPE)) {
156 return ASSET_URL_TYPES;
157 }
158 return JAVASCRIPT_TYPES;
159 }
160
161 return NO_TYPES;
162 }
163
164 /**
165 * Returns the estimated size for the requested source type.
166 * @param {NormalModule} module the module
167 * @param {SourceType=} type source type
168 * @returns {number} estimate size of the module
169 */
170 getSize(module, type) {
171 const originalSource = module.originalSource();
172
173 if (!originalSource) {
174 return 0;
175 }
176
177 // Example: m.exports="abcd"
178 return originalSource.size() + 12;
179 }
180}
181
182module.exports = AssetSourceGenerator;
Note: See TracBrowser for help on using the repository browser.