1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Sergey Melyukov @smelukov
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { RawSource } = require("webpack-sources");
|
---|
9 | const ConcatenationScope = require("../ConcatenationScope");
|
---|
10 | const Generator = require("../Generator");
|
---|
11 | const {
|
---|
12 | NO_TYPES,
|
---|
13 | CSS_URL_TYPES,
|
---|
14 | JS_TYPES,
|
---|
15 | JS_AND_CSS_URL_TYPES
|
---|
16 | } = require("../ModuleSourceTypesConstants");
|
---|
17 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
18 |
|
---|
19 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
20 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
---|
21 | /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
---|
22 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
---|
23 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
24 | /** @typedef {import("../NormalModule")} NormalModule */
|
---|
25 |
|
---|
26 | class AssetSourceGenerator extends Generator {
|
---|
27 | /**
|
---|
28 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
29 | */
|
---|
30 | constructor(moduleGraph) {
|
---|
31 | super();
|
---|
32 |
|
---|
33 | this._moduleGraph = moduleGraph;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param {NormalModule} module module for which the code should be generated
|
---|
38 | * @param {GenerateContext} generateContext context for generate
|
---|
39 | * @returns {Source | null} generated code
|
---|
40 | */
|
---|
41 | generate(
|
---|
42 | module,
|
---|
43 | { type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
|
---|
44 | ) {
|
---|
45 | const originalSource = module.originalSource();
|
---|
46 | const data = getData ? getData() : undefined;
|
---|
47 |
|
---|
48 | switch (type) {
|
---|
49 | case "javascript": {
|
---|
50 | if (!originalSource) {
|
---|
51 | return new RawSource("");
|
---|
52 | }
|
---|
53 |
|
---|
54 | const content = originalSource.source();
|
---|
55 | const encodedSource =
|
---|
56 | typeof content === "string" ? content : content.toString("utf-8");
|
---|
57 |
|
---|
58 | let sourceContent;
|
---|
59 | if (concatenationScope) {
|
---|
60 | concatenationScope.registerNamespaceExport(
|
---|
61 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
---|
62 | );
|
---|
63 | sourceContent = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
---|
64 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
---|
65 | } = ${JSON.stringify(encodedSource)};`;
|
---|
66 | } else {
|
---|
67 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
68 | sourceContent = `${RuntimeGlobals.module}.exports = ${JSON.stringify(
|
---|
69 | encodedSource
|
---|
70 | )};`;
|
---|
71 | }
|
---|
72 | return new RawSource(sourceContent);
|
---|
73 | }
|
---|
74 | case "css-url": {
|
---|
75 | if (!originalSource) {
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | const content = originalSource.source();
|
---|
80 | const encodedSource =
|
---|
81 | typeof content === "string" ? content : content.toString("utf-8");
|
---|
82 |
|
---|
83 | if (data) {
|
---|
84 | data.set("url", { [type]: encodedSource });
|
---|
85 | }
|
---|
86 | return null;
|
---|
87 | }
|
---|
88 | default:
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @param {NormalModule} module module for which the bailout reason should be determined
|
---|
95 | * @param {ConcatenationBailoutReasonContext} context context
|
---|
96 | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
---|
97 | */
|
---|
98 | getConcatenationBailoutReason(module, context) {
|
---|
99 | return undefined;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @param {NormalModule} module fresh module
|
---|
104 | * @returns {SourceTypes} available types (do not mutate)
|
---|
105 | */
|
---|
106 | getTypes(module) {
|
---|
107 | const sourceTypes = new Set();
|
---|
108 | const connections = this._moduleGraph.getIncomingConnections(module);
|
---|
109 |
|
---|
110 | for (const connection of connections) {
|
---|
111 | if (!connection.originModule) {
|
---|
112 | continue;
|
---|
113 | }
|
---|
114 |
|
---|
115 | sourceTypes.add(connection.originModule.type.split("/")[0]);
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
---|
119 | return JS_AND_CSS_URL_TYPES;
|
---|
120 | } else if (sourceTypes.has("javascript")) {
|
---|
121 | return JS_TYPES;
|
---|
122 | } else if (sourceTypes.has("css")) {
|
---|
123 | return CSS_URL_TYPES;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return NO_TYPES;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * @param {NormalModule} module the module
|
---|
131 | * @param {string=} type source type
|
---|
132 | * @returns {number} estimate size of the module
|
---|
133 | */
|
---|
134 | getSize(module, type) {
|
---|
135 | const originalSource = module.originalSource();
|
---|
136 |
|
---|
137 | if (!originalSource) {
|
---|
138 | return 0;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // Example: m.exports="abcd"
|
---|
142 | return originalSource.size() + 12;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | module.exports = AssetSourceGenerator;
|
---|