Skip to content

Commit

Permalink
fix: Add render errors to compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
jahredhope committed Jan 3, 2019
1 parent 18c1c99 commit 1f7d36c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/RenderError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = class RenderError extends Error {
constructor(error) {
super(
`html-render-webpack-plugin: An error occured during render: \n${error}`
);

this.name = "RenderError";

Error.captureStackTrace(this, this.constructor);
}
};
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const renderHtml = require("./renderHtml");
const chalk = require("chalk");

const validateOptions = require("schema-utils");

const schema = require("./schemas/HtmlRenderWebpackPlugin.json");
const RenderError = require("./RenderError");
const renderHtml = require("./renderHtml");

const MultiStats = require("webpack/lib/MultiStats");

Expand Down Expand Up @@ -84,7 +85,7 @@ module.exports = class HtmlRenderPlugin {
});
} catch (error) {
this.logError("An error occured rendering HTML", error);
throw error;
currentCompilation.errors.push(new RenderError(error));
}
}
applyRenderPlugin(compiler) {
Expand Down
21 changes: 21 additions & 0 deletions tests/test-cases/errors/errors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const MemoryFS = require("memory-fs");
const webpack = require("webpack");

describe("Render HTML from in-config Plugin", () => {
it("should render a HTML file", async done => {
const compiler = webpack(require("./webpack.errors.config"));

const memoryFs = new MemoryFS();
compiler.outputFileSystem = memoryFs;

compiler.run((error, result) => {
// Render errors do not show up as build errors
expect(error).toBe(null);
// Errors show up in compilation
expect(result.toJson({ all: false, errors: true }).errors).toEqual(
expect.arrayContaining([expect.stringContaining("Example error")])
);
done();
});
});
});
4 changes: 4 additions & 0 deletions tests/test-cases/errors/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async () => {
const bar = "foo";
return bar;
};
3 changes: 3 additions & 0 deletions tests/test-cases/errors/src/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function exampleRender() {
throw new Error("Example error");
}
32 changes: 32 additions & 0 deletions tests/test-cases/errors/webpack.default.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require("path");

const srcPath = path.resolve(__dirname, "./src");
const paths = {
renderEntry: path.resolve(srcPath, "render.js"),
clientEntry: path.resolve(srcPath, "client.js")
};

module.exports = [
{
name: "client",
target: "web",
mode: "production",
entry: paths.clientEntry,
output: {
filename: "client-[name]-[contenthash].js"
}
},
{
dependencies: ["client"],
name: "render",
target: "node",
mode: "production",
entry: paths.renderEntry,
output: {
libraryExport: "default",
library: "static",
libraryTarget: "umd2",
filename: "render-[name]-[contenthash].js"
}
}
];
17 changes: 17 additions & 0 deletions tests/test-cases/errors/webpack.errors.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require("path");
const merge = require("webpack-merge");
const defaultConfig = require("./webpack.default.config");
const HtmlRenderPlugin = require("../../../src");

const renderDirectory = path.join(process.cwd(), "dist", "render");

const htmlRenderPlugin = new HtmlRenderPlugin({ renderDirectory });

module.exports = [
merge(defaultConfig[0], {
plugins: [htmlRenderPlugin]
}),
merge(defaultConfig[1], {
plugins: [htmlRenderPlugin.render()]
})
];

0 comments on commit 1f7d36c

Please sign in to comment.