Skip to content

Commit 584f4da

Browse files
authored
Merge pull request webpack#4500 from timse/make-ignored-relative
Make ignored relative
2 parents a573e99 + 6441a35 commit 584f4da

6 files changed

Lines changed: 78 additions & 19 deletions

File tree

lib/NormalModuleFactory.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,16 @@ function NormalModuleFactory(context, resolvers, options) {
144144
return callback(e);
145145
}
146146

147-
if(resource === false)
147+
if(resource === false) {
148+
// ignored
148149
return callback(null,
149-
new RawModule("/* (ignored) */",
150+
new RawModule(
151+
"/* (ignored) */",
150152
"ignored " + context + " " + request,
151-
request + " (ignored)")); // ignored
153+
request + " (ignored)"
154+
)
155+
);
156+
}
152157

153158
var userRequest = loaders.map(loaderToIdent).concat([resource]).join("!");
154159

lib/RecordIdsPlugin.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44
*/
55
"use strict";
66

7-
function makeRelative(compiler, identifier) {
8-
const context = compiler.context;
9-
return identifier.split("|").map(str => str.split("!").map(str => path.relative(context, str)).join("!")).join("|");
10-
}
11-
127
const path = require("path");
138

149
class RecordIdsPlugin {
10+
11+
static looksLikeAbsolutePath(maybeAbsolutePath) {
12+
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
13+
}
14+
15+
static makeRelative(context, identifier) {
16+
return identifier
17+
.split(/([|! ])/)
18+
.map(str => RecordIdsPlugin.looksLikeAbsolutePath(str) ? path.relative(context, str) : str)
19+
.join("");
20+
}
21+
1522
apply(compiler) {
1623
compiler.plugin("compilation", compilation => {
1724
compilation.plugin("record-modules", (modules, records) => {
1825
if(!records.modules) records.modules = {};
1926
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
2027
if(!records.modules.usedIds) records.modules.usedIds = {};
2128
modules.forEach(function(module) {
22-
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
29+
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
2330
const identifier = module.portableId;
2431
records.modules.byIdentifier[identifier] = module.id;
2532
records.modules.usedIds[module.id] = module.id;
@@ -31,7 +38,7 @@ class RecordIdsPlugin {
3138
const usedIds = {};
3239
modules.forEach(function(module) {
3340
if(module.id !== null) return;
34-
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
41+
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
3542
const identifier = module.portableId;
3643
const id = records.modules.byIdentifier[identifier];
3744
if(id === undefined) return;
@@ -55,7 +62,7 @@ class RecordIdsPlugin {
5562
block = block.parent;
5663
}
5764
if(!block.identifier) return null;
58-
ident.unshift(makeRelative(compiler, block.identifier()));
65+
ident.unshift(RecordIdsPlugin.makeRelative(compiler.context, block.identifier()));
5966
return ident.join(":");
6067
}
6168
compilation.plugin("record-chunks", (chunks, records) => {

test/RecordIdsPlugin.test.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* globals describe, before, it */
12
"use strict";
23

34
const should = require("should");
@@ -7,13 +8,6 @@ const webpack = require("../lib/webpack");
78

89
const RecordIdsPlugin = require("../lib/RecordIdsPlugin");
910

10-
function makeRelative(compiler, identifier) {
11-
const context = compiler.context;
12-
return identifier.split("|").map((str) =>
13-
str.split("!")
14-
.map((str) => path.relative(context, str)).join("!")).join("|");
15-
}
16-
1711
describe("RecordIdsPlugin", () => {
1812

1913
let compiler;
@@ -40,7 +34,7 @@ describe("RecordIdsPlugin", () => {
4034
for(let i = 0; i < compilation.modules.length; i++) {
4135
try {
4236
should.exist(compilation.modules[i].portableId);
43-
compilation.modules[i].portableId.should.equal(makeRelative(compiler, compilation.modules[i].identifier()));
37+
compilation.modules[i].portableId.should.equal(RecordIdsPlugin.makeRelative(compiler.context, compilation.modules[i].identifier()));
4438
} catch(e) {
4539
done(e);
4640
pass = false;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"browser": { "foo": false }
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
try {
2+
require("pkgs/somepackage/foo");
3+
} catch(e){}
4+
5+
it("should write relative paths to records", function() {
6+
var fs = require("fs");
7+
var path = require("path");
8+
var content = fs.readFileSync(path.join(__dirname, "records.json"), "utf-8");
9+
content.should.eql(`{
10+
"modules": {
11+
"byIdentifier": {
12+
"external \\"fs\\"": 0,
13+
"external \\"path\\"": 1,
14+
"ignored pkgs/somepackage/foo": 2,
15+
"test.js": 3
16+
},
17+
"usedIds": {
18+
"0": 0,
19+
"1": 1,
20+
"2": 2,
21+
"3": 3
22+
}
23+
},
24+
"chunks": {
25+
"byName": {
26+
"main": 0
27+
},
28+
"byBlocks": {},
29+
"usedIds": {
30+
"0": 0
31+
}
32+
}
33+
}`);
34+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require("path");
2+
3+
module.exports = {
4+
entry: "./test",
5+
recordsPath: path.resolve(__dirname, "../../../js/config/records/issue-2991/records.json"),
6+
target: "node",
7+
node: {
8+
__dirname: false
9+
},
10+
resolve: {
11+
aliasFields: [ "browser" ],
12+
alias: {
13+
pkgs: path.resolve(__dirname, "pkgs")
14+
}
15+
}
16+
};

0 commit comments

Comments
 (0)