Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pages-plugin with loader #5994

Merged
merged 26 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f4c6dde
Remove unused argument
timneutkens Jan 5, 2019
38cf34d
Replace pages-plugin with loader
timneutkens Jan 5, 2019
63e6a5e
Add loader-utils types
timneutkens Jan 5, 2019
8036af1
Remove logs
timneutkens Jan 5, 2019
a343679
Bring back previous deposal behavior
timneutkens Jan 5, 2019
71eaed0
Remove console.log
timneutkens Jan 5, 2019
555cf9e
Remove webpack/utils as it’s no longer in use
timneutkens Jan 5, 2019
3ec971a
Remove hot-self-accept-loader
timneutkens Jan 6, 2019
85b19c8
Error Recovery tests
timneutkens Jan 7, 2019
cb0590c
Make hotSelfAccept a noop default loader
timneutkens Jan 7, 2019
b82fbfb
Fix windows deleted/added
timneutkens Jan 7, 2019
3a8888e
Remove logging
timneutkens Jan 7, 2019
4ef30fa
Remove unused variables
timneutkens Jan 7, 2019
4ed7b69
Remove log
timneutkens Jan 7, 2019
27889aa
Simplify entrypoint generation
timneutkens Jan 7, 2019
9bd040c
Don’t return the function
timneutkens Jan 7, 2019
143aab1
Fix _app test
timneutkens Jan 7, 2019
6303cba
Remove code that’s always true
timneutkens Jan 8, 2019
89dec37
Move aliases to constants
timneutkens Jan 8, 2019
025422d
Use alias
timneutkens Jan 8, 2019
418ee0c
Join pages alias in reduce
timneutkens Jan 8, 2019
a013927
Default pages differently
timneutkens Jan 8, 2019
6fdeba9
Loop over pages instead of manually defining
timneutkens Jan 8, 2019
fa58135
Move entry generation into common function
timneutkens Jan 8, 2019
a7f0076
Update packages/next/build/webpack/loaders/next-client-pages-loader.ts
lucleray Jan 8, 2019
e902c55
Update packages/next/build/webpack/loaders/next-client-pages-loader.ts
timneutkens Jan 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 45 additions & 16 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ export default async function build (dir: string, conf = null): Promise<void> {
return result
}, {})

