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