Skip to content

Commit 5563ee9

Browse files
committed
use makePathsRelative instead of contextify
unify contextify and makePathsRelative separate NormalModuleFactory ignored, ContextModule and ConcatenatedModule identifier with "|"
1 parent d544027 commit 5563ee9

12 files changed

+267
-238
lines changed

lib/ContextModule.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,35 @@ class ContextModule extends Module {
125125
_createIdentifier() {
126126
let identifier = this.context;
127127
if (this.options.resourceQuery) {
128-
identifier += ` ${this.options.resourceQuery}`;
128+
identifier += `|${this.options.resourceQuery}`;
129129
}
130130
if (this.options.mode) {
131-
identifier += ` ${this.options.mode}`;
131+
identifier += `|${this.options.mode}`;
132132
}
133133
if (!this.options.recursive) {
134-
identifier += " nonrecursive";
134+
identifier += "|nonrecursive";
135135
}
136136
if (this.options.addon) {
137-
identifier += ` ${this.options.addon}`;
137+
identifier += `|${this.options.addon}`;
138138
}
139139
if (this.options.regExp) {
140-
identifier += ` ${this.options.regExp}`;
140+
identifier += `|${this.options.regExp}`;
141141
}
142142
if (this.options.include) {
143-
identifier += ` include: ${this.options.include}`;
143+
identifier += `|include: ${this.options.include}`;
144144
}
145145
if (this.options.exclude) {
146-
identifier += ` exclude: ${this.options.exclude}`;
146+
identifier += `|exclude: ${this.options.exclude}`;
147147
}
148148
if (this.options.groupOptions) {
149-
identifier += ` groupOptions: ${JSON.stringify(
149+
identifier += `|groupOptions: ${JSON.stringify(
150150
this.options.groupOptions
151151
)}`;
152152
}
153153
if (this.options.namespaceObject === "strict") {
154-
identifier += " strict namespace object";
154+
identifier += "|strict namespace object";
155155
} else if (this.options.namespaceObject) {
156-
identifier += " namespace object";
156+
identifier += "|namespace object";
157157
}
158158

159159
return identifier;

lib/NormalModuleFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class NormalModuleFactory {
211211
null,
212212
new RawModule(
213213
"/* (ignored) */",
214-
`ignored ${request}`,
214+
`ignored|${request}`,
215215
`${request} (ignored)`
216216
)
217217
);

lib/ids/DeterministicChunkIdsPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class DeterministicChunkIdsPlugin {
4242
Array.from(chunks).filter(chunk => {
4343
return chunk.id === null;
4444
}),
45-
chunk => getFullChunkName(chunk, chunkGraph, context),
45+
chunk =>
46+
getFullChunkName(chunk, chunkGraph, context, compiler.root),
4647
compareNatural,
4748
this.options.maxLength || 3,
4849
getUsedChunkIds(compilation),

lib/ids/DeterministicModuleIdsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DeterministicModuleIdsPlugin {
4444
return false;
4545
return chunkGraph.getModuleId(module) === null;
4646
}),
47-
module => getFullModuleName(module, context),
47+
module => getFullModuleName(module, context, compiler.root),
4848
compareModulesByPreOrderIndexOrIdentifier(
4949
compilation.moduleGraph
5050
),

lib/ids/HashedModuleIdsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class HashedModuleIdsPlugin {
5353
compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
5454
);
5555
for (const module of modulesInNaturalOrder) {
56-
const ident = getFullModuleName(module, context);
56+
const ident = getFullModuleName(module, context, compiler.root);
5757
const hash = createHash(options.hashFunction);
5858
hash.update(ident || "");
5959
const hashId = hash.digest(options.hashDigest);

lib/ids/IdHelpers.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"use strict";
77

88
const createHash = require("../util/createHash");
9-
const { contextify } = require("../util/identifier");
9+
const { makePathsRelative } = require("../util/identifier");
1010

1111
/** @typedef {import("../Chunk")} Chunk */
1212
/** @typedef {import("../ChunkGraph")} ChunkGraph */
@@ -77,21 +77,32 @@ exports.getShortModuleName = getShortModuleName;
7777
* @param {string} shortName the short name
7878
* @param {Module} module the module
7979
* @param {string} context context directory
80+
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
8081
* @returns {string} long module name
8182
*/
82-
const getLongModuleName = (shortName, module, context) => {
83-
const localIdentifier = contextify(context, module.identifier());
84-
return `${shortName}?${getHash(localIdentifier, 4)}`;
83+
const getLongModuleName = (
84+
shortName,
85+
module,
86+
context,
87+
associatedObjectForCache
88+
) => {
89+
const fullName = getFullModuleName(module, context, associatedObjectForCache);
90+
return `${shortName}?${getHash(fullName, 4)}`;
8591
};
8692
exports.getLongModuleName = getLongModuleName;
8793

8894
/**
8995
* @param {Module} module the module
9096
* @param {string} context context directory
97+
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
9198
* @returns {string} full module name
9299
*/
93-
const getFullModuleName = (module, context) => {
94-
return contextify(context, module.identifier());
100+
const getFullModuleName = (module, context, associatedObjectForCache) => {
101+
return makePathsRelative(
102+
context,
103+
module.identifier(),
104+
associatedObjectForCache
105+
);
95106
};
96107
exports.getFullModuleName = getFullModuleName;
97108

@@ -120,15 +131,22 @@ exports.getShortChunkName = getShortChunkName;
120131
* @param {ChunkGraph} chunkGraph the chunk grph
121132
* @param {string} context context directory
122133
* @param {string} delimiter delimiter for names
134+
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
123135
* @returns {string} short chunk name
124136
*/
125-
const getLongChunkName = (chunk, chunkGraph, context, delimiter) => {
137+
const getLongChunkName = (
138+
chunk,
139+
chunkGraph,
140+
context,
141+
delimiter,
142+
associatedObjectForCache
143+
) => {
126144
const modules = chunkGraph.getChunkRootModules(chunk);
127145
const shortModuleNames = modules.map(m =>
128146
requestToId(getShortModuleName(m, context))
129147
);
130148
const longModuleNames = modules.map(m =>
131-
requestToId(getLongModuleName("", m, context))
149+
requestToId(getLongModuleName("", m, context, associatedObjectForCache))
132150
);
133151
chunk.idNameHints.sort();
134152
const chunkName = Array.from(chunk.idNameHints)
@@ -142,12 +160,20 @@ exports.getLongChunkName = getLongChunkName;
142160
* @param {Chunk} chunk the chunk
143161
* @param {ChunkGraph} chunkGraph the chunk grph
144162
* @param {string} context context directory
163+
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
145164
* @returns {string} full chunk name
146165
*/
147-
const getFullChunkName = (chunk, chunkGraph, context) => {
166+
const getFullChunkName = (
167+
chunk,
168+
chunkGraph,
169+
context,
170+
associatedObjectForCache
171+
) => {
148172
if (chunk.name) return chunk.name;
149173
const modules = chunkGraph.getChunkRootModules(chunk);
150-
const fullModuleNames = modules.map(m => contextify(context, m.identifier()));
174+
const fullModuleNames = modules.map(m =>
175+
makePathsRelative(context, m.identifier(), associatedObjectForCache)
176+
);
151177
return fullModuleNames.join();
152178
};
153179
exports.getFullChunkName = getFullChunkName;

lib/ids/NamedChunkIdsPlugin.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ class NamedChunkIdsPlugin {
4444
return chunk.id === null;
4545
}),
4646
chunk => getShortChunkName(chunk, chunkGraph, context, delimiter),
47-
chunk => getLongChunkName(chunk, chunkGraph, context, delimiter),
47+
chunk =>
48+
getLongChunkName(
49+
chunk,
50+
chunkGraph,
51+
context,
52+
delimiter,
53+
compiler.root
54+
),
4855
compareChunksNatural(chunkGraph),
4956
getUsedChunkIds(compilation),
5057
(chunk, name) => {

lib/ids/NamedModuleIdsPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class NamedModuleIdsPlugin {
4040
return chunkGraph.getModuleId(module) === null;
4141
}),
4242
m => getShortModuleName(m, context),
43-
(m, shortName) => getLongModuleName(shortName, m, context),
43+
(m, shortName) =>
44+
getLongModuleName(shortName, m, context, compiler.root),
4445
compareModulesByIdentifier,
4546
getUsedModuleIds(compilation),
4647
(m, name) => chunkGraph.setModuleId(m, name)

lib/optimize/ConcatenatedModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ class ConcatenatedModule extends Module {
615615
}
616616
const hash = createHash("md4");
617617
hash.update(orderedConcatenationListIdentifiers);
618-
return this.rootModule.identifier() + " " + hash.digest("hex");
618+
return this.rootModule.identifier() + "|" + hash.digest("hex");
619619
}
620620

621621
/**

lib/util/identifier.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,39 @@ const path = require("path");
1212
*/
1313

1414
/**
15-
*
16-
* @param {string} maybeAbsolutePath path to check
17-
* @returns {boolean} returns true if path is "Absolute Path"-like
15+
* @param {string} context context for relative path
16+
* @param {string} maybeAbsolutePath path to make relative
17+
* @returns {string} relative path in request style
1818
*/
19-
const looksLikeAbsolutePath = maybeAbsolutePath => {
19+
const absoluteToRequest = (context, maybeAbsolutePath) => {
2020
if (/^\/.*\/$/.test(maybeAbsolutePath)) {
2121
// this 'path' is actually a regexp generated by dynamic requires.
2222
// Don't treat it as an absolute path.
23-
return false;
23+
return maybeAbsolutePath;
24+
}
25+
if (/^[a-zA-Z]:\\/.test(maybeAbsolutePath)) {
26+
const splitPath = maybeAbsolutePath.split("?", 2);
27+
splitPath[0] = path.win32.relative(context, splitPath[0]);
28+
if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
29+
splitPath[0] = splitPath[0].replace(/\\/g, "/");
30+
if (!/^(\.\.\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
31+
splitPath[0] = "./" + splitPath[0];
32+
}
33+
}
34+
return splitPath.join("?");
35+
} else if (/^\//.test(maybeAbsolutePath)) {
36+
const splitPath = maybeAbsolutePath.split("?", 2);
37+
splitPath[0] = path.posix.relative(context, splitPath[0]);
38+
if (!/^(\.\.\/|\/)/.test(splitPath[0])) {
39+
splitPath[0] = "./" + splitPath[0];
40+
}
41+
return splitPath.join("?");
42+
} else {
43+
// not an absolute path
44+
return maybeAbsolutePath;
2445
}
25-
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
2646
};
2747

28-
/**
29-
*
30-
* @param {string} p path to normalize
31-
* @returns {string} normalized version of path
32-
*/
33-
const normalizePathSeparator = p => p.replace(/\\/g, "/");
34-
3548
/**
3649
*
3750
* @param {string} context context for relative path
@@ -41,12 +54,7 @@ const normalizePathSeparator = p => p.replace(/\\/g, "/");
4154
const _makePathsRelative = (context, identifier) => {
4255
return identifier
4356
.split(/([|! ])/)
44-
.map(
45-
str =>
46-
looksLikeAbsolutePath(str)
47-
? normalizePathSeparator(path.relative(context, str))
48-
: str
49-
)
57+
.map(str => absoluteToRequest(context, str))
5058
.join("");
5159
};
5260

@@ -90,24 +98,10 @@ exports.makePathsRelative = (context, identifier, associatedObjectForCache) => {
9098
* @param {string} request any request string may containing absolute paths, query string, etc.
9199
* @returns {string} a new request string avoiding absolute paths when possible
92100
*/
93-
exports.contextify = (context, request) => {
101+
const contextify = (context, request) => {
94102
return request
95103
.split("!")
96-
.map(r => {
97-
const splitPath = r.split("?", 2);
98-
if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
99-
splitPath[0] = path.win32.relative(context, splitPath[0]);
100-
if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
101-
splitPath[0] = splitPath[0].replace(/\\/g, "/");
102-
}
103-
}
104-
if (/^\//.test(splitPath[0])) {
105-
splitPath[0] = path.posix.relative(context, splitPath[0]);
106-
}
107-
if (!/^(\.\.\/|\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
108-
splitPath[0] = "./" + splitPath[0];
109-
}
110-
return splitPath.join("?");
111-
})
104+
.map(r => absoluteToRequest(context, r))
112105
.join("!");
113106
};
107+
exports.contextify = contextify;

0 commit comments

Comments
 (0)