[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { OriginalSource, RawSource } = require("webpack-sources");
|
---|
| 9 | const Module = require("./Module");
|
---|
| 10 | const { JS_TYPES } = require("./ModuleSourceTypesConstants");
|
---|
| 11 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
|
---|
| 12 | const makeSerializable = require("./util/makeSerializable");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 15 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 16 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 17 | /** @typedef {import("./Compilation")} Compilation */
|
---|
| 18 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 19 | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
---|
| 20 | /** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
---|
| 21 | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
---|
| 22 | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
---|
| 23 | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
---|
| 24 | /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
---|
| 25 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
---|
| 26 | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
| 27 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
---|
| 28 | /** @typedef {import("./WebpackError")} WebpackError */
|
---|
| 29 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 30 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 31 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 32 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
---|
| 33 |
|
---|
| 34 | class RawModule extends Module {
|
---|
| 35 | /**
|
---|
| 36 | * @param {string} source source code
|
---|
| 37 | * @param {string} identifier unique identifier
|
---|
| 38 | * @param {string=} readableIdentifier readable identifier
|
---|
| 39 | * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
|
---|
| 40 | */
|
---|
| 41 | constructor(source, identifier, readableIdentifier, runtimeRequirements) {
|
---|
| 42 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
|
---|
| 43 | this.sourceStr = source;
|
---|
| 44 | this.identifierStr = identifier || this.sourceStr;
|
---|
| 45 | this.readableIdentifierStr = readableIdentifier || this.identifierStr;
|
---|
| 46 | this.runtimeRequirements = runtimeRequirements || null;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * @returns {SourceTypes} types available (do not mutate)
|
---|
| 51 | */
|
---|
| 52 | getSourceTypes() {
|
---|
| 53 | return JS_TYPES;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * @returns {string} a unique identifier of the module
|
---|
| 58 | */
|
---|
| 59 | identifier() {
|
---|
| 60 | return this.identifierStr;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * @param {string=} type the source type for which the size should be estimated
|
---|
| 65 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
| 66 | */
|
---|
| 67 | size(type) {
|
---|
| 68 | return Math.max(1, this.sourceStr.length);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * @param {RequestShortener} requestShortener the request shortener
|
---|
| 73 | * @returns {string} a user readable identifier of the module
|
---|
| 74 | */
|
---|
| 75 | readableIdentifier(requestShortener) {
|
---|
| 76 | return /** @type {string} */ (
|
---|
| 77 | requestShortener.shorten(this.readableIdentifierStr)
|
---|
| 78 | );
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * @param {NeedBuildContext} context context info
|
---|
| 83 | * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
| 84 | * @returns {void}
|
---|
| 85 | */
|
---|
| 86 | needBuild(context, callback) {
|
---|
| 87 | return callback(null, !this.buildMeta);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * @param {WebpackOptions} options webpack options
|
---|
| 92 | * @param {Compilation} compilation the compilation
|
---|
| 93 | * @param {ResolverWithOptions} resolver the resolver
|
---|
| 94 | * @param {InputFileSystem} fs the file system
|
---|
| 95 | * @param {function(WebpackError=): void} callback callback function
|
---|
| 96 | * @returns {void}
|
---|
| 97 | */
|
---|
| 98 | build(options, compilation, resolver, fs, callback) {
|
---|
| 99 | this.buildMeta = {};
|
---|
| 100 | this.buildInfo = {
|
---|
| 101 | cacheable: true
|
---|
| 102 | };
|
---|
| 103 | callback();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * @param {CodeGenerationContext} context context for code generation
|
---|
| 108 | * @returns {CodeGenerationResult} result
|
---|
| 109 | */
|
---|
| 110 | codeGeneration(context) {
|
---|
| 111 | const sources = new Map();
|
---|
| 112 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
---|
| 113 | sources.set(
|
---|
| 114 | "javascript",
|
---|
| 115 | new OriginalSource(this.sourceStr, this.identifier())
|
---|
| 116 | );
|
---|
| 117 | } else {
|
---|
| 118 | sources.set("javascript", new RawSource(this.sourceStr));
|
---|
| 119 | }
|
---|
| 120 | return { sources, runtimeRequirements: this.runtimeRequirements };
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * @param {Hash} hash the hash used to track dependencies
|
---|
| 125 | * @param {UpdateHashContext} context context
|
---|
| 126 | * @returns {void}
|
---|
| 127 | */
|
---|
| 128 | updateHash(hash, context) {
|
---|
| 129 | hash.update(this.sourceStr);
|
---|
| 130 | super.updateHash(hash, context);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @param {ObjectSerializerContext} context context
|
---|
| 135 | */
|
---|
| 136 | serialize(context) {
|
---|
| 137 | const { write } = context;
|
---|
| 138 |
|
---|
| 139 | write(this.sourceStr);
|
---|
| 140 | write(this.identifierStr);
|
---|
| 141 | write(this.readableIdentifierStr);
|
---|
| 142 | write(this.runtimeRequirements);
|
---|
| 143 |
|
---|
| 144 | super.serialize(context);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * @param {ObjectDeserializerContext} context context
|
---|
| 149 | */
|
---|
| 150 | deserialize(context) {
|
---|
| 151 | const { read } = context;
|
---|
| 152 |
|
---|
| 153 | this.sourceStr = read();
|
---|
| 154 | this.identifierStr = read();
|
---|
| 155 | this.readableIdentifierStr = read();
|
---|
| 156 | this.runtimeRequirements = read();
|
---|
| 157 |
|
---|
| 158 | super.deserialize(context);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | makeSerializable(RawModule, "webpack/lib/RawModule");
|
---|
| 163 |
|
---|
| 164 | module.exports = RawModule;
|
---|