2018-07-24 09:24:40 +00:00
|
|
|
import path from 'path'
|
2016-10-14 15:05:08 +00:00
|
|
|
import webpack from 'webpack'
|
2018-01-30 15:40:52 +00:00
|
|
|
import resolve from 'resolve'
|
|
|
|
import CaseSensitivePathPlugin from 'case-sensitive-paths-webpack-plugin'
|
2016-12-03 22:01:15 +00:00
|
|
|
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
|
2018-07-24 09:24:40 +00:00
|
|
|
import WebpackBar from 'webpackbar'
|
2018-06-16 17:23:02 +00:00
|
|
|
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
|
2018-07-24 09:24:40 +00:00
|
|
|
import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cache'
|
|
|
|
import NextJsRequireCacheHotReloader from './webpack/plugins/nextjs-require-cache-hot-reloader'
|
2018-06-16 17:23:02 +00:00
|
|
|
import UnlinkFilePlugin from './webpack/plugins/unlink-file-plugin'
|
|
|
|
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
|
|
|
|
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
|
2018-07-24 09:24:40 +00:00
|
|
|
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
|
|
|
|
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
|
2018-10-01 22:55:31 +00:00
|
|
|
import {SERVER_DIRECTORY, REACT_LOADABLE_MANIFEST, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants'
|
2019-01-11 21:26:27 +00:00
|
|
|
import {NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST_CLIENT, PAGES_DIR_ALIAS, DOT_NEXT_ALIAS} from '../lib/constants'
|
2018-08-13 22:09:05 +00:00
|
|
|
import AutoDllPlugin from 'autodll-webpack-plugin'
|
2019-02-10 03:55:09 +00:00
|
|
|
import TerserPlugin from './webpack/plugins/terser-webpack-plugin/src/cjs.js'
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
import {ServerlessPlugin} from './webpack/plugins/serverless-plugin'
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// The externals config makes sure that
|
|
|
|
// on the server side when modules are
|
|
|
|
// in node_modules they don't get compiled by webpack
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
function externalsConfig (isServer, target) {
|
2018-01-30 15:40:52 +00:00
|
|
|
const externals = []
|
2017-12-05 23:46:06 +00:00
|
|
|
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
// When the serverless target is used all node_modules will be compiled into the output bundles
|
|
|
|
// So that the serverless bundles have 0 runtime dependencies
|
|
|
|
if (!isServer || target === 'serverless') {
|
2018-01-30 15:40:52 +00:00
|
|
|
return externals
|
|
|
|
}
|
2017-12-05 23:46:06 +00:00
|
|
|
|
2018-11-28 14:03:02 +00:00
|
|
|
const notExternalModules = ['next/app', 'next/document', 'next/link', 'next/router', 'next/error', 'http-status', 'string-hash', 'ansi-html', 'hoist-non-react-statics', 'htmlescape']
|
2018-10-02 22:08:57 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
externals.push((context, request, callback) => {
|
2018-10-02 22:08:57 +00:00
|
|
|
if (notExternalModules.indexOf(request) !== -1) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(request, { basedir: context, preserveSymlinks: true }, (err, res) => {
|
2018-01-30 15:40:52 +00:00
|
|
|
if (err) {
|
|
|
|
return callback()
|
|
|
|
}
|
2017-02-16 02:07:29 +00:00
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
// Default pages have to be transpiled
|
2019-01-27 20:56:02 +00:00
|
|
|
if (res.match(/next[/\\]dist[/\\]/) || res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/) || res.match(/node_modules[/\\]@babel[/\\]runtime-corejs2[/\\]/)) {
|
2018-11-07 14:38:35 +00:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
// Webpack itself has to be compiled because it doesn't always use module relative paths
|
2018-07-30 13:48:02 +00:00
|
|
|
if (res.match(/node_modules[/\\]webpack/) || res.match(/node_modules[/\\]css-loader/)) {
|
2018-02-06 12:09:41 +00:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2018-11-04 00:11:40 +00:00
|
|
|
// styled-jsx has to be transpiled
|
|
|
|
if (res.match(/node_modules[/\\]styled-jsx/)) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2018-03-31 14:08:16 +00:00
|
|
|
if (res.match(/node_modules[/\\].*\.js$/)) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return callback(null, `commonjs ${request}`)
|
|
|
|
}
|
2017-01-31 06:31:27 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
2017-08-30 10:49:40 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
return externals
|
|
|
|
}
|
2017-08-30 10:49:40 +00:00
|
|
|
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
function optimizationConfig ({ dev, isServer, totalPages, target }) {
|
2018-11-22 09:14:14 +00:00
|
|
|
const terserPluginConfig = {
|
|
|
|
parallel: true,
|
|
|
|
sourceMap: false,
|
|
|
|
cache: true,
|
|
|
|
cacheKeys: (keys) => {
|
|
|
|
// path changes per build because we add buildId
|
|
|
|
// because the input is already hashed the path is not needed
|
|
|
|
delete keys.path
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
if (isServer && target === 'serverless') {
|
2018-11-17 19:15:33 +00:00
|
|
|
return {
|
|
|
|
splitChunks: false,
|
|
|
|
minimizer: [
|
2019-01-11 23:15:12 +00:00
|
|
|
new TerserPlugin({...terserPluginConfig,
|
|
|
|
terserOptions: {
|
|
|
|
compress: false,
|
|
|
|
mangle: false,
|
|
|
|
module: false,
|
|
|
|
keep_classnames: true,
|
|
|
|
keep_fnames: true
|
|
|
|
}
|
|
|
|
})
|
2018-11-17 19:15:33 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
if (isServer) {
|
|
|
|
return {
|
|
|
|
splitChunks: false,
|
|
|
|
minimize: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:04:37 +00:00
|
|
|
const config = {
|
2018-07-24 09:24:40 +00:00
|
|
|
runtimeChunk: {
|
2018-07-27 17:29:25 +00:00
|
|
|
name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK
|
2018-07-24 09:24:40 +00:00
|
|
|
},
|
2018-07-30 13:48:02 +00:00
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
default: false,
|
|
|
|
vendors: false
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 09:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev) {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2018-09-03 18:59:11 +00:00
|
|
|
// Terser is a better uglifier
|
2018-11-05 16:51:56 +00:00
|
|
|
config.minimizer = [
|
2019-01-16 09:32:47 +00:00
|
|
|
new TerserPlugin({...terserPluginConfig,
|
|
|
|
terserOptions: {
|
|
|
|
safari10: true
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 16:51:56 +00:00
|
|
|
]
|
2018-09-03 18:59:11 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// Only enabled in production
|
|
|
|
// This logic will create a commons bundle
|
|
|
|
// with modules that are used in 50% of all pages
|
2018-07-30 13:48:02 +00:00
|
|
|
config.splitChunks.chunks = 'all'
|
|
|
|
config.splitChunks.cacheGroups.commons = {
|
|
|
|
name: 'commons',
|
|
|
|
chunks: 'all',
|
|
|
|
minChunks: totalPages > 2 ? totalPages * 0.5 : 2
|
2018-07-24 09:24:40 +00:00
|
|
|
}
|
2018-09-17 13:57:28 +00:00
|
|
|
config.splitChunks.cacheGroups.react = {
|
|
|
|
name: 'commons',
|
|
|
|
chunks: 'all',
|
|
|
|
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/
|
|
|
|
}
|
2018-07-30 13:48:02 +00:00
|
|
|
|
|
|
|
return config
|
2018-07-24 09:24:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:10:32 +00:00
|
|
|
export default async function getBaseWebpackConfig (dir, {dev = false, isServer = false, buildId, config, target = 'server', entrypoints}) {
|
2018-01-30 15:40:52 +00:00
|
|
|
const defaultLoaders = {
|
|
|
|
babel: {
|
2018-05-23 18:26:57 +00:00
|
|
|
loader: 'next-babel-loader',
|
2018-09-08 23:10:47 +00:00
|
|
|
options: {dev, isServer, cwd: dir}
|
2018-06-14 17:30:14 +00:00
|
|
|
},
|
2019-01-08 22:10:32 +00:00
|
|
|
// Backwards compat
|
2018-06-14 17:30:14 +00:00
|
|
|
hotSelfAccept: {
|
2019-01-08 22:10:32 +00:00
|
|
|
loader: 'noop-loader'
|
2016-10-15 16:17:27 +00:00
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:21:18 +00:00
|
|
|
// Support for NODE_PATH
|
|
|
|
const nodePathList = (process.env.NODE_PATH || '')
|
|
|
|
.split(process.platform === 'win32' ? ';' : ':')
|
|
|
|
.filter((p) => !!p)
|
|
|
|
|
2018-09-05 15:49:49 +00:00
|
|
|
const distDir = path.join(dir, config.distDir)
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
const outputDir = target === 'serverless' ? 'serverless' : SERVER_DIRECTORY
|
|
|
|
const outputPath = path.join(distDir, isServer ? outputDir : '')
|
2019-01-08 22:10:32 +00:00
|
|
|
const totalPages = Object.keys(entrypoints).length
|
2018-03-31 12:00:56 +00:00
|
|
|
const clientEntries = !isServer ? {
|
2018-07-24 09:24:40 +00:00
|
|
|
// Backwards compatibility
|
|
|
|
'main.js': [],
|
2018-07-27 17:29:25 +00:00
|
|
|
[CLIENT_STATIC_FILES_RUNTIME_MAIN]: [
|
2018-11-28 14:03:02 +00:00
|
|
|
path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, (dev ? `next-dev` : 'next'))
|
2018-03-31 12:00:56 +00:00
|
|
|
].filter(Boolean)
|
|
|
|
} : {}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2018-08-13 22:09:05 +00:00
|
|
|
const resolveConfig = {
|
2018-11-20 11:13:31 +00:00
|
|
|
// Disable .mjs for node_modules bundling
|
2018-12-16 15:26:45 +00:00
|
|
|
extensions: isServer ? ['.wasm', '.js', '.mjs', '.jsx', '.json'] : ['.wasm', '.mjs', '.js', '.jsx', '.json'],
|
2018-08-13 22:09:05 +00:00
|
|
|
modules: [
|
|
|
|
'node_modules',
|
|
|
|
...nodePathList // Support for NODE_PATH environment variable
|
|
|
|
],
|
|
|
|
alias: {
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
next: NEXT_PROJECT_ROOT,
|
2019-01-08 22:10:32 +00:00
|
|
|
[PAGES_DIR_ALIAS]: path.join(dir, 'pages'),
|
|
|
|
[DOT_NEXT_ALIAS]: distDir
|
2018-12-04 09:59:12 +00:00
|
|
|
},
|
2019-02-12 00:39:57 +00:00
|
|
|
mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main']
|
2018-08-13 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 10:27:22 +00:00
|
|
|
const webpackMode = dev ? 'development' : 'production'
|
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
let webpackConfig = {
|
2018-08-30 10:27:22 +00:00
|
|
|
mode: webpackMode,
|
2018-04-18 16:18:06 +00:00
|
|
|
devtool: dev ? 'cheap-module-source-map' : false,
|
2018-01-30 15:40:52 +00:00
|
|
|
name: isServer ? 'server' : 'client',
|
|
|
|
target: isServer ? 'node' : 'web',
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
externals: externalsConfig(isServer, target),
|
|
|
|
optimization: optimizationConfig({dir, dev, isServer, totalPages, target}),
|
2018-07-24 09:24:40 +00:00
|
|
|
recordsPath: path.join(outputPath, 'records.json'),
|
2016-10-14 15:05:08 +00:00
|
|
|
context: dir,
|
2018-03-31 12:00:56 +00:00
|
|
|
// Kept as function to be backwards compatible
|
2018-01-30 15:40:52 +00:00
|
|
|
entry: async () => {
|
|
|
|
return {
|
2018-03-31 12:00:56 +00:00
|
|
|
...clientEntries,
|
2019-01-08 22:10:32 +00:00
|
|
|
...entrypoints
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
|
|
|
},
|
2016-10-14 15:05:08 +00:00
|
|
|
output: {
|
2018-07-24 09:24:40 +00:00
|
|
|
path: outputPath,
|
|
|
|
filename: ({chunk}) => {
|
2018-08-04 17:00:13 +00:00
|
|
|
// Use `[name]-[contenthash].js` in production
|
2018-07-27 17:29:25 +00:00
|
|
|
if (!dev && (chunk.name === CLIENT_STATIC_FILES_RUNTIME_MAIN || chunk.name === CLIENT_STATIC_FILES_RUNTIME_WEBPACK)) {
|
2018-08-04 18:18:20 +00:00
|
|
|
return chunk.name.replace(/\.js$/, '-[contenthash].js')
|
2018-07-24 09:24:40 +00:00
|
|
|
}
|
|
|
|
return '[name]'
|
|
|
|
},
|
2018-09-04 15:30:01 +00:00
|
|
|
libraryTarget: isServer ? 'commonjs2' : 'jsonp',
|
2018-07-24 09:24:40 +00:00
|
|
|
hotUpdateChunkFilename: 'static/webpack/[id].[hash].hot-update.js',
|
|
|
|
hotUpdateMainFilename: 'static/webpack/[hash].hot-update.json',
|
|
|
|
// This saves chunks with the name given via `import()`
|
2018-09-02 23:40:21 +00:00
|
|
|
chunkFilename: isServer ? `${dev ? '[name]' : '[name].[contenthash]'}.js` : `static/chunks/${dev ? '[name]' : '[name].[contenthash]'}.js`,
|
2018-10-02 11:10:07 +00:00
|
|
|
strictModuleExceptionHandling: true,
|
2018-12-13 00:05:21 +00:00
|
|
|
crossOriginLoading: config.crossOrigin,
|
2019-01-24 09:25:58 +00:00
|
|
|
futureEmitAssets: !dev,
|
2018-10-02 11:10:07 +00:00
|
|
|
webassemblyModuleFilename: 'static/wasm/[modulehash].wasm'
|
2016-10-14 15:05:08 +00:00
|
|
|
},
|
2018-01-30 15:40:52 +00:00
|
|
|
performance: { hints: false },
|
2018-08-13 22:09:05 +00:00
|
|
|
resolve: resolveConfig,
|
2016-10-14 15:05:08 +00:00
|
|
|
resolveLoader: {
|
2016-12-28 18:16:52 +00:00
|
|
|
modules: [
|
2018-06-14 17:30:14 +00:00
|
|
|
NEXT_PROJECT_ROOT_NODE_MODULES,
|
2017-01-01 05:57:13 +00:00
|
|
|
'node_modules',
|
2018-06-16 17:23:02 +00:00
|
|
|
path.join(__dirname, 'webpack', 'loaders'), // The loaders Next.js provides
|
2018-02-01 15:21:18 +00:00
|
|
|
...nodePathList // Support for NODE_PATH environment variable
|
2016-10-14 15:05:08 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
module: {
|
2018-01-03 12:43:48 +00:00
|
|
|
rules: [
|
2018-01-30 15:40:52 +00:00
|
|
|
{
|
2019-02-11 17:59:24 +00:00
|
|
|
test: /\.(js|mjs|jsx)$/,
|
2019-01-11 21:26:27 +00:00
|
|
|
include: [dir, /next-server[\\/]dist[\\/]lib/],
|
2018-11-28 14:03:02 +00:00
|
|
|
exclude: (path) => {
|
2019-01-11 21:26:27 +00:00
|
|
|
if (/next-server[\\/]dist[\\/]lib/.exec(path)) {
|
2018-11-28 14:03:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return /node_modules/.exec(path)
|
|
|
|
},
|
2018-01-30 15:40:52 +00:00
|
|
|
use: defaultLoaders.babel
|
|
|
|
}
|
|
|
|
].filter(Boolean)
|
2016-10-16 04:01:17 +00:00
|
|
|
},
|
2018-01-30 15:40:52 +00:00
|
|
|
plugins: [
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
target === 'serverless' && isServer && new ServerlessPlugin(),
|
2018-08-13 22:09:05 +00:00
|
|
|
// Precompile react / react-dom for development, speeding up webpack
|
|
|
|
dev && !isServer && new AutoDllPlugin({
|
|
|
|
filename: '[name]_[hash].js',
|
2018-09-02 23:40:21 +00:00
|
|
|
path: './static/development/dll',
|
2018-08-13 22:09:05 +00:00
|
|
|
context: dir,
|
|
|
|
entry: {
|
|
|
|
dll: [
|
|
|
|
'react',
|
|
|
|
'react-dom'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
config: {
|
2018-08-30 10:27:22 +00:00
|
|
|
mode: webpackMode,
|
2018-08-13 22:09:05 +00:00
|
|
|
resolve: resolveConfig
|
|
|
|
}
|
|
|
|
}),
|
2018-07-24 09:24:40 +00:00
|
|
|
// This plugin makes sure `output.filename` is used for entry chunks
|
|
|
|
new ChunkNamesPlugin(),
|
|
|
|
!isServer && new ReactLoadablePlugin({
|
|
|
|
filename: REACT_LOADABLE_MANIFEST
|
|
|
|
}),
|
|
|
|
new WebpackBar({
|
|
|
|
name: isServer ? 'server' : 'client'
|
|
|
|
}),
|
|
|
|
dev && !isServer && new FriendlyErrorsWebpackPlugin(),
|
|
|
|
// Even though require.cache is server only we have to clear assets from both compilations
|
|
|
|
// This is because the client compilation generates the build manifest that's used on the server side
|
|
|
|
dev && new NextJsRequireCacheHotReloader(),
|
|
|
|
dev && !isServer && new webpack.HotModuleReplacementPlugin(),
|
2018-01-30 15:40:52 +00:00
|
|
|
dev && new webpack.NoEmitOnErrorsPlugin(),
|
|
|
|
dev && new UnlinkFilePlugin(),
|
|
|
|
dev && new CaseSensitivePathPlugin(), // Since on macOS the filesystem is case-insensitive this will make sure your path are case-sensitive
|
2018-11-05 16:51:56 +00:00
|
|
|
!dev && new webpack.HashedModuleIdsPlugin(),
|
2018-09-23 14:02:28 +00:00
|
|
|
// Removes server/client code by minifier
|
2019-02-08 13:38:18 +00:00
|
|
|
new webpack.DefinePlugin(Object.assign(
|
|
|
|
{},
|
|
|
|
config.env ? Object.keys(config.env)
|
|
|
|
.reduce((acc, key) => ({
|
|
|
|
...acc,
|
|
|
|
...{ [`process.env.${key}`]: JSON.stringify(config.env[key]) }
|
|
|
|
}), {}) : {},
|
|
|
|
{
|
|
|
|
'process.crossOrigin': JSON.stringify(config.crossOrigin),
|
|
|
|
'process.browser': JSON.stringify(!isServer)
|
|
|
|
}
|
|
|
|
)),
|
2018-09-05 15:49:49 +00:00
|
|
|
// This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory
|
|
|
|
!isServer && dev && new webpack.DefinePlugin({
|
|
|
|
'process.env.__NEXT_DIST_DIR': JSON.stringify(distDir)
|
|
|
|
}),
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
target !== 'serverless' && isServer && new PagesManifestPlugin(),
|
2018-04-12 07:47:42 +00:00
|
|
|
!isServer && new BuildManifestPlugin(),
|
2018-02-19 10:49:41 +00:00
|
|
|
isServer && new NextJsSsrImportPlugin(),
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 10:39:12 +00:00
|
|
|
target !== 'serverless' && isServer && new NextJsSSRModuleCachePlugin({outputPath}),
|
2019-01-18 11:12:29 +00:00
|
|
|
!dev && new webpack.IgnorePlugin({
|
|
|
|
checkResource: (resource) => {
|
|
|
|
return /react-is/.test(resource)
|
|
|
|
},
|
|
|
|
checkContext: (context) => {
|
|
|
|
return /next-server[\\/]dist[\\/]/.test(context) || /next[\\/]dist[\\/]/.test(context)
|
|
|
|
}
|
|
|
|
})
|
2018-01-30 15:40:52 +00:00
|
|
|
].filter(Boolean)
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-12-22 01:36:00 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
if (typeof config.webpack === 'function') {
|
2018-02-17 11:37:33 +00:00
|
|
|
webpackConfig = config.webpack(webpackConfig, {dir, dev, isServer, buildId, config, defaultLoaders, totalPages})
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// Backwards compat for `main.js` entry key
|
|
|
|
const originalEntry = webpackConfig.entry
|
|
|
|
webpackConfig.entry = async () => {
|
2018-11-21 15:04:37 +00:00
|
|
|
const entry = {...await originalEntry()}
|
2018-07-24 09:24:40 +00:00
|
|
|
|
|
|
|
// Server compilation doesn't have main.js
|
|
|
|
if (typeof entry['main.js'] !== 'undefined') {
|
2018-07-27 17:29:25 +00:00
|
|
|
entry[CLIENT_STATIC_FILES_RUNTIME_MAIN] = [
|
2018-07-24 09:24:40 +00:00
|
|
|
...entry['main.js'],
|
2018-07-27 17:29:25 +00:00
|
|
|
...entry[CLIENT_STATIC_FILES_RUNTIME_MAIN]
|
2018-07-24 09:24:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
delete entry['main.js']
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
return webpackConfig
|
2016-10-14 15:05:08 +00:00
|
|
|
}
|