forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request webpack#5235 from faceyspacey/master
feat($context): add "weak-context" asyncMode for universal rendering
- Loading branch information
Showing
32 changed files
with
322 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
"use strict"; | ||
const ImportContextDependency = require("./ImportContextDependency"); | ||
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall"); | ||
|
||
class ImportWeakContextDependency extends ImportContextDependency { | ||
constructor(request, recursive, regExp, range, valueRange, chunkName) { | ||
super(request, recursive, regExp, range, valueRange, chunkName); | ||
this.async = "async-weak"; | ||
} | ||
|
||
get type() { | ||
return "import() context weak"; | ||
} | ||
} | ||
|
||
ImportWeakContextDependency.Template = ContextDependencyTemplateAsRequireCall; | ||
|
||
module.exports = ImportWeakContextDependency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
"use strict"; | ||
const ModuleDependency = require("./ModuleDependency"); | ||
const webpackMissingPromiseModule = require("./WebpackMissingModule").promise; | ||
|
||
class ImportWeakDependency extends ModuleDependency { | ||
constructor(request, range) { | ||
super(request); | ||
this.range = range; | ||
this.weak = true; | ||
} | ||
|
||
get type() { | ||
return "import() weak"; | ||
} | ||
} | ||
|
||
ImportWeakDependency.Template = class ImportDependencyTemplate { | ||
apply(dep, source, outputOptions, requestShortener) { | ||
const comment = this.getOptionalComment(outputOptions.pathinfo, requestShortener.shorten(dep.request)); | ||
|
||
const content = this.getContent(dep, comment); | ||
source.replace(dep.range[0], dep.range[1] - 1, content); | ||
} | ||
|
||
getOptionalComment(pathinfo, shortenedRequest) { | ||
if(!pathinfo) { | ||
return ""; | ||
} | ||
|
||
return `/*! ${shortenedRequest} */ `; | ||
} | ||
|
||
getContent(dep, comment) { | ||
if(dep.module) { | ||
const stringifiedId = JSON.stringify(dep.module.id); | ||
return `Promise.resolve(${comment}${stringifiedId}).then(function(id) { if(!__webpack_require__.m[id]) throw new Error("Module '" + id + "' is not available (weak dependency)"); return __webpack_require__(id); })`; | ||
} | ||
|
||
return webpackMissingPromiseModule(dep.request); | ||
} | ||
}; | ||
|
||
module.exports = ImportWeakDependency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
it("should not bundle context requires with asyncMode === 'weak'", function() { | ||
var contextRequire = require.context(".", false, /two/, "weak"); | ||
(function() { | ||
contextRequire("./two") | ||
}).should.throw(/not available/); | ||
}); | ||
|
||
it("should find module with asyncMode === 'weak' when required elsewhere", function() { | ||
var contextRequire = require.context(".", false, /.+/, "weak"); | ||
contextRequire("./three").should.be.eql(3); | ||
require("./three"); // in a real app would be served as a separate chunk | ||
}); | ||
|
||
it("should find module with asyncMode === 'weak' when required elsewhere (recursive)", function() { | ||
var contextRequire = require.context(".", true, /.+/, "weak"); | ||
contextRequire("./dir/four").should.be.eql(4); | ||
require("./dir/four"); // in a real app would be served as a separate chunk | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "b"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "c"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "b"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "c"; |
Oops, something went wrong.