Skip to content

Commit fabe631

Browse files
committed
Only generate portable records when needed
1 parent 0376a32 commit fabe631

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

lib/RecordIdsPlugin.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
const identifierUtils = require("./util/identifier");
88

9-
const portableIdCache = new WeakMap();
10-
119
class RecordIdsPlugin {
10+
constructor(options) {
11+
this.options = options || {};
12+
}
1213

1314
apply(compiler) {
15+
const portableIds = this.options.portableIds;
1416
compiler.plugin("compilation", compilation => {
1517
compilation.plugin("record-modules", (modules, records) => {
1618
if(!records.modules) records.modules = {};
1719
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
1820
if(!records.modules.usedIds) records.modules.usedIds = {};
1921
modules.forEach(module => {
20-
let identifier = portableIdCache.get(module);
21-
if(!identifier) portableIdCache.set(module, identifier = identifierUtils.makePathsRelative(compiler.context, module.identifier()));
22+
const identifier = portableIds ? identifierUtils.makePathsRelative(compiler.context, module.identifier(), compilation.cache) : module.identifier();
2223
records.modules.byIdentifier[identifier] = module.id;
2324
records.modules.usedIds[module.id] = module.id;
2425
});
@@ -29,8 +30,7 @@ class RecordIdsPlugin {
2930
const usedIds = {};
3031
modules.forEach(module => {
3132
if(module.id !== null) return;
32-
let identifier = portableIdCache.get(module);
33-
if(!identifier) portableIdCache.set(module, identifier = identifierUtils.makePathsRelative(compiler.context, module.identifier()));
33+
const identifier = portableIds ? identifierUtils.makePathsRelative(compiler.context, module.identifier(), compilation.cache) : module.identifier();
3434
const id = records.modules.byIdentifier[identifier];
3535
if(id === undefined) return;
3636
if(usedIds[id]) return;
@@ -53,7 +53,8 @@ class RecordIdsPlugin {
5353
block = block.parent;
5454
}
5555
if(!block.identifier) return null;
56-
ident.push(identifierUtils.makePathsRelative(compiler.context, block.identifier()));
56+
const identifier = portableIds ? identifierUtils.makePathsRelative(compiler.context, block.identifier(), compilation.cache) : block.identifier();
57+
ident.push(identifier);
5758
return ident.reverse().join(":");
5859
};
5960

lib/WebpackOptionsApply.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ class WebpackOptionsApply extends OptionsApply {
321321

322322
compiler.apply(new TemplatedPathPlugin());
323323

324-
compiler.apply(new RecordIdsPlugin());
324+
compiler.apply(new RecordIdsPlugin({
325+
portableIds: options.optimization.portableRecords
326+
}));
325327

326328
compiler.apply(new WarnCaseSensitiveModulesPlugin());
327329

lib/WebpackOptionsDefaulter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
137137
this.set("optimization.noEmitOnErrors", "make", options => options.mode === "production");
138138
this.set("optimization.namedModules", "make", options => options.mode === "development");
139139
this.set("optimization.namedChunks", "make", options => options.mode === "development");
140+
this.set("optimization.portableRecords", "make", options => !!(options.recordsInputPath || options.recordsOutputPath || options.recordsPath));
140141
this.set("optimization.nodeEnv", "make", options => options.mode);
141142

142143
this.set("resolve", "call", value => Object.assign({}, value));

schemas/WebpackOptions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,10 @@
12711271
"description": "Use readable chunk identifiers for better debugging",
12721272
"type": "boolean"
12731273
},
1274+
"portableRecords": {
1275+
"description": "Generate records with relative paths to be able to move the context folder",
1276+
"type": "boolean"
1277+
},
12741278
"nodeEnv": {
12751279
"description": "Set process.env.NODE_ENV to a specific value",
12761280
"anyOf": [

0 commit comments

Comments
 (0)