1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { RawSource } = require("webpack-sources");
|
9 | const Generator = require("../Generator");
|
10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const TYPES = new Set(["javascript"]);
|
17 |
|
18 | class AssetSourceGenerator extends Generator {
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 | generate(module, { chunkGraph, runtimeTemplate, runtimeRequirements }) {
|
25 | runtimeRequirements.add(RuntimeGlobals.module);
|
26 |
|
27 | const originalSource = module.originalSource();
|
28 |
|
29 | if (!originalSource) {
|
30 | return new RawSource("");
|
31 | }
|
32 |
|
33 | const content = originalSource.source();
|
34 |
|
35 | let encodedSource;
|
36 | if (typeof content === "string") {
|
37 | encodedSource = content;
|
38 | } else {
|
39 | encodedSource = content.toString("utf-8");
|
40 | }
|
41 | return new RawSource(
|
42 | `${RuntimeGlobals.module}.exports = ${JSON.stringify(encodedSource)};`
|
43 | );
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 | getTypes(module) {
|
51 | return TYPES;
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | getSize(module, type) {
|
60 | const originalSource = module.originalSource();
|
61 |
|
62 | if (!originalSource) {
|
63 | return 0;
|
64 | }
|
65 |
|
66 |
|
67 | return originalSource.size() + 12;
|
68 | }
|
69 | }
|
70 |
|
71 | module.exports = AssetSourceGenerator;
|