Skip to content

Commit

Permalink
Merge pull request webpack#4500 from timse/make-ignored-relative
Browse files Browse the repository at this point in the history
Make ignored relative
  • Loading branch information
sokra committed Apr 3, 2017
2 parents a573e99 + 6441a35 commit 584f4da
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
11 changes: 8 additions & 3 deletions lib/NormalModuleFactory.js
Expand Up @@ -144,11 +144,16 @@ function NormalModuleFactory(context, resolvers, options) {
return callback(e);
}

if(resource === false)
if(resource === false) {
// ignored
return callback(null,
new RawModule("/* (ignored) */",
new RawModule(
"/* (ignored) */",
"ignored " + context + " " + request,
request + " (ignored)")); // ignored
request + " (ignored)"
)
);
}

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

Expand Down
23 changes: 15 additions & 8 deletions lib/RecordIdsPlugin.js
Expand Up @@ -4,22 +4,29 @@
*/
"use strict";

function makeRelative(compiler, identifier) {
const context = compiler.context;
return identifier.split("|").map(str => str.split("!").map(str => path.relative(context, str)).join("!")).join("|");
}

const path = require("path");

class RecordIdsPlugin {

static looksLikeAbsolutePath(maybeAbsolutePath) {
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
}

static makeRelative(context, identifier) {
return identifier
.split(/([|! ])/)
.map(str => RecordIdsPlugin.looksLikeAbsolutePath(str) ? path.relative(context, str) : str)
.join("");
}

apply(compiler) {
compiler.plugin("compilation", compilation => {
compilation.plugin("record-modules", (modules, records) => {
if(!records.modules) records.modules = {};
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
if(!records.modules.usedIds) records.modules.usedIds = {};
modules.forEach(function(module) {
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
const identifier = module.portableId;
records.modules.byIdentifier[identifier] = module.id;
records.modules.usedIds[module.id] = module.id;
Expand All @@ -31,7 +38,7 @@ class RecordIdsPlugin {
const usedIds = {};
modules.forEach(function(module) {
if(module.id !== null) return;
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
if(!module.portableId) module.portableId = RecordIdsPlugin.makeRelative(compiler.context, module.identifier());
const identifier = module.portableId;
const id = records.modules.byIdentifier[identifier];
if(id === undefined) return;
Expand All @@ -55,7 +62,7 @@ class RecordIdsPlugin {
block = block.parent;
}
if(!block.identifier) return null;
ident.unshift(makeRelative(compiler, block.identifier()));
ident.unshift(RecordIdsPlugin.makeRelative(compiler.context, block.identifier()));
return ident.join(":");
}
compilation.plugin("record-chunks", (chunks, records) => {
Expand Down
10 changes: 2 additions & 8 deletions test/RecordIdsPlugin.test.js
@@ -1,3 +1,4 @@
/* globals describe, before, it */
"use strict";

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

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

function makeRelative(compiler, identifier) {
const context = compiler.context;
return identifier.split("|").map((str) =>
str.split("!")
.map((str) => path.relative(context, str)).join("!")).join("|");
}

describe("RecordIdsPlugin", () => {

let compiler;
Expand All @@ -40,7 +34,7 @@ describe("RecordIdsPlugin", () => {
for(let i = 0; i < compilation.modules.length; i++) {
try {
should.exist(compilation.modules[i].portableId);
compilation.modules[i].portableId.should.equal(makeRelative(compiler, compilation.modules[i].identifier()));
compilation.modules[i].portableId.should.equal(RecordIdsPlugin.makeRelative(compiler.context, compilation.modules[i].identifier()));
} catch(e) {
done(e);
pass = false;
Expand Down
@@ -0,0 +1,3 @@
{
"browser": { "foo": false }
}
34 changes: 34 additions & 0 deletions test/configCases/records/issue-2991/test.js
@@ -0,0 +1,34 @@
try {
require("pkgs/somepackage/foo");
} catch(e){}

it("should write relative paths to records", function() {
var fs = require("fs");
var path = require("path");
var content = fs.readFileSync(path.join(__dirname, "records.json"), "utf-8");
content.should.eql(`{
"modules": {
"byIdentifier": {
"external \\"fs\\"": 0,
"external \\"path\\"": 1,
"ignored pkgs/somepackage/foo": 2,
"test.js": 3
},
"usedIds": {
"0": 0,
"1": 1,
"2": 2,
"3": 3
}
},
"chunks": {
"byName": {
"main": 0
},
"byBlocks": {},
"usedIds": {
"0": 0
}
}
}`);
});
16 changes: 16 additions & 0 deletions test/configCases/records/issue-2991/webpack.config.js
@@ -0,0 +1,16 @@
var path = require("path");

module.exports = {
entry: "./test",
recordsPath: path.resolve(__dirname, "../../../js/config/records/issue-2991/records.json"),
target: "node",
node: {
__dirname: false
},
resolve: {
aliasFields: [ "browser" ],
alias: {
pkgs: path.resolve(__dirname, "pkgs")
}
}
};

0 comments on commit 584f4da

Please sign in to comment.