-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace pages-plugin with loader (#5994)
* Remove unused argument * Replace pages-plugin with loader * Add loader-utils types * Remove logs * Bring back previous deposal behavior * Remove console.log * Remove webpack/utils as it’s no longer in use * Remove hot-self-accept-loader * Error Recovery tests * Make hotSelfAccept a noop default loader * Fix windows deleted/added * Remove logging * Remove unused variables * Remove log * Simplify entrypoint generation * Don’t return the function * Fix _app test * Remove code that’s always true * Move aliases to constants * Use alias * Join pages alias in reduce * Default pages differently * Loop over pages instead of manually defining * Move entry generation into common function * Update packages/next/build/webpack/loaders/next-client-pages-loader.ts Co-Authored-By: timneutkens <tim@timneutkens.nl> * Update packages/next/build/webpack/loaders/next-client-pages-loader.ts
- Loading branch information
1 parent
3a3347d
commit 9ffd23e
Showing
16 changed files
with
202 additions
and
512 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {join} from 'path' | ||
import {stringify} from 'querystring' | ||
import {PAGES_DIR_ALIAS, DOT_NEXT_ALIAS} from '../lib/constants' | ||
import {ServerlessLoaderQuery} from './webpack/loaders/next-serverless-loader' | ||
|
||
type PagesMapping = { | ||
[page: string]: string | ||
} | ||
|
||
export function createPagesMapping(pagePaths: string[], extensions: string[]): PagesMapping { | ||
const pages: PagesMapping = pagePaths.reduce((result: PagesMapping, pagePath): PagesMapping => { | ||
const page = `/${pagePath.replace(new RegExp(`\\.+(${extensions.join('|')})$`), '').replace(/\\/g, '/')}`.replace(/\/index$/, '') | ||
result[page === '' ? '/' : page] = join(PAGES_DIR_ALIAS, pagePath).replace(/\\/g, '/') | ||
return result | ||
}, {}) | ||
|
||
pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app' | ||
pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error' | ||
pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document' | ||
|
||
return pages | ||
} | ||
|
||
type WebpackEntrypoints = { | ||
[bundle: string]: string|string[] | ||
} | ||
|
||
type Entrypoints = { | ||
client: WebpackEntrypoints | ||
server: WebpackEntrypoints | ||
} | ||
|
||
export function createEntrypoints(pages: PagesMapping, target: 'server'|'serverless', buildId: string, config: any): Entrypoints { | ||
const client: WebpackEntrypoints = {} | ||
const server: WebpackEntrypoints = {} | ||
|
||
const defaultServerlessOptions = { | ||
absoluteAppPath: pages['/_app'], | ||
absoluteDocumentPath: pages['/_document'], | ||
absoluteErrorPath: pages['/_error'], | ||
distDir: DOT_NEXT_ALIAS, | ||
buildId, | ||
assetPrefix: config.assetPrefix, | ||
generateEtags: config.generateEtags | ||
} | ||
|
||
Object.keys(pages).forEach((page) => { | ||
const absolutePagePath = pages[page] | ||
const bundleFile = page === '/' ? '/index.js' : `${page}.js` | ||
const bundlePath = join('static', buildId, 'pages', bundleFile) | ||
if(target === 'serverless') { | ||
const serverlessLoaderOptions: ServerlessLoaderQuery = {page, absolutePagePath, ...defaultServerlessOptions} | ||
server[join('pages', bundleFile)] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!` | ||
} else if(target === 'server') { | ||
server[bundlePath] = [absolutePagePath] | ||
} | ||
if (page === '/_document') { | ||
return | ||
} | ||
client[bundlePath] = `next-client-pages-loader?${stringify({page, absolutePagePath})}!` | ||
}) | ||
|
||
return { | ||
client, | ||
server | ||
} | ||
} |
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
50 changes: 0 additions & 50 deletions
50
packages/next/build/webpack/loaders/hot-self-accept-loader.js
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
packages/next/build/webpack/loaders/next-client-pages-loader.ts
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,29 @@ | ||
import {loader} from 'webpack' | ||
import loaderUtils from 'loader-utils' | ||
|
||
export type ClientPagesLoaderOptions = { | ||
absolutePagePath: string, | ||
page: string | ||
} | ||
|
||
const nextClientPagesLoader: loader.Loader = function () { | ||
const {absolutePagePath, page}: any = loaderUtils.getOptions(this) | ||
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath) | ||
const stringifiedPage = JSON.stringify(page) | ||
|
||
return ` | ||
(window.__NEXT_P=window.__NEXT_P||[]).push([${stringifiedPage}, function() { | ||
var page = require(${stringifiedAbsolutePagePath}) | ||
if(module.hot) { | ||
module.hot.accept(${stringifiedAbsolutePagePath}, function() { | ||
if(!next.router.components[${stringifiedPage}]) return | ||
var updatedPage = require(${stringifiedAbsolutePagePath}) | ||
next.router.update(${stringifiedPage}, updatedPage.default || updatedPage) | ||
}) | ||
} | ||
return { page: page.default || page } | ||
}]); | ||
` | ||
} | ||
|
||
export default nextClientPagesLoader |
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 = (source) => source |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.