Skip to content

Commit 385769b

Browse files
committed
use split trick to make code simpler, integrate makeRelative as static method into recordsidplugin
1 parent ce5e3e1 commit 385769b

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

‎lib/RecordIdsPlugin.js‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66

77
const path = require("path");
88

9-
const looksLikeAbsolutePath = (maybeAbsolutePath) => /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
9+
class RecordIdsPlugin {
1010

11-
function makeRelative(compiler, identifier) {
12-
const context = compiler.context;
13-
return identifier.split("|")
14-
.map(str => str.split("!")
15-
.map(str => str.split(" ")
16-
.map(str => looksLikeAbsolutePath(str) ? path.relative(context, str) : str)
17-
.join(" "))
18-
.join("!"))
19-
.join("|");
20-
}
11+
static looksLikeAbsolutePath(maybeAbsolutePath) {
12+
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
13+
}
2114

22-
class RecordIdsPlugin {
15+
static makeRelative(context, identifier) {
16+
return identifier
17+
.split(/([|! ])/)
18+
.map(str => RecordIdsPlugin.looksLikeAbsolutePath(str) ? path.relative(context, str) : str)
19+
.join("");
20+
}
2321

2422
apply(compiler) {
2523
compiler.plugin("compilation", compilation => {
@@ -28,7 +26,7 @@ class RecordIdsPlugin {
2826
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
2927
if(!records.modules.usedIds) records.modules.usedIds = {};
3028
modules.forEach(function(module) {
31-
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
29+
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
3230
const identifier = module.portableId;
3331
records.modules.byIdentifier[identifier] = module.id;
3432
records.modules.usedIds[module.id] = module.id;
@@ -40,7 +38,7 @@ class RecordIdsPlugin {
4038
const usedIds = {};
4139
modules.forEach(function(module) {
4240
if(module.id !== null) return;
43-
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
41+
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
4442
const identifier = module.portableId;
4543
const id = records.modules.byIdentifier[identifier];
4644
if(id === undefined) return;
@@ -64,7 +62,7 @@ class RecordIdsPlugin {
6462
block = block.parent;
6563
}
6664
if(!block.identifier) return null;
67-
ident.unshift(makeRelative(compiler, block.identifier()));
65+
ident.unshift(RecordIdsPlugin.makeRelative(compiler.context, block.identifier()));
6866
return ident.join(":");
6967
}
7068
compilation.plugin("record-chunks", (chunks, records) => {

0 commit comments

Comments
 (0)