Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 5ce3408
Author: Tobias Koppers <tobias.koppers@googlemail.com>
Date:   Thu Oct 31 15:55:59 2019 +0100

    WIP

commit f9e263a
Author: Tobias Koppers <tobias.koppers@googlemail.com>
Date:   Thu Oct 31 12:43:43 2019 +0100

    add redirecting support to create correct exports info for exportsType=named

commit b411e8d
Author: Tobias Koppers <tobias.koppers@googlemail.com>
Date:   Wed Oct 30 23:24:13 2019 +0100

    add support for tree-shaking JSON modules
  • Loading branch information
sbnewsthailand committed Jun 1, 2023
1 parent c80b069 commit 8841422
Show file tree
Hide file tree
Showing 27 changed files with 6,272 additions and 6,868 deletions.
4,225 changes: 697 additions & 3,528 deletions lib/Compilation.js

Large diffs are not rendered by default.

223 changes: 30 additions & 193 deletions lib/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,58 @@

"use strict";

const memoize = require("./util/memoize");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./WebpackError")} WebpackError */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("./util/Hash")} Hash */
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */

/**
* @typedef {Object} UpdateHashContext
* @property {ChunkGraph} chunkGraph
* @property {RuntimeSpec} runtime
* @property {RuntimeTemplate=} runtimeTemplate
*/

/**
* @typedef {Object} SourcePosition
* @property {number} line
* @property {number=} column
/** @typedef {Object} SourcePosition
* @property {number} line
* @property {number=} column
*/

/**
* @typedef {Object} RealDependencyLocation
* @property {SourcePosition} start
* @property {SourcePosition=} end
* @property {number=} index
/** @typedef {Object} RealDependencyLocation
* @property {SourcePosition} start
* @property {SourcePosition=} end
* @property {number=} index
*/

/**
* @typedef {Object} SyntheticDependencyLocation
* @property {string} name
* @property {number=} index
/** @typedef {Object} SynteticDependencyLocation
* @property {string} name
* @property {number=} index
*/

/** @typedef {SyntheticDependencyLocation|RealDependencyLocation} DependencyLocation */
/** @typedef {SynteticDependencyLocation|RealDependencyLocation} DependencyLocation */

/**
* @typedef {Object} ExportSpec
* @property {string} name the name of the export
* @property {boolean=} canMangle can the export be renamed (defaults to true)
* @property {boolean=} terminalBinding is the export a terminal binding that should be checked for export star conflicts
* @property {(string | ExportSpec)[]=} exports nested exports
* @property {ModuleGraphConnection=} from when reexported: from which module
* @property {Module=} from when reexported: from which module
* @property {string[] | null=} export when reexported: from which export
* @property {number=} priority when reexported: with which priority
* @property {boolean=} hidden export is not visible, because another export blends over it
*/

/**
* @typedef {Object} ExportsSpec
* @property {(string | ExportSpec)[] | true | null} exports exported names, true for unknown exports or null for no exports
* @property {Set<string>=} excludeExports when exports = true, list of unaffected exports
* @property {Set<string>=} hideExports list of maybe prior exposed, but now hidden exports
* @property {ModuleGraphConnection=} from when reexported: from which module
* @property {number=} priority when reexported: with which priority
* @property {boolean=} canMangle can the export be renamed (defaults to true)
* @property {boolean=} terminalBinding are the exports terminal bindings that should be checked for export star conflicts
* @property {Module[]=} dependencies module on which the result depends on
*/

/**
* @typedef {Object} ReferencedExport
* @property {string[]} name name of the referenced export
* @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
*/

const TRANSITIVE = Symbol("transitive");

const getIgnoredModule = memoize(() => {
const RawModule = require("./RawModule");
return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
});

