Skip to content

Commit ce63b53

Browse files
committed
remove loader origin name and purify
1 parent 36303a0 commit ce63b53

3 files changed

Lines changed: 54 additions & 94 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,29 @@ class NormalModule extends Module {
136136
}
137137

138138
createLoaderContext(resolver, options, compilation, fs) {
139+
const requestShorten = compilation.runtimeTemplate.requestShortener.shorten.bind(
140+
compilation.runtimeTemplate.requestShortener
141+
);
139142
const loaderContext = {
140143
version: 2,
141144
emitWarning: warning => {
142145
if (!(warning instanceof Error))
143146
warning = new NonErrorEmittedError(warning);
144-
this.warnings.push(new ModuleWarning(this, warning));
147+
const currentLoader = this.getCurrentLoader(loaderContext);
148+
this.warnings.push(
149+
new ModuleWarning(this, warning, {
150+
from: requestShorten(currentLoader.loader)
151+
})
152+
);
145153
},
146154
emitError: error => {
147155
if (!(error instanceof Error)) error = new NonErrorEmittedError(error);
148-
this.errors.push(new ModuleError(this, error));
156+
const currentLoader = this.getCurrentLoader(loaderContext);
157+
this.errors.push(
158+
new ModuleError(this, error, {
159+
from: requestShorten(currentLoader.loader)
160+
})
161+
);
149162
},
150163
exec: (code, filename) => {
151164
const module = new NativeModule(filename, this);
@@ -180,6 +193,17 @@ class NormalModule extends Module {
180193
return loaderContext;
181194
}
182195

196+
getCurrentLoader(loaderContext) {
197+
if (
198+
this.loaders &&
199+
this.loaders.length &&
200+
this.loaders[loaderContext.loaderIndex]
201+
) {
202+
return this.loaders[loaderContext.loaderIndex];
203+
}
204+
return null;
205+
}
206+
183207
createSource(source, resourceBuffer, sourceMap) {
184208
// if there is no identifier return raw source
185209
if (!this.identifier) {
@@ -233,7 +257,12 @@ class NormalModule extends Module {
233257
}
234258

235259
if (err) {
236-
const error = new ModuleBuildError(this, err);
260+
const currentLoader = this.getCurrentLoader(loaderContext);
261+
const error = new ModuleBuildError(this, err, {
262+
from: compilation.runtimeTemplate.requestShortener.shorten(
263+
currentLoader.loader
264+
)
265+
});
237266
return callback(error);
238267
}
239268

‎test/Errors.test.js‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,13 @@ describe("Errors", () => {
225225
warnings.length.should.be.eql(1);
226226
warnings[0]
227227
.split("\n")[1]
228-
.should.match(/^Module Warning \(@emit-error-loader\): [^\s]+/);
228+
.should.match(
229+
/^Module Warning \(@.\/emit-error-loader.js\): [^\s]+/
230+
);
229231
errors.length.should.be.eql(1);
230232
errors[0]
231233
.split("\n")[1]
232-
.should.match(/^Module Error \(@emit-error-loader\): [^\s]+/);
234+
.should.match(/^Module Error \(@.\/emit-error-loader.js\): [^\s]+/);
233235
}
234236
),
235237
getErrorsPromise(
@@ -254,14 +256,18 @@ describe("Errors", () => {
254256
warnings.length.should.be.eql(1);
255257
warnings[0]
256258
.split("\n")[1]
257-
.should.match(/^Module Warning \(@emit-error-loader\): [^\s]+/);
259+
.should.match(
260+
/^Module Warning \(@.\/emit-error-loader.js\): [^\s]+/
261+
);
258262
errors.length.should.be.eql(2);
259263
errors[0]
260264
.split("\n")[1]
261-
.should.match(/^Module Error \(@emit-error-loader\): [^\s]+/);
265+
.should.match(/^Module Error \(@.\/emit-error-loader.js\): [^\s]+/);
262266
errors[1]
263267
.split("\n")[1]
264-
.should.match(/^Module build failed \(@json-loader\): [^\s]+/);
268+
.should.match(
269+
/^Module build failed \(@\(webpack\)\/node_modules\/json-loader\/index.js\): [^\s]+/
270+
);
265271
}
266272
),
267273
getErrorsPromise(
@@ -282,7 +288,7 @@ describe("Errors", () => {
282288
errors[0]
283289
.split("\n")[1]
284290
.should.match(
285-
/^Module build failed \(@async-error-loader\): [^\s]+/
291+
/^Module build failed \(@.\/async-error-loader.js\): [^\s]+/
286292
);
287293
}
288294
),
@@ -304,7 +310,7 @@ describe("Errors", () => {
304310
errors[0]
305311
.split("\n")[1]
306312
.should.match(
307-
/^Module build failed \(@throw-error-loader\): [^\s]+/
313+
/^Module build failed \(@.\/throw-error-loader.js\): [^\s]+/
308314
);
309315
}
310316
),
@@ -326,25 +332,29 @@ describe("Errors", () => {
326332
warnings[0]
327333
.split("\n")[1]
328334
.should.match(
329-
/^Module Warning \(@irregular-error-loader\): [^\s]+/
335+
/^Module Warning \(@.\/irregular-error-loader.js\): [^\s]+/
330336
);
331337
warnings[1]
332338
.split("\n")[1]
333339
.should.match(
334-
/^Module Warning \(@irregular-error-loader\): [^\s]+/
340+
/^Module Warning \(@.\/irregular-error-loader.js\): [^\s]+/
335341
);
336342

337343
errors.length.should.be.eql(3);
338344
errors[0]
339345
.split("\n")[1]
340-
.should.match(/^Module Error \(@irregular-error-loader\): [^\s]+/);
346+
.should.match(
347+
/^Module Error \(@.\/irregular-error-loader.js\): [^\s]+/
348+
);
341349
errors[1]
342350
.split("\n")[1]
343-
.should.match(/^Module Error \(@irregular-error-loader\): [^\s]+/);
351+
.should.match(
352+
/^Module Error \(@.\/irregular-error-loader.js\): [^\s]+/
353+
);
344354
errors[2]
345355
.split("\n")[1]
346356
.should.match(
347-
/^Module build failed \(@irregular-error-loader\): [^\s]+/
357+
/^Module build failed \(@.\/irregular-error-loader.js\): [^\s]+/
348358
);
349359
}
350360
)

‎test/NormalModule.unittest.js‎

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* globals describe, it, beforeEach, afterEach */
22
"use strict";
33
require("should");
4-
const path = require("path");
54
const sinon = require("sinon");
65
const NormalModule = require("../lib/NormalModule");
76
const NullDependency = require("../lib/dependencies/NullDependency");
@@ -429,82 +428,4 @@ describe("NormalModule", function() {
429428
});
430429
});
431430
});
432-
433-
describe("#purifyCurrentLoaderName", function() {
434-
beforeEach(function() {
435-
loaders = [
436-
{
437-
origin: "configured-loader",
438-
loader: "/some/loaders/configured-loader/index.js"
439-
},
440-
{
441-
origin: path.resolve(__dirname, "some/loaders/a-loader"),
442-
loader: path.resolve(__dirname, "some/loaders/a-loader")
443-
},
444-
{
445-
loader: path.resolve(__dirname, "some/loaders/another-loader.js")
446-
},
447-
{
448-
origin: "./some/loaders/another-loader-with-relative-path.js",
449-
loader: path.resolve(
450-
__dirname,
451-
"some/loaders/another-loader-with-relative-path.js"
452-
)
453-
}
454-
];
455-
normalModule = new NormalModule({
456-
type: "javascript/auto",
457-
request,
458-
userRequest,
459-
rawRequest,
460-
loaders,
461-
resource,
462-
parser
463-
});
464-
});
465-
it("should get origin configured loader name", function() {
466-
normalModule
467-
.purifyCurrentLoaderName({
468-
loaderIndex: 0
469-
})
470-
.should.eql("configured-loader");
471-
});
472-
it("should be relating when it's an absolute path", function() {
473-
normalModule
474-
.purifyCurrentLoaderName({
475-
loaderIndex: 1,
476-
rootContext: path.resolve(__dirname, "some/loaders")
477-
})
478-
.should.eql("a-loader");
479-
normalModule
480-
.purifyCurrentLoaderName({
481-
loaderIndex: 2,
482-
rootContext: path.resolve(__dirname, "some/loaders")
483-
})
484-
.should.eql("another-loader.js");
485-
});
486-
it("should be using directly when it's a relative path", function() {
487-
normalModule
488-
.purifyCurrentLoaderName({
489-
loaderIndex: 3
490-
})
491-
.should.eql("./some/loaders/another-loader-with-relative-path.js");
492-
});
493-
it("should return null if it had no loaders", function() {
494-
normalModule = new NormalModule({
495-
type: "javascript/auto",
496-
request,
497-
userRequest,
498-
rawRequest,
499-
loaders: [],
500-
resource,
501-
parser
502-
});
503-
(
504-
normalModule.purifyCurrentLoaderName({
505-
loaderIndex: 0
506-
}) === null
507-
).should.eql(true);
508-
});
509-
});
510431
});

0 commit comments

Comments
 (0)