UNPKG

2.73 kBJavaScriptView Raw
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
17class RequireEnsureDependency extends NullDependency {
18 constructor(range, contentRange, errorHandlerRange) {
19 super();
20
21 this.range = range;
22 this.contentRange = contentRange;
23 this.errorHandlerRange = errorHandlerRange;
24 }
25
26 get type() {
27 return "require.ensure";
28 }
29
30 serialize(context) {
31 const { write } = context;
32
33 write(this.range);
34 write(this.contentRange);
35 write(this.errorHandlerRange);
36
37 super.serialize(context);
38 }
39
40 deserialize(context) {
41 const { read } = context;
42
43 this.range = read();
44 this.contentRange = read();
45 this.errorHandlerRange = read();
46
47 super.deserialize(context);
48 }
49}
50
51makeSerializable(
52 RequireEnsureDependency,
53 "webpack/lib/dependencies/RequireEnsureDependency"
54);
55
56RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate extends (
57 NullDependency.Template
58) {
59 /**
60 * @param {Dependency} dependency the dependency for which the template should be applied
61 * @param {ReplaceSource} source the current replace source which can be modified
62 * @param {DependencyTemplateContext} templateContext the context object
63 * @returns {void}
64 */
65 apply(
66 dependency,
67 source,
68 { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
69 ) {
70 const dep = /** @type {RequireEnsureDependency} */ (dependency);
71 const depBlock = /** @type {AsyncDependenciesBlock} */ (
72 moduleGraph.getParentBlock(dep)
73 );
74 const promise = runtimeTemplate.blockPromise({
75 chunkGraph,
76 block: depBlock,
77 message: "require.ensure",
78 runtimeRequirements
79 });
80 const range = dep.range;
81 const contentRange = dep.contentRange;
82 const errorHandlerRange = dep.errorHandlerRange;
83 source.replace(range[0], contentRange[0] - 1, `${promise}.then((`);
84 if (errorHandlerRange) {
85 source.replace(
86 contentRange[1],
87 errorHandlerRange[0] - 1,
88 ").bind(null, __webpack_require__))['catch']("
89 );
90 source.replace(errorHandlerRange[1], range[1] - 1, ")");
91 } else {
92 source.replace(
93 contentRange[1],
94 range[1] - 1,
95 `).bind(null, __webpack_require__))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`
96 );
97 }
98 }
99};
100
101module.exports = RequireEnsureDependency;