Skip to content

Commit

Permalink
added strictModuleExceptionHandling option
Browse files Browse the repository at this point in the history
fixes #1729
  • Loading branch information
sokra committed Nov 16, 2016
1 parent a58ce33 commit b87bfc0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/MainTemplate.js
Expand Up @@ -62,8 +62,23 @@ function MainTemplate(outputOptions) {
this.indent(this.applyPluginsWaterfall("module-obj", "", chunk, hash, "moduleId")),
"};",
"",
"// Execute the module function",
"modules[moduleId].call(module.exports, module, module.exports, " + this.renderRequireFunctionForModule(hash, chunk, "moduleId") + ");",
this.asString(outputOptions.strictModuleExceptionHandling ? [
"// Execute the module function",
"var threw = true;",
"try {",
this.indent([
"modules[moduleId].call(module.exports, module, module.exports, " + this.renderRequireFunctionForModule(hash, chunk, "moduleId") + ");",
"threw = false;"
]),
"} finally {",
this.indent([
"if(threw) delete installedModules[moduleId];"
]),
"}"
] : [
"// Execute the module function",
"modules[moduleId].call(module.exports, module, module.exports, " + this.renderRequireFunctionForModule(hash, chunk, "moduleId") + ");",
]),
"",
"// Flag the module as loaded",
"module.l = true;",
Expand Down
1 change: 1 addition & 0 deletions lib/WebpackOptionsDefaulter.js
Expand Up @@ -58,6 +58,7 @@ function WebpackOptionsDefaulter() {
this.set("output.hashDigest", "hex");
this.set("output.hashDigestLength", 20);
this.set("output.devtoolLineToLine", false);
this.set("output.strictModuleExceptionHandling", false);

this.set("node", {});
this.set("node.console", false);
Expand Down
4 changes: 4 additions & 0 deletions schemas/webpackOptionsSchema.json
Expand Up @@ -362,6 +362,10 @@
"description": "Prefixes every line of the source in the bundle with this string.",
"type": "string"
},
"strictModuleExceptionHandling": {
"description": "Handles exceptions in module loading correctly at a performance cost.",
"type": "boolean"
},
"umdNamedDefine": {
"description": "If `output.libraryTarget` is set to umd and `output.library` is set, setting this to true will name the AMD module.",
"type": "boolean"
Expand Down
1 change: 1 addition & 0 deletions test/configCases/runtime/opt-in-finally/exception.js
@@ -0,0 +1 @@
throw new Error("Exception");
8 changes: 8 additions & 0 deletions test/configCases/runtime/opt-in-finally/index.js
@@ -0,0 +1,8 @@
it("should throw exception on every try to load a module", function() {
(function() {
require("./exception");
}).should.throw();
(function() {
require("./exception");
}).should.throw();
});
5 changes: 5 additions & 0 deletions test/configCases/runtime/opt-in-finally/webpack.config.js
@@ -0,0 +1,5 @@
module.exports = {
output: {
strictModuleExceptionHandling: true
}
}

0 comments on commit b87bfc0

Please sign in to comment.