feat($context): add "weak-context" asyncMode for universal rendering#5235
feat($context): add "weak-context" asyncMode for universal rendering#5235sokra merged 12 commits intowebpack:masterfrom
Conversation
sokra
left a comment
There was a problem hiding this comment.
Also do the following:
- Add your new asyncModes to the magic comment for
import(). - Update the parser plugin for
require.resolveWeakto set the asyncMode for the generated context.
lib/ContextModule.js
Outdated
| } | ||
|
|
||
| getSourceString(asyncMode, outputOptions, requestShortener) { | ||
| if(asyncMode === "weak-context") { |
There was a problem hiding this comment.
omit the context. It's always a context. asnycMode === "weak"
lib/ContextModule.js
Outdated
|
|
||
| // store the dependences in a different key than `this.dependences` | ||
| // to prevent them from being bundled in the parent | ||
| this.weakDependencies = dependencies; |
There was a problem hiding this comment.
Instead of doing this, you need to set a weak property to true on each of this.dependencies.
lib/ContextModule.js
Outdated
|
|
||
| getSourceString(asyncMode, outputOptions, requestShortener) { | ||
| if(asyncMode === "weak-context") { | ||
| return this.getSyncSource(this.weakDependencies, this.id); |
There was a problem hiding this comment.
create a new method getWeakSyncSource.
You can copy the getSyncSource method, but add a check on require like this
function webpackContext(req) {
var id = webpackContextResolve(req);
if(!__webpack_require__.m[id])
throw new Error("Module '" + id + "' is not available (weak dependency)");
return __webpack_require__(id);
};And do the same of getWeakEagerSource with asyncMode === "eager-weak". The difference to getWeakSyncSource is that it returns a Promise so it can be used for
import(/* webpackMode: "eager-weak" */`./templates/${expr}.js`)
| // falls through | ||
| case 4: | ||
| { | ||
| const weakExpr = parser.evaluateExpression(expr.arguments[3]); |
There was a problem hiding this comment.
pass the asyncMode as argument instead.
package-lock.json
Outdated
| @@ -0,0 +1,5124 @@ | |||
| { | |||
There was a problem hiding this comment.
We use yarn. Don't add this file and update yarn.lock instead.
|
Hi @faceyspacey. Just a little hint from a friendly bot about the best practice when submitting pull requests:
You don't have to change it for this PR, just make sure to follow this hint the next time you submit a PR. |
|
This is very exciting @faceyspacey congrats and thank you for submitting your first webpack/webpack PR 🎉 |
|
thanks @TheLarkInn I'm excited to get this right! Thanks for helping push the SSR+Splitting movement along. Not sure if you saw, but I released something yesterday that provides a nice "stop gap" solution to sokra's eventual plan of dual css + js imports: Will help get developers ready for Webpack's future. Maybe I can submit some PRs related to that as well. Not sure where that stuff is being built though. |
|
Once you get this one taken care of and @sokra and @Kovensky get ya taken care of his, I'll ping ya and get ya involved with the ICSS2/style team. |
|
@faceyspacey Thanks for your update. I labeled the Pull Request so reviewers will review it again. @sokra Please review the new changes. |
|
The requested changes/additions have been implemented, as well as tests + docs. There's 4 features this PR addresses: Features
Notes
Tests
DEMOThe demo showcase the 4 features, as well as a 5th for babel-plugin-universal-import, which brings the final concept together. 🚀 Here's how you get it:
Additional notes are in the readme for the resolve-weak branch. In short, open Notes on new
|
rename ImportEagerWeak to ImportWeak rename "eager-weak" asyncMode to "async-weak" weak dependencies don't need to be in dependencies blocks
|
Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon. |
|
It looks like this Pull Request doesn't include enough test cases (based on Code Coverage analysis of the PR diff). A PR need to be covered by tests if you add a new feature (we want to make sure that your feature is working) or if you fix a bug (we want to make sure that we don't run into a regression in future). @faceyspacey Please check if this is appliable to your PR and if you can add more test cases. Read the test readme for details how to write test cases. |
|
Thanks |
|
Awesome work! 🎉 |
|
YAY!! |
Add eager-weak + notes to the `resolveWeak` section of the Module Methods page. The corresponding webpack PR that added this functionality was merged and released: webpack/webpack#5235
|
Is there a way to use the magic comment to name the chunks? I've tried this and it doesn't work: const pages = require.context(/* webpackChunkName: "[request]" */
"@/pages/", true, /^\.\/.*\.vue$/, "lazy")It seems that when chunks are created through |

What kind of change does this PR introduce?
Feature slash bugfix.
It makes it possible to achieve the "universal rendering" use-case that
require.resolveWeakhas become a common solution for in packages such as React Universal Component and React Loadable.The use-case is to be able to synchronously render dynamic chunks (when available) without including them in the parent bundle. These additional dynamic chunks are then left up to the developer to detect and serve as needed. Webpack Flush Chunks offers a semi-automated solution to accomplish this.
Usage (use-case):
The 4th parameter of
require.contextis a boolean indicating you want to create a"weak-context"and the 5th parameter is achunkNamejust like you supply as a "magic comment."Did you add tests for your changes?
Coming soon...
Summary
Solves this issue:
#4993
Ideally we should also update the
require.resolveWeakfunction, but I figured I'd get this out sooner than later, since all that really matters is the use-case. Please give me tips on how to updaterequire.resolveWeak. I wasn't successful at my first go at trying to update that. My first attempt involved rewriting the__webpack_require__replacement in ContextDependencyTemplateAsId to berequire.contextinstead, but obviously it's being replaced too late in the game.