webpack/lib/dependencies/ConstDependency.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-07-30 23:08:51 +08:00
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
const NullDependency = require("./NullDependency");
2013-01-31 01:49:25 +08:00
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2018-07-25 15:33:48 +08:00
/** @typedef {import("../util/createHash").Hash} Hash */
class ConstDependency extends NullDependency {
constructor(expression, range, requireWebpackRequire) {
super();
this.expression = expression;
this.range = range;
this.requireWebpackRequire = requireWebpackRequire;
}
2013-01-31 01:49:25 +08:00
2018-07-25 15:33:48 +08:00
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {ChunkGraph} chunkGraph chunk graph
2018-07-25 15:33:48 +08:00
* @returns {void}
*/
updateHash(hash, chunkGraph) {
hash.update(this.range + "");
hash.update(this.expression + "");
}
2018-10-09 20:30:59 +08:00
serialize(context) {
const { write } = context;
write(this.expression);
write(this.range);
write(this.requireWebpackRequire);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.expression = read();
this.range = read();
this.requireWebpackRequire = read();
super.deserialize(context);
}
}
2013-01-31 01:49:25 +08:00
2018-10-09 20:30:59 +08:00
makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
ConstDependency.Template = class ConstDependencyTemplate extends NullDependency.Template {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(dependency, source, templateContext) {
const dep = /** @type {ConstDependency} */ (dependency);
2018-02-25 09:00:20 +08:00
if (typeof dep.range === "number") {
source.insert(dep.range, dep.expression);
return;
}
2013-01-31 01:49:25 +08:00
2015-04-24 05:55:50 +08:00
source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
}
2017-01-11 17:51:58 +08:00
};
2015-09-03 20:17:11 +08:00
module.exports = ConstDependency;