let entrypoints
if (config.target === 'serverless') {
const serverlessEntrypoints: any = {}
// Because on Windows absolute paths in the generated code can break because of numbers, eg 1 in the path,
// we have to use a private alias
const pagesDirAlias = 'private-next-pages'
const dotNextDirAlias = 'private-dot-next'
const absoluteAppPath = pages['/_app'] ? join(pagesDirAlias, pages['/_app']).replace(/\\/g, '/') : 'next/dist/pages/_app'
const absoluteDocumentPath = pages['/_document'] ? join(pagesDirAlias, pages['/_document']).replace(/\\/g, '/') : 'next/dist/pages/_document'
const absoluteErrorPath = pages['/_error'] ? join(pagesDirAlias, pages['/_error']).replace(/\\/g, '/') : 'next/dist/pages/_error'
// Because on Windows absolute paths in the generated code can break because of numbers, eg 1 in the path,
// we have to use a private alias
const pagesDirAlias = 'private-next-pages'
const dotNextDirAlias = 'private-dot-next'

const absoluteAppPath = pages['/_app'] ? join(pagesDirAlias, pages['/_app']).replace(/\\/g, '/') : 'next/dist/pages/_app'
const absoluteDocumentPath = pages['/_document'] ? join(pagesDirAlias, pages['/_document']).replace(/\\/g, '/') : 'next/dist/pages/_document'
const absoluteErrorPath = pages['/_error'] ? join(pagesDirAlias, pages['/_error']).replace(/\\/g, '/') : 'next/dist/pages/_error'

let serverEntrypoints: any = {}

if (config.target === 'serverless') {
const defaultOptions = {
absoluteAppPath,
absoluteDocumentPath,
Expand All @@ -66,21 +67,49 @@ export default async function build (dir: string, conf = null): Promise<void> {
const absolutePagePath = join(pagesDirAlias, pages[page]).replace(/\\/g, '/')
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const serverlessLoaderOptions: ServerlessLoaderQuery = {page, absolutePagePath, ...defaultOptions}
serverlessEntrypoints[join('pages', bundleFile)] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
serverEntrypoints[join('pages', bundleFile)] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
})

const errorPage = join('pages', '/_error.js')
if (!serverlessEntrypoints[errorPage]) {
if (!serverEntrypoints[errorPage]) {
const serverlessLoaderOptions: ServerlessLoaderQuery = {page: '/_error', absolutePagePath: 'next/dist/pages/_error', ...defaultOptions}
serverlessEntrypoints[errorPage] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
serverEntrypoints[errorPage] = `next-serverless-loader?${stringify(serverlessLoaderOptions)}!`
}
} else if(config.target === 'server') {
Object.keys(pages).forEach(async (page) => {
if (page === '/_app' || page === '/_document' || page === '/_error') {
return
}

entrypoints = serverlessEntrypoints
const absolutePagePath = join(pagesDirAlias, pages[page]).replace(/\\/g, '/')
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
serverEntrypoints[join('static', buildId, 'pages', bundleFile)] = [absolutePagePath]
})

serverEntrypoints[join('static', buildId, 'pages', '/_app.js')] = [absoluteAppPath]
serverEntrypoints[join('static', buildId, 'pages', '/_error.js')] = [absoluteErrorPath]
serverEntrypoints[join('static', buildId, 'pages', '/_document.js')] = [absoluteDocumentPath]
}

const clientEntrypoints: any = {}

Object.keys(pages).forEach(async (page) => {
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
if (page === '/_app' || page === '/_document' || page === '/_error') {
return
}

const absolutePagePath = join(pagesDirAlias, pages[page]).replace(/\\/g, '/')
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const clientPagesLoaderOptions = {page, absolutePagePath}
clientEntrypoints[join('static', buildId, 'pages', bundleFile)] = `next-client-pages-loader?${stringify(clientPagesLoaderOptions)}!`
})

clientEntrypoints[join('static', buildId, 'pages', '/_app.js')] = `next-client-pages-loader?${stringify({page: '/_app', absolutePagePath: absoluteAppPath})}!`
clientEntrypoints[join('static', buildId, 'pages', '/_error.js')] = `next-client-pages-loader?${stringify({page: '/_error', absolutePagePath: absoluteErrorPath})}!`

const configs: any = await Promise.all([
getBaseWebpackConfig(dir, { buildId, isServer: false, config, target: config.target }),
getBaseWebpackConfig(dir, { buildId, isServer: true, config, target: config.target, entrypoints })
getBaseWebpackConfig(dir, { buildId, isServer: false, config, target: config.target, entrypoints: clientEntrypoints }),
getBaseWebpackConfig(dir, { buildId, isServer: true, config, target: config.target, entrypoints: serverEntrypoints })
])

let result: CompilerResult = {warnings: [], errors: []}
Expand Down
33 changes: 8 additions & 25 deletions packages/next/build/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import resolve from 'resolve'
import CaseSensitivePathPlugin from 'case-sensitive-paths-webpack-plugin'
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
import WebpackBar from 'webpackbar'
import {getPages} from './webpack/utils'
import PagesPlugin from './webpack/plugins/pages-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cache'
import NextJsRequireCacheHotReloader from './webpack/plugins/nextjs-require-cache-hot-reloader'
Expand Down Expand Up @@ -139,22 +137,18 @@ function optimizationConfig ({ dev, isServer, totalPages, target }) {
return config
}

export default async function getBaseWebpackConfig (dir, {dev = false, isServer = false, buildId, config, target = 'server', entrypoints = false}) {
export default async function getBaseWebpackConfig (dir, {dev = false, isServer = false, buildId, config, target = 'server', entrypoints}) {
if (!entrypoints) {
throw new Error('No entrypoints provided')
}
const defaultLoaders = {
babel: {
loader: 'next-babel-loader',
options: {dev, isServer, cwd: dir}
},
// Backwards compat
hotSelfAccept: {
loader: 'hot-self-accept-loader',
options: {
include: [
path.join(dir, 'pages')
],
// All pages are javascript files. So we apply hot-self-accept-loader here to facilitate hot reloading of pages.
// This makes sure plugins just have to implement `pageExtensions` instead of also implementing the loader
extensions: new RegExp(`\\.+(${config.pageExtensions.join('|')})$`)
}
loader: 'noop-loader'
}
}

Expand All @@ -166,8 +160,7 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
const distDir = path.join(dir, config.distDir)
const outputDir = target === 'serverless' ? 'serverless' : SERVER_DIRECTORY
const outputPath = path.join(distDir, isServer ? outputDir : '')
const pagesEntries = await getPages(dir, {nextPagesDir: DEFAULT_PAGES_DIR, dev, buildId, isServer, pageExtensions: config.pageExtensions.join('|')})
const totalPages = Object.keys(pagesEntries).length
const totalPages = Object.keys(entrypoints).length
const clientEntries = !isServer ? {
// Backwards compatibility
'main.js': [],
Expand Down Expand Up @@ -205,13 +198,9 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
context: dir,
// Kept as function to be backwards compatible
entry: async () => {
if (entrypoints) {
return entrypoints
}
return {
...clientEntries,
// Only _error and _document when in development. The rest is handled by on-demand-entries
...pagesEntries
...entrypoints
}
},
output: {
Expand Down Expand Up @@ -244,11 +233,6 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
},
module: {
rules: [
dev && !isServer && {
test: defaultLoaders.hotSelfAccept.options.extensions,
include: defaultLoaders.hotSelfAccept.options.include,
use: defaultLoaders.hotSelfAccept
},
{
test: /\.(js|jsx)$/,
include: [dir, NEXT_PROJECT_ROOT_DIST_CLIENT, DEFAULT_PAGES_DIR, /next-server[\\/]dist[\\/]lib/],
Expand Down Expand Up @@ -309,7 +293,6 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
}),
target !== 'serverless' && isServer && new PagesManifestPlugin(),
!isServer && new BuildManifestPlugin(),
!isServer && new PagesPlugin(),
isServer && new NextJsSsrImportPlugin(),
target !== 'serverless' && isServer && new NextJsSSRModuleCachePlugin({outputPath}),
!isServer && !dev && new AssetsSizePlugin({buildId, distDir})
Expand Down
50 changes: 0 additions & 50 deletions packages/next/build/webpack/loaders/hot-self-accept-loader.js

This file was deleted.

29 changes: 29 additions & 0 deletions packages/next/build/webpack/loaders/next-client-pages-loader.ts
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 nextServerlessLoader: loader.Loader = function () {
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
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 nextServerlessLoader
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/next/build/webpack/loaders/noop-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (source) => source
53 changes: 0 additions & 53 deletions packages/next/build/webpack/plugins/pages-plugin.js

This file was deleted.

80 changes: 0 additions & 80 deletions packages/next/build/webpack/utils.js

This file was deleted.