source: frontend/node_modules/webpack/lib/dependencies/RequireEnsureDependency.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const makeSerializable = require("../util/makeSerializable");
10const NullDependency = require("./NullDependency");
11
12/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
14/** @typedef {import("../Dependency")} Dependency */
15/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
16/** @typedef {import("../javascript/JavascriptParser").Range} Range */
17/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
18/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
19
20class RequireEnsureDependency extends NullDependency {
21 /**
22 * Creates an instance of RequireEnsureDependency.
23 * @param {Range} range range
24 * @param {Range} contentRange content range
25 * @param {Range | false} errorHandlerRange error handler range
26 */
27 constructor(range, contentRange, errorHandlerRange) {
28 super();
29
30 this.range = range;
31 this.contentRange = contentRange;
32 this.errorHandlerRange = errorHandlerRange;
33 }
34
35 get type() {
36 return "require.ensure";
37 }
38
39 /**
40 * Serializes this instance into the provided serializer context.
41 * @param {ObjectSerializerContext} context context
42 */
43 serialize(context) {
44 const { write } = context;
45
46 write(this.range);
47 write(this.contentRange);
48 write(this.errorHandlerRange);
49
50 super.serialize(context);
51 }
52
53 /**
54 * Restores this instance from the provided deserializer context.
55 * @param {ObjectDeserializerContext} context context
56 */
57 deserialize(context) {
58 const { read } = context;
59
60 this.range = read();
61 this.contentRange = read();
62 this.errorHandlerRange = read();
63
64 super.deserialize(context);
65 }
66}
67
68makeSerializable(
69 RequireEnsureDependency,
70 "webpack/lib/dependencies/RequireEnsureDependency"
71);
72
73RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate extends (
74 NullDependency.Template
75) {
76 /**
77 * Applies the plugin by registering its hooks on the compiler.
78 * @param {Dependency} dependency the dependency for which the template should be applied
79 * @param {ReplaceSource} source the current replace source which can be modified
80 * @param {DependencyTemplateContext} templateContext the context object
81 * @returns {void}
82 */
83 apply(
84 dependency,
85 source,
86 { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
87 ) {
88 const dep = /** @type {RequireEnsureDependency} */ (dependency);
89 const depBlock = /** @type {AsyncDependenciesBlock} */ (
90 moduleGraph.getParentBlock(dep)
91 );
92 const promise = runtimeTemplate.blockPromise({
93 chunkGraph,
94 block: depBlock,
95 message: "require.ensure",
96 runtimeRequirements
97 });
98 const range = dep.range;
99 const contentRange = dep.contentRange;
100 const errorHandlerRange = dep.errorHandlerRange;
101 source.replace(range[0], contentRange[0] - 1, `${promise}.then((`);
102 if (errorHandlerRange) {
103 source.replace(
104 contentRange[1],
105 errorHandlerRange[0] - 1,
106 `).bind(null, ${RuntimeGlobals.require}))['catch'](`
107 );
108 source.replace(errorHandlerRange[1], range[1] - 1, ")");
109 } else {
110 source.replace(
111 contentRange[1],
112 range[1] - 1,
113 `).bind(null, ${RuntimeGlobals.require}))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`
114 );
115 }
116 }
117};
118
119module.exports = RequireEnsureDependency;
Note: See TracBrowser for help on using the repository browser.