| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Florent Cailhol @ooflorent
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const DependencyTemplate = require("../DependencyTemplate");
|
|---|
| 9 | const InitFragment = require("../InitFragment");
|
|---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 11 | const NullDependency = require("./NullDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 15 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 16 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 20 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 21 |
|
|---|
| 22 | class CachedConstDependency extends NullDependency {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of CachedConstDependency.
|
|---|
| 25 | * @param {string} expression expression
|
|---|
| 26 | * @param {Range | null} range range
|
|---|
| 27 | * @param {string} identifier identifier
|
|---|
| 28 | * @param {number=} place place where we inject the expression
|
|---|
| 29 | */
|
|---|
| 30 | constructor(
|
|---|
| 31 | expression,
|
|---|
| 32 | range,
|
|---|
| 33 | identifier,
|
|---|
| 34 | place = CachedConstDependency.PLACE_MODULE
|
|---|
| 35 | ) {
|
|---|
| 36 | super();
|
|---|
| 37 |
|
|---|
| 38 | this.expression = expression;
|
|---|
| 39 | this.range = range;
|
|---|
| 40 | this.identifier = identifier;
|
|---|
| 41 | this.place = place;
|
|---|
| 42 | /** @type {undefined | string} */
|
|---|
| 43 | this._hashUpdate = undefined;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Create hash update.
|
|---|
| 48 | * @returns {string} hash update
|
|---|
| 49 | */
|
|---|
| 50 | _createHashUpdate() {
|
|---|
| 51 | return `${this.place}${this.identifier}${this.range}${this.expression}`;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Updates the hash with the data contributed by this instance.
|
|---|
| 56 | * @param {Hash} hash hash to be updated
|
|---|
| 57 | * @param {UpdateHashContext} context context
|
|---|
| 58 | * @returns {void}
|
|---|
| 59 | */
|
|---|
| 60 | updateHash(hash, context) {
|
|---|
| 61 | if (this._hashUpdate === undefined) {
|
|---|
| 62 | this._hashUpdate = this._createHashUpdate();
|
|---|
| 63 | }
|
|---|
| 64 | hash.update(this._hashUpdate);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Serializes this instance into the provided serializer context.
|
|---|
| 69 | * @param {ObjectSerializerContext} context context
|
|---|
| 70 | */
|
|---|
| 71 | serialize(context) {
|
|---|
| 72 | const { write } = context;
|
|---|
| 73 |
|
|---|
| 74 | write(this.expression);
|
|---|
| 75 | write(this.range);
|
|---|
| 76 | write(this.identifier);
|
|---|
| 77 | write(this.place);
|
|---|
| 78 |
|
|---|
| 79 | super.serialize(context);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Restores this instance from the provided deserializer context.
|
|---|
| 84 | * @param {ObjectDeserializerContext} context context
|
|---|
| 85 | */
|
|---|
| 86 | deserialize(context) {
|
|---|
| 87 | const { read } = context;
|
|---|
| 88 |
|
|---|
| 89 | this.expression = read();
|
|---|
| 90 | this.range = read();
|
|---|
| 91 | this.identifier = read();
|
|---|
| 92 | this.place = read();
|
|---|
| 93 |
|
|---|
| 94 | super.deserialize(context);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | CachedConstDependency.PLACE_MODULE = 10;
|
|---|
| 99 | CachedConstDependency.PLACE_CHUNK = 20;
|
|---|
| 100 |
|
|---|
| 101 | makeSerializable(
|
|---|
| 102 | CachedConstDependency,
|
|---|
| 103 | "webpack/lib/dependencies/CachedConstDependency"
|
|---|
| 104 | );
|
|---|
| 105 |
|
|---|
| 106 | CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
|
|---|
| 107 | DependencyTemplate
|
|---|
| 108 | ) {
|
|---|
| 109 | /**
|
|---|
| 110 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 111 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 112 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 113 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 114 | * @returns {void}
|
|---|
| 115 | */
|
|---|
| 116 | apply(dependency, source, { initFragments, chunkInitFragments }) {
|
|---|
| 117 | const dep = /** @type {CachedConstDependency} */ (dependency);
|
|---|
| 118 |
|
|---|
| 119 | (dep.place === CachedConstDependency.PLACE_MODULE
|
|---|
| 120 | ? initFragments
|
|---|
| 121 | : chunkInitFragments
|
|---|
| 122 | ).push(
|
|---|
| 123 | new InitFragment(
|
|---|
| 124 | `var ${dep.identifier} = ${dep.expression};\n`,
|
|---|
| 125 | InitFragment.STAGE_CONSTANTS,
|
|---|
| 126 | // For a chunk we inject expression after imports
|
|---|
| 127 | dep.place === CachedConstDependency.PLACE_MODULE ? 0 : 10,
|
|---|
| 128 | `const ${dep.identifier}`
|
|---|
| 129 | )
|
|---|
| 130 | );
|
|---|
| 131 |
|
|---|
| 132 | if (typeof dep.range === "number") {
|
|---|
| 133 | source.insert(dep.range, dep.identifier);
|
|---|
| 134 | } else if (dep.range !== null) {
|
|---|
| 135 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | module.exports = CachedConstDependency;
|
|---|