1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/packages/next/build/entries.ts
Tim Neutkens 9ffd23eeef
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
2019-01-08 23:10:32 +01:00

68 lines
2.3 KiB
TypeScript

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
}
}