[6a3a178] | 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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 | const NullDependency = require("./NullDependency");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 15 |
|
---|
| 16 | class RequireHeaderDependency extends NullDependency {
|
---|
| 17 | constructor(range) {
|
---|
| 18 | super();
|
---|
| 19 | if (!Array.isArray(range)) throw new Error("range must be valid");
|
---|
| 20 | this.range = range;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | serialize(context) {
|
---|
| 24 | const { write } = context;
|
---|
| 25 | write(this.range);
|
---|
| 26 | super.serialize(context);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | static deserialize(context) {
|
---|
| 30 | const obj = new RequireHeaderDependency(context.read());
|
---|
| 31 | obj.deserialize(context);
|
---|
| 32 | return obj;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | makeSerializable(
|
---|
| 37 | RequireHeaderDependency,
|
---|
| 38 | "webpack/lib/dependencies/RequireHeaderDependency"
|
---|
| 39 | );
|
---|
| 40 |
|
---|
| 41 | RequireHeaderDependency.Template = class RequireHeaderDependencyTemplate extends (
|
---|
| 42 | NullDependency.Template
|
---|
| 43 | ) {
|
---|
| 44 | /**
|
---|
| 45 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 46 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 47 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 48 | * @returns {void}
|
---|
| 49 | */
|
---|
| 50 | apply(dependency, source, { runtimeRequirements }) {
|
---|
| 51 | const dep = /** @type {RequireHeaderDependency} */ (dependency);
|
---|
| 52 | runtimeRequirements.add(RuntimeGlobals.require);
|
---|
| 53 | source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__");
|
---|
| 54 | }
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | module.exports = RequireHeaderDependency;
|
---|