123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- "use strict";
- const RuntimeGlobals = require("../RuntimeGlobals");
- const { equals } = require("../util/ArrayHelpers");
- const makeSerializable = require("../util/makeSerializable");
- const propertyAccess = require("../util/propertyAccess");
- const NullDependency = require("./NullDependency");
- class CommonJsSelfReferenceDependency extends NullDependency {
-
- constructor(range, base, names, call) {
- super();
- this.range = range;
- this.base = base;
- this.names = names;
- this.call = call;
- }
- get type() {
- return "cjs self exports reference";
- }
- get category() {
- return "self";
- }
-
- getResourceIdentifier() {
- return `self`;
- }
-
- getReferencedExports(moduleGraph, runtime) {
- return [this.call ? this.names.slice(0, -1) : this.names];
- }
-
- serialize(context) {
- const { write } = context;
- write(this.range);
- write(this.base);
- write(this.names);
- write(this.call);
- super.serialize(context);
- }
-
- deserialize(context) {
- const { read } = context;
- this.range = read();
- this.base = read();
- this.names = read();
- this.call = read();
- super.deserialize(context);
- }
- }
- makeSerializable(
- CommonJsSelfReferenceDependency,
- "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
- );
- CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
- NullDependency.Template
- ) {
-
- apply(
- dependency,
- source,
- { module, moduleGraph, runtime, runtimeRequirements }
- ) {
- const dep = (dependency);
- let used;
- if (dep.names.length === 0) {
- used = dep.names;
- } else {
- used = moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
- }
- if (!used) {
- throw new Error(
- "Self-reference dependency has unused export name: This should not happen"
- );
- }
- let base = undefined;
- switch (dep.base) {
- case "exports":
- runtimeRequirements.add(RuntimeGlobals.exports);
- base = module.exportsArgument;
- break;
- case "module.exports":
- runtimeRequirements.add(RuntimeGlobals.module);
- base = `${module.moduleArgument}.exports`;
- break;
- case "this":
- runtimeRequirements.add(RuntimeGlobals.thisAsExports);
- base = "this";
- break;
- default:
- throw new Error(`Unsupported base ${dep.base}`);
- }
- if (base === dep.base && equals(used, dep.names)) {
-
-
-
-
- return;
- }
- source.replace(
- dep.range[0],
- dep.range[1] - 1,
- `${base}${propertyAccess(used)}`
- );
- }
- };
- module.exports = CommonJsSelfReferenceDependency;
|