Skip to content

Commit 7d87f34

Browse files
committed
Only create one RequestShortener per Compiler
move Compiler.context assignment into Compiler
1 parent 0d66130 commit 7d87f34

File tree

8 files changed

+36
-19
lines changed

8 files changed

+36
-19
lines changed

lib/Compilation.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class Compilation extends Tapable {
170170
this.compiler = compiler;
171171
this.resolverFactory = compiler.resolverFactory;
172172
this.inputFileSystem = compiler.inputFileSystem;
173+
this.requestShortener = compiler.requestShortener;
173174

174175
const options = this.options = compiler.options;
175176
this.outputOptions = options && options.output;
@@ -181,8 +182,8 @@ class Compilation extends Tapable {
181182
this.chunkTemplate = new ChunkTemplate(this.outputOptions);
182183
this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(this.outputOptions);
183184
this.moduleTemplates = {
184-
javascript: new ModuleTemplate(this.outputOptions),
185-
webassembly: new ModuleTemplate(this.outputOptions)
185+
javascript: new ModuleTemplate(this.outputOptions, this.requestShortener),
186+
webassembly: new ModuleTemplate(this.outputOptions, this.requestShortener)
186187
};
187188

188189
this.semaphore = new Semaphore(options.parallelism || 100);

lib/Compiler.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const NormalModuleFactory = require("./NormalModuleFactory");
1818
const ContextModuleFactory = require("./ContextModuleFactory");
1919
const ResolverFactory = require("./ResolverFactory");
2020

21+
const RequestShortener = require("./RequestShortener");
2122
const makePathsRelative = require("./util/identifier").makePathsRelative;
2223

2324
class Watching {
@@ -174,7 +175,7 @@ class Watching {
174175
}
175176

176177
class Compiler extends Tapable {
177-
constructor() {
178+
constructor(context) {
178179
super();
179180
this.hooks = {
180181
shouldEmit: new SyncBailHook(["compilation"]),
@@ -321,6 +322,10 @@ class Compiler extends Tapable {
321322
};
322323

323324
this.options = {};
325+
326+
this.context = context;
327+
328+
this.requestShortener = new RequestShortener(context);
324329
}
325330

326331
watch(watchOptions, handler) {
@@ -515,7 +520,7 @@ class Compiler extends Tapable {
515520
}
516521

517522
createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) {
518-
const childCompiler = new Compiler();
523+
const childCompiler = new Compiler(this.context);
519524
if(Array.isArray(plugins)) {
520525
plugins.forEach(plugin => childCompiler.apply(plugin));
521526
}

lib/FunctionModulePlugin.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
"use strict";
66

77
const FunctionModuleTemplatePlugin = require("./FunctionModuleTemplatePlugin");
8-
const RequestShortener = require("./RequestShortener");
98

109
class FunctionModulePlugin {
11-
constructor(options, requestShortener) {
10+
constructor(options) {
1211
this.options = options;
13-
this.requestShortener = requestShortener;
1412
}
1513

1614
apply(compiler) {
1715
compiler.plugin("compilation", (compilation) => {
18-
compilation.moduleTemplates.javascript.requestShortener = this.requestShortener || new RequestShortener(compiler.context);
1916
compilation.moduleTemplates.javascript.apply(new FunctionModuleTemplatePlugin());
2017
});
2118
}

lib/ModuleTemplate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
99
const SyncHook = require("tapable").SyncHook;
1010

1111
module.exports = class ModuleTemplate extends Template {
12-
constructor(outputOptions) {
12+
constructor(outputOptions, requestShortener) {
1313
super(outputOptions);
14+
this.requestShortener = requestShortener;
1415
this.hooks = {
1516
content: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),
1617
module: new SyncWaterfallHook(["source", "module", "options", "dependencyTemplates"]),

lib/SourceMapDevToolPlugin.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"use strict";
66

77
const path = require("path");
8-
const RequestShortener = require("./RequestShortener");
98
const ConcatSource = require("webpack-sources").ConcatSource;
109
const RawSource = require("webpack-sources").RawSource;
1110
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
@@ -74,7 +73,7 @@ class SourceMapDevToolPlugin {
7473
const moduleFilenameTemplate = this.moduleFilenameTemplate;
7574
const namespace = this.namespace;
7675
const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
77-
const requestShortener = new RequestShortener(compiler.context);
76+
const requestShortener = compiler.requestShortener;
7877
const options = this.options;
7978
options.test = options.test || /\.(js|css)($|\?)/i;
8079

lib/Stats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class Stats {
9696
};
9797

9898
const compilation = this.compilation;
99-
const context = optionsOrFallback(options.context, process.cwd());
100-
const requestShortener = new RequestShortener(context);
99+
const context = optionsOrFallback(options.context, compilation.compiler.context);
100+
const requestShortener = compilation.compiler.context === context ? compilation.requestShortener : new RequestShortener(context);
101101
const showPerformance = optionOrLocalFallback(options.performance, true);
102102
const showHash = optionOrLocalFallback(options.hash, true);
103103
const showEnv = optionOrLocalFallback(options.env, false);

lib/webpack.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const webpack = (options, callback) => {
2424
} else if(typeof options === "object") {
2525
options = new WebpackOptionsDefaulter().process(options);
2626

27-
compiler = new Compiler();
28-
compiler.context = options.context;
27+
compiler = new Compiler(options.context);
2928
compiler.options = options;
3029
new NodeEnvironmentPlugin().apply(compiler);
3130
if(options.plugins && Array.isArray(options.plugins)) {

test/Stats.unittest.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ describe("Stats", () => {
1111
it("hasErrors", () => {
1212
const mockStats = new Stats({
1313
errors: ["firstError"],
14-
hash: "1234"
14+
hash: "1234",
15+
compiler: {
16+
context: ""
17+
}
1518
});
1619
mockStats.hasErrors().should.be.ok();
1720
});
1821
it("hasWarnings", () => {
1922
const mockStats = new Stats({
2023
warnings: ["firstError"],
21-
hash: "1234"
24+
hash: "1234",
25+
compiler: {
26+
context: ""
27+
}
2228
});
2329
mockStats.hasWarnings().should.be.ok();
2430
});
@@ -27,14 +33,20 @@ describe("Stats", () => {
2733
it("hasErrors", () => {
2834
const mockStats = new Stats({
2935
errors: [],
30-
hash: "1234"
36+
hash: "1234",
37+
compiler: {
38+
context: ""
39+
}
3140
});
3241
mockStats.hasErrors().should.not.be.ok();
3342
});
3443
it("hasWarnings", () => {
3544
const mockStats = new Stats({
3645
warnings: [],
37-
hash: "1234"
46+
hash: "1234",
47+
compiler: {
48+
context: ""
49+
}
3850
});
3951
mockStats.hasWarnings().should.not.be.ok();
4052
});
@@ -51,6 +63,9 @@ describe("Stats", () => {
5163
hash: "1234",
5264
mainTemplate: {
5365
getPublicPath: () => "path"
66+
},
67+
compiler: {
68+
context: ""
5469
}
5570
});
5671
const obj = mockStats.toJson();

0 commit comments

Comments
 (0)