| 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 {
|
|---|
| 11 | JAVASCRIPT_TYPE,
|
|---|
| 12 | JAVASCRIPT_TYPES
|
|---|
| 13 | } = require("./ModuleSourceTypeConstants");
|
|---|
| 14 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
|
|---|
| 15 | const makeSerializable = require("./util/makeSerializable");
|
|---|
| 16 |
|
|---|
| 17 | /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 18 | /** @typedef {import("./Compilation")} Compilation */
|
|---|
| 19 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 20 | /** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
|---|
| 21 | /** @typedef {import("./Module").BuildCallback} BuildCallback */
|
|---|
| 22 | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 23 | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 24 | /** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 25 | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 26 | /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
|
|---|
| 27 | /** @typedef {import("./Module").Sources} Sources */
|
|---|
| 28 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
|---|
| 29 | /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
|
|---|
| 30 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
|---|
| 31 | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 32 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 33 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 34 | /** @typedef {import("./util/Hash")} Hash */
|
|---|
| 35 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 36 |
|
|---|
| 37 | class RawModule extends Module {
|
|---|
| 38 | /**
|
|---|
| 39 | * Creates an instance of RawModule.
|
|---|
| 40 | * @param {string} source source code
|
|---|
| 41 | * @param {string} identifier unique identifier
|
|---|
| 42 | * @param {string=} readableIdentifier readable identifier
|
|---|
| 43 | * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
|
|---|
| 44 | */
|
|---|
| 45 | constructor(source, identifier, readableIdentifier, runtimeRequirements) {
|
|---|
| 46 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
|
|---|
| 47 | this.sourceStr = source;
|
|---|
| 48 | this.identifierStr = identifier || this.sourceStr;
|
|---|
| 49 | this.readableIdentifierStr = readableIdentifier || this.identifierStr;
|
|---|
| 50 | this.runtimeRequirements = runtimeRequirements || null;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Returns the source types this module can generate.
|
|---|
| 55 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 56 | */
|
|---|
| 57 | getSourceTypes() {
|
|---|
| 58 | return JAVASCRIPT_TYPES;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Returns the unique identifier used to reference this module.
|
|---|
| 63 | * @returns {string} a unique identifier of the module
|
|---|
| 64 | */
|
|---|
| 65 | identifier() {
|
|---|
| 66 | return this.identifierStr;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Returns the estimated size for the requested source type.
|
|---|
| 71 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 72 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 73 | */
|
|---|
| 74 | size(type) {
|
|---|
| 75 | return Math.max(1, this.sourceStr.length);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Returns a human-readable identifier for this module.
|
|---|
| 80 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 81 | * @returns {string} a user readable identifier of the module
|
|---|
| 82 | */
|
|---|
| 83 | readableIdentifier(requestShortener) {
|
|---|
| 84 | return /** @type {string} */ (
|
|---|
| 85 | requestShortener.shorten(this.readableIdentifierStr)
|
|---|
| 86 | );
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 91 | * @param {NeedBuildContext} context context info
|
|---|
| 92 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 93 | * @returns {void}
|
|---|
| 94 | */
|
|---|
| 95 | needBuild(context, callback) {
|
|---|
| 96 | return callback(null, !this.buildMeta);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Builds the module using the provided compilation context.
|
|---|
| 101 | * @param {WebpackOptions} options webpack options
|
|---|
| 102 | * @param {Compilation} compilation the compilation
|
|---|
| 103 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 104 | * @param {InputFileSystem} fs the file system
|
|---|
| 105 | * @param {BuildCallback} callback callback function
|
|---|
| 106 | * @returns {void}
|
|---|
| 107 | */
|
|---|
| 108 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 109 | this.buildMeta = {};
|
|---|
| 110 | this.buildInfo = {
|
|---|
| 111 | cacheable: true
|
|---|
| 112 | };
|
|---|
| 113 | callback();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Gets side effects connection state.
|
|---|
| 118 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 119 | * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
|
|---|
| 120 | */
|
|---|
| 121 | getSideEffectsConnectionState(moduleGraph) {
|
|---|
| 122 | if (this.factoryMeta !== undefined) {
|
|---|
| 123 | if (this.factoryMeta.sideEffectFree) return false;
|
|---|
| 124 | if (this.factoryMeta.sideEffectFree === false) return true;
|
|---|
| 125 | }
|
|---|
| 126 | return true;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Generates code and runtime requirements for this module.
|
|---|
| 131 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 132 | * @returns {CodeGenerationResult} result
|
|---|
| 133 | */
|
|---|
| 134 | codeGeneration(context) {
|
|---|
| 135 | /** @type {Sources} */
|
|---|
| 136 | const sources = new Map();
|
|---|
| 137 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
|---|
| 138 | sources.set(
|
|---|
| 139 | JAVASCRIPT_TYPE,
|
|---|
| 140 | new OriginalSource(this.sourceStr, this.identifier())
|
|---|
| 141 | );
|
|---|
| 142 | } else {
|
|---|
| 143 | sources.set(JAVASCRIPT_TYPE, new RawSource(this.sourceStr));
|
|---|
| 144 | }
|
|---|
| 145 | return { sources, runtimeRequirements: this.runtimeRequirements };
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Updates the hash with the data contributed by this instance.
|
|---|
| 150 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 151 | * @param {UpdateHashContext} context context
|
|---|
| 152 | * @returns {void}
|
|---|
| 153 | */
|
|---|
| 154 | updateHash(hash, context) {
|
|---|
| 155 | hash.update(this.sourceStr);
|
|---|
| 156 | super.updateHash(hash, context);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Serializes this instance into the provided serializer context.
|
|---|
| 161 | * @param {ObjectSerializerContext} context context
|
|---|
| 162 | */
|
|---|
| 163 | serialize(context) {
|
|---|
| 164 | const { write } = context;
|
|---|
| 165 |
|
|---|
| 166 | write(this.sourceStr);
|
|---|
| 167 | write(this.identifierStr);
|
|---|
| 168 | write(this.readableIdentifierStr);
|
|---|
| 169 | write(this.runtimeRequirements);
|
|---|
| 170 |
|
|---|
| 171 | super.serialize(context);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * Restores this instance from the provided deserializer context.
|
|---|
| 176 | * @param {ObjectDeserializerContext} context context
|
|---|
| 177 | */
|
|---|
| 178 | deserialize(context) {
|
|---|
| 179 | const { read } = context;
|
|---|
| 180 |
|
|---|
| 181 | this.sourceStr = read();
|
|---|
| 182 | this.identifierStr = read();
|
|---|
| 183 | this.readableIdentifierStr = read();
|
|---|
| 184 | this.runtimeRequirements = read();
|
|---|
| 185 |
|
|---|
| 186 | super.deserialize(context);
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | makeSerializable(RawModule, "webpack/lib/RawModule");
|
|---|
| 191 |
|
|---|
| 192 | module.exports = RawModule;
|
|---|