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
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
"use strict";
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
const NullDependency = require("./NullDependency");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
2018-07-18 01:38:42 +08:00
|
|
|
/** @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 */
|
2018-07-23 23:33:29 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
class ConstDependency extends NullDependency {
|
2017-12-13 18:14:00 +08:00
|
|
|
constructor(expression, range, requireWebpackRequire) {
|
2017-01-04 04:40:14 +08:00
|
|
|
super();
|
|
|
|
this.expression = expression;
|
|
|
|
this.range = range;
|
2017-12-13 18:14:00 +08:00
|
|
|
this.requireWebpackRequire = requireWebpackRequire;
|
2017-01-04 04:40:14 +08:00
|
|
|
}
|
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
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
updateHash(hash, chunkGraph) {
|
2017-01-04 04:40:14 +08:00
|
|
|
hash.update(this.range + "");
|
|
|
|
hash.update(this.expression + "");
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
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
|
2018-07-18 01:38:42 +08:00
|
|
|
* @param {DependencyTemplateContext} templateContext the context object
|
2018-07-23 23:33:29 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-18 01:38:42 +08:00
|
|
|
apply(dependency, source, templateContext) {
|
2018-07-23 23:33:29 +08:00
|
|
|
const dep = /** @type {ConstDependency} */ (dependency);
|
2018-02-25 09:00:20 +08:00
|
|
|
if (typeof dep.range === "number") {
|
2017-01-04 04:40:14 +08:00
|
|
|
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-04 04:40:14 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2015-09-03 20:17:11 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
module.exports = ConstDependency;
|