| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Ivan Kopeykin @vankop
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Dependency = require("../Dependency");
|
|---|
| 9 | const Template = require("../Template");
|
|---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 11 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 18 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 19 |
|
|---|
| 20 | class WebpackIsIncludedDependency extends ModuleDependency {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of WebpackIsIncludedDependency.
|
|---|
| 23 | * @param {string} request the request string
|
|---|
| 24 | * @param {Range} range location in source code
|
|---|
| 25 | */
|
|---|
| 26 | constructor(request, range) {
|
|---|
| 27 | super(request);
|
|---|
| 28 |
|
|---|
| 29 | this.weak = true;
|
|---|
| 30 | this.range = range;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Returns list of exports referenced by this dependency
|
|---|
| 35 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 36 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 37 | * @returns {ReferencedExports} referenced exports
|
|---|
| 38 | */
|
|---|
| 39 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 40 | // This doesn't use any export
|
|---|
| 41 | return Dependency.NO_EXPORTS_REFERENCED;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | get type() {
|
|---|
| 45 | return "__webpack_is_included__";
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | makeSerializable(
|
|---|
| 50 | WebpackIsIncludedDependency,
|
|---|
| 51 | "webpack/lib/dependencies/WebpackIsIncludedDependency"
|
|---|
| 52 | );
|
|---|
| 53 |
|
|---|
| 54 | WebpackIsIncludedDependency.Template = class WebpackIsIncludedDependencyTemplate extends (
|
|---|
| 55 | ModuleDependency.Template
|
|---|
| 56 | ) {
|
|---|
| 57 | /**
|
|---|
| 58 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 59 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 60 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 61 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 62 | * @returns {void}
|
|---|
| 63 | */
|
|---|
| 64 | apply(dependency, source, { runtimeTemplate, chunkGraph, moduleGraph }) {
|
|---|
| 65 | const dep = /** @type {WebpackIsIncludedDependency} */ (dependency);
|
|---|
| 66 | const connection = moduleGraph.getConnection(dep);
|
|---|
| 67 | const included = connection
|
|---|
| 68 | ? chunkGraph.getNumberOfModuleChunks(connection.module) > 0
|
|---|
| 69 | : false;
|
|---|
| 70 | const comment = runtimeTemplate.outputOptions.pathinfo
|
|---|
| 71 | ? Template.toComment(
|
|---|
| 72 | `__webpack_is_included__ ${runtimeTemplate.requestShortener.shorten(
|
|---|
| 73 | dep.request
|
|---|
| 74 | )}`
|
|---|
| 75 | )
|
|---|
| 76 | : "";
|
|---|
| 77 |
|
|---|
| 78 | source.replace(
|
|---|
| 79 | dep.range[0],
|
|---|
| 80 | dep.range[1] - 1,
|
|---|
| 81 | `${comment}${JSON.stringify(included)}`
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | module.exports = WebpackIsIncludedDependency;
|
|---|