UNPKG

webpack

Version:

Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.

121 lines (107 loc) • 3.82 kB
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ "use strict"; const RuntimeGlobals = require("../../runtime/RuntimeGlobals"); const makeSerializable = require("../../util/makeSerializable"); const NullDependency = require("../core/NullDependency"); /** @import { ReplaceSource } from "webpack-sources" */ /** @import AsyncDependenciesBlock from "../../graph/AsyncDependenciesBlock" */ /** @import Dependency from "../../graph/Dependency" */ /** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */ /** @import { Range } from "../../javascript/JavascriptParser" */ /** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, Range | false]>} ObjectDeserializerContext */ /** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, Range | false]>} ObjectSerializerContext */ class RequireEnsureDependency extends NullDependency { /** * Creates an instance of RequireEnsureDependency. * @param {Range} range range * @param {Range} contentRange content range * @param {Range | false} errorHandlerRange error handler range */ constructor(range, contentRange, errorHandlerRange) { super(); this.range = range; this.contentRange = contentRange; this.errorHandlerRange = errorHandlerRange; } get type() { return "require.ensure"; } /** * Serializes this instance into the provided serializer context. * @param {ObjectSerializerContext} context context */ serialize(context) { context .write(this.range) .write(this.contentRange) .write(this.errorHandlerRange); super.serialize(context); } /** * Restores this instance from the provided deserializer context. * @param {ObjectDeserializerContext} context context */ deserialize(context) { this.range = context.read(); const c1 = context.rest; this.contentRange = c1.read(); const c2 = c1.rest; this.errorHandlerRange = c2.read(); super.deserialize(c2.rest); } } makeSerializable(RequireEnsureDependency, [ "webpack/lib/dependencies/commonjs/RequireEnsureDependency", // TODO in the next major release: remove, they restore pre-move cache packs "webpack/lib/dependencies/RequireEnsureDependency" ]); RequireEnsureDependency.Template = class RequireEnsureDependencyTemplate extends ( NullDependency.Template ) { /** * Applies the plugin by registering its hooks on the compiler. * @param {Dependency} dependency the dependency for which the template should be applied * @param {ReplaceSource} source the current replace source which can be modified * @param {DependencyTemplateContext} templateContext the context object * @returns {void} */ apply( dependency, source, { module, runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements } ) { const dep = /** @type {RequireEnsureDependency} */ (dependency); const depBlock = /** @type {AsyncDependenciesBlock} */ ( moduleGraph.getParentBlock(dep) ); const promise = runtimeTemplate.blockPromise({ chunkGraph, block: depBlock, message: "require.ensure", runtimeRequirements, originModule: module }); const range = dep.range; const contentRange = dep.contentRange; const errorHandlerRange = dep.errorHandlerRange; source.replace(range[0], contentRange[0] - 1, `${promise}.then((`); if (errorHandlerRange) { source.replace( contentRange[1], errorHandlerRange[0] - 1, `).bind(null, ${RuntimeGlobals.require}))['catch'](` ); source.replace(errorHandlerRange[1], range[1] - 1, ")"); } else { source.replace( contentRange[1], range[1] - 1, `).bind(null, ${RuntimeGlobals.require}))['catch'](${RuntimeGlobals.uncaughtErrorHandler})` ); } } }; module.exports = RequireEnsureDependency;