class Dependency {
constructor() {
/** @type {Module} */
this._parentModule = undefined;
/** @type {DependenciesBlock} */
this._parentDependenciesBlock = undefined;
/** @type {number} */
this._parentDependenciesBlockIndex = -1;
// TODO check if this can be moved into ModuleDependency
/** @type {boolean} */
this.weak = false;
// TODO check if this can be moved into ModuleDependency
/** @type {boolean} */
this.optional = false;
this._locSL = 0;
this._locSC = 0;
this._locEL = 0;
this._locEC = 0;
this._locI = undefined;
this._locN = undefined;
this._loc = undefined;
/** @type {DependencyLocation} */
this.loc = undefined;
}

/**
Expand All @@ -117,80 +66,6 @@ class Dependency {
return "unknown";
}

/**
* @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
*/
get category() {
return "unknown";
}

/**
* @returns {DependencyLocation} location
*/
get loc() {
if (this._loc !== undefined) return this._loc;
/** @type {SyntheticDependencyLocation & RealDependencyLocation} */
const loc = {};
if (this._locSL > 0) {
loc.start = { line: this._locSL, column: this._locSC };
}
if (this._locEL > 0) {
loc.end = { line: this._locEL, column: this._locEC };
}
if (this._locN !== undefined) {
loc.name = this._locN;
}
if (this._locI !== undefined) {
loc.index = this._locI;
}
return (this._loc = loc);
}

set loc(loc) {
if ("start" in loc && typeof loc.start === "object") {
this._locSL = loc.start.line || 0;
this._locSC = loc.start.column || 0;
} else {
this._locSL = 0;
this._locSC = 0;
}
if ("end" in loc && typeof loc.end === "object") {
this._locEL = loc.end.line || 0;
this._locEC = loc.end.column || 0;
} else {
this._locEL = 0;
this._locEC = 0;
}
if ("index" in loc) {
this._locI = loc.index;
} else {
this._locI = undefined;
}
if ("name" in loc) {
this._locN = loc.name;
} else {
this._locN = undefined;
}
this._loc = loc;
}

setLoc(startLine, startColumn, endLine, endColumn) {
this._locSL = startLine;
this._locSC = startColumn;
this._locEL = endLine;
this._locEC = endColumn;
this._locI = undefined;
this._locN = undefined;
this._loc = undefined;
}

/**
* @returns {string | undefined} a request context
*/
getContext() {
return undefined;
}

/**
* @returns {string | null} an identifier to merge equal requests
*/
Expand All @@ -199,15 +74,8 @@ class Dependency {
}

/**
* @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
*/
couldAffectReferencingModule() {
return TRANSITIVE;
}

/**
* Returns the referenced module and export
* @deprecated
* Returns the referenced module and export
* @param {ModuleGraph} moduleGraph module graph
* @returns {never} throws error
*/
Expand All @@ -220,16 +88,15 @@ class Dependency {
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {(string[] | ReferencedExport)[]} referenced exports
* @returns {string[][]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return Dependency.EXPORTS_OBJECT_REFERENCED;
getReferencedExports(moduleGraph) {
return Dependency.NS_OBJECT_REFERENCED;
}

/**
* @param {ModuleGraph} moduleGraph module graph
* @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
* @returns {function(): boolean} function to determine if the connection is active
*/
getCondition(moduleGraph) {
return null;
Expand Down Expand Up @@ -265,10 +132,15 @@ class Dependency {
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {UpdateHashContext} context context
* @param {ChunkGraph} chunkGraph chunk graph
* @returns {void}
*/
updateHash(hash, context) {}
updateHash(hash, chunkGraph) {
const module = chunkGraph.moduleGraph.getModule(this);
if (module) {
hash.update(chunkGraph.getModuleId(module) + "");
}
}

/**
* implement this method to allow the occurrence order plugin to count correctly
Expand All @@ -278,55 +150,22 @@ class Dependency {
return 1;
}

/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {ConnectionState} how this dependency connects the module to referencing modules
*/
getModuleEvaluationSideEffectsState(moduleGraph) {
return true;
}

/**
* @param {string} context context directory
* @returns {Module} a module
*/
createIgnoredModule(context) {
return getIgnoredModule();
}

/**
* @param {ObjectSerializerContext} context context
*/
serialize({ write }) {
write(this.weak);
write(this.optional);
write(this._locSL);
write(this._locSC);
write(this._locEL);
write(this._locEC);
write(this._locI);
write(this._locN);
write(this.loc);
}

/**
* @param {ObjectDeserializerContext} context context
*/
deserialize({ read }) {
this.weak = read();
this.optional = read();
this._locSL = read();
this._locSC = read();
this._locEL = read();
this._locEC = read();
this._locI = read();
this._locN = read();
this.loc = read();
}
}

/** @type {string[][]} */
Dependency.NO_EXPORTS_REFERENCED = [];
/** @type {string[][]} */
Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
Dependency.NS_OBJECT_REFERENCED = [[]];
Dependency.DEFAULT_EXPORT_REFERENCED = [["default"]];

Object.defineProperty(Dependency.prototype, "module", {
/**
Expand Down Expand Up @@ -358,6 +197,4 @@ Object.defineProperty(Dependency.prototype, "disconnect", {
}
});

Dependency.TRANSITIVE = TRANSITIVE;

module.exports = Dependency;

0 comments on commit 8841422

Please sign in to comment.