Skip to content

Commit eb960cc

Browse files
committed
Fixed Profiling Output path for creating a folder, if it doesn't exists.
1 parent 3b344f2 commit eb960cc

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/debug/ProfilingPlugin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require("fs");
2+
const path = require("path");
23
const { Tracer } = require("chrome-trace-event");
34
const validateOptions = require("schema-utils");
45
const schema = require("../../schemas/plugins/debug/ProfilingPlugin.json");
@@ -86,13 +87,18 @@ class Profiler {
8687

8788
/**
8889
* @param {string} outputPath The ___location where to write the log.
90+
* @param {Object} compiler Current compiler Instance
8991
* @returns {Trace} The trace object
9092
*/
91-
const createTrace = outputPath => {
93+
const createTrace = (outputPath, compiler) => {
9294
const trace = new Tracer({
9395
noStream: true
9496
});
9597
const profiler = new Profiler(inspector);
98+
if (outputPath.match(/\/|\\/)) {
99+
const dir = path.dirname(outputPath);
100+
compiler.outputFileSystem.mkdirp(dir);
101+
}
96102
const fsStream = fs.createWriteStream(outputPath);
97103

98104
let counter = 0;
@@ -158,7 +164,7 @@ class ProfilingPlugin {
158164
}
159165

160166
apply(compiler) {
161-
const tracer = createTrace(this.outputPath);
167+
const tracer = createTrace(this.outputPath, compiler);
162168
tracer.profiler.startProfiling();
163169

164170
// Compiler Hooks

test/ProfilingPlugin.unittest.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use strict";
22

33
const ProfilingPlugin = require("../lib/debug/ProfilingPlugin");
4+
const path = require("path");
5+
const fs = require("fs");
6+
const webpack = require("../");
47

58
describe("Profiling Plugin", () => {
69
it("should persist the passed outpath", () => {
@@ -15,6 +18,26 @@ describe("Profiling Plugin", () => {
1518
expect(plugin.outputPath).toBe("events.json");
1619
});
1720

21+
it("should handle outpath with folder", done => {
22+
const finalPath = "test/fixtures/profilingPath/events.json";
23+
const compiler = webpack({
24+
context: "/",
25+
entry: "./fixtures/a.js",
26+
plugins: [
27+
new webpack.debug.ProfilingPlugin({
28+
outputPath: finalPath
29+
})
30+
]
31+
});
32+
const outputPath = path.join(__dirname, "/fixtures/profilingPath");
33+
compiler.run(err => {
34+
if (err) return done(err);
35+
if (!fs.existsSync(outputPath))
36+
return done(new Error("Folder should be created."));
37+
done();
38+
});
39+
});
40+
1841
it("should handle when unable to require the inspector", () => {
1942
const profiler = new ProfilingPlugin.Profiler();
2043
return profiler.startProfiling();

0 commit comments

Comments
 (0)