Skip to content

Commit 2d8aeee

Browse files
committed
Add comment explaining what this does
1 parent 8fb1d4f commit 2d8aeee

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

build/webpack/plugins/pages-plugin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ import {
88
export default class PagesPlugin {
99
apply (compiler: any) {
1010
compiler.hooks.compilation.tap('PagesPlugin', (compilation) => {
11+
// This hook is triggered right before a module gets wrapped into it's initializing function,
12+
// For example when you look at the source of a bundle you'll see an object holding `'pages/_app.js': function(module, etc, etc)`
13+
// This hook triggers right before that code is added and wraps the module into `__NEXT_REGISTER_PAGE` when the module is a page
14+
// The reason we're doing this is that we don't want to execute the page code which has potential side effects before switching to a route
1115
compilation.moduleTemplates.javascript.hooks.render.tap('PagesPluginRenderPageRegister', (moduleSourcePostModule, module, options) => {
1216
const {chunk} = options
1317

14-
// check if the current module is the entry module
18+
// check if the current module is the entry module, we only want to wrap the topmost module
1519
if (chunk.entryModule !== module) {
1620
return moduleSourcePostModule
1721
}
1822

23+
// Check if the chunk is a page
1924
if (!IS_BUNDLED_PAGE_REGEX.test(chunk.name)) {
2025
return moduleSourcePostModule
2126
}
2227

28+
// Match the route the chunk belongs to
2329
let routeName = ROUTE_NAME_REGEX.exec(chunk.name)[1]
2430

2531
// We need to convert \ into / when we are in windows

0 commit comments

Comments
 (0)