Skip to content

Commit 9347c8b

Browse files
alexnewmannnarunoda
authored andcommitted
Specify a different build directory for vercel#1513 (vercel#1599)
* Update references to `.next` * Remove console logs and extraneous semi colons * Remove lint errors * Update references to .next and update docs * Update options from nested to flat with `distDir` * Add integration tests, and update `.gitignore` * Rename integration folder to dist-dir to match standards
1 parent 12a7610 commit 9347c8b

File tree

15 files changed

+128
-34
lines changed

15 files changed

+128
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ npm-debug.log
1111
# coverage
1212
.nyc_output
1313
coverage
14+
15+
.DS_Store

bin/next-build

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ if (argv.help) {
2222
Usage
2323
$ next build <dir>
2424
25-
<dir> represents where the compiled .next folder should go.
26-
If no directory is provided, .next will be created in the current directory
25+
<dir> represents where the compiled dist folder should go.
26+
If no directory is provided, the dist folder will be created in the current directory.
27+
You can set a custom folder in config https://github.com/zeit/next.js#custom-configuration, otherwise it will be created inside '.next'
2728
`)
2829
process.exit(0)
2930
}

bin/next-dev

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ if (argv.help) {
2929
Usage
3030
$ next dev <dir> -p <port number>
3131
32-
<dir> represents where the compiled .next folder should go.
33-
If no directory is provided, .next will be created in the current directory
32+
<dir> represents where the compiled folder should go.
33+
If no directory is provided, the folder will be created in the current directory.
34+
You can set a custom folder in config https://github.com/zeit/next.js#custom-configuration.
3435
3536
Options
3637
--port, -p A port number on which to start the application

bin/next-start

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolve } from 'path'
44
import parseArgs from 'minimist'
55
import Server from '../server'
66
import { existsSync } from 'fs'
7+
import getConfig from '../server/config'
78

89
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
910

@@ -32,9 +33,10 @@ if (argv.help) {
3233
Usage
3334
$ next start <dir> -p <port>
3435
35-
<dir> is the directory that contains the compiled .next folder
36+
<dir> is the directory that contains the compiled dist folder
3637
created by running \`next build\`.
3738
If no directory is provided, the current directory will be assumed.
39+
You can set a custom dist folder in config https://github.com/zeit/next.js#custom-configuration
3840
3941
Options
4042
--port, -p A port number on which to start the application
@@ -45,11 +47,12 @@ if (argv.help) {
4547
}
4648

4749
const dir = resolve(argv._[0] || '.')
50+
const dist = getConfig(dir).distDir
4851

4952
const srv = new Server({ dir })
5053

51-
if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) {
52-
console.error(`> Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.`)
54+
if (!existsSync(resolve(dir, dist, 'BUILD_ID'))) {
55+
console.error(`> Could not find a valid build in the '${dist}' directory! Try building your app with 'next build' before starting the server.`)
5356
process.exit(1)
5457
}
5558

readme.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,17 @@ module.exports = {
644644
}
645645
```
646646

647+
#### Setting a custom build directory
648+
649+
You can specify a name to use for a custom build directory. For example, the following config will create a `build` folder instead of a `.next` folder. If no configuration is specified then next will create a `.next` folder.
650+
651+
```javascript
652+
// next.config.js
653+
module.exports = {
654+
distDir: 'build'
655+
}
656+
```
657+
647658
### Customizing webpack config
648659

649660
In order to extend our usage of `webpack`, you can define a function that extends its config via `next.config.js`.
@@ -718,7 +729,7 @@ Then run `now` and enjoy!
718729

719730
Next.js can be deployed to other hosting solutions too. Please have a look at the ['Deployment'](https://github.com/zeit/next.js/wiki/Deployment) section of the wiki.
720731

721-
Note: we recommend putting `.next` in `.npmignore` or `.gitignore`. Otherwise, use `files` or `now.files` to opt-into a whitelist of files you want to deploy (and obviously exclude `.next`)
732+
Note: we recommend putting `.next`, or your custom dist folder (Please have a look at ['Custom Config'](You can set a custom folder in config https://github.com/zeit/next.js#custom-configuration.)), in `.npmignore` or `.gitignore`. Otherwise, use `files` or `now.files` to opt-into a whitelist of files you want to deploy (and obviously exclude `.next` or your custom dist folder)
722733

723734
## FAQ
724735

server/build/clean.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { resolve } from 'path'
22
import del from 'del'
3+
import getConfig from '../config'
34

45
export default function clean (dir) {
5-
return del(resolve(dir, '.next'))
6+
const dist = getConfig(dir).distDir
7+
return del(resolve(dir, dist))
68
}

server/build/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { tmpdir } from 'os'
22
import { join } from 'path'
3+
import getConfig from '../config'
34
import fs from 'mz/fs'
45
import uuid from 'uuid'
56
import del from 'del'
@@ -13,8 +14,10 @@ export default async function build (dir) {
1314

1415
try {
1516
await runCompiler(compiler)
16-
await writeBuildStats(buildDir)
17-
await writeBuildId(buildDir)
17+
18+
// Pass in both the buildDir and the dir to retrieve config
19+
await writeBuildStats(buildDir, dir)
20+
await writeBuildId(buildDir, dir)
1821
} catch (err) {
1922
console.error(`> Failed to build on ${buildDir}`)
2023
throw err
@@ -45,22 +48,24 @@ function runCompiler (compiler) {
4548
})
4649
}
4750

48-
async function writeBuildStats (dir) {
51+
async function writeBuildStats (buildDir, dir) {
52+
const dist = getConfig(dir).distDir
4953
// Here we can't use hashes in webpack chunks.
5054
// That's because the "app.js" is not tied to a chunk.
5155
// It's created by merging a few assets. (commons.js and main.js)
5256
// So, we need to generate the hash ourself.
5357
const assetHashMap = {
5458
'app.js': {
55-
hash: await md5File(join(dir, '.next', 'app.js'))
59+
hash: await md5File(join(buildDir, dist, 'app.js'))
5660
}
5761
}
58-
const buildStatsPath = join(dir, '.next', 'build-stats.json')
62+
const buildStatsPath = join(buildDir, dist, 'build-stats.json')
5963
await fs.writeFile(buildStatsPath, JSON.stringify(assetHashMap), 'utf8')
6064
}
6165

62-
async function writeBuildId (dir) {
63-
const buildIdPath = join(dir, '.next', 'BUILD_ID')
66+
async function writeBuildId (buildDir, dir) {
67+
const dist = getConfig(dir).distDir
68+
const buildIdPath = join(buildDir, dist, 'BUILD_ID')
6469
const buildId = uuid.v4()
6570
await fs.writeFile(buildIdPath, buildId, 'utf8')
6671
}

server/build/replace.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import mv from 'mv'
22
import { join } from 'path'
3+
import getConfig from '../config'
34

45
export default async function replaceCurrentBuild (dir, buildDir) {
5-
const _dir = join(dir, '.next')
6-
const _buildDir = join(buildDir, '.next')
7-
const oldDir = join(buildDir, '.next.old')
6+
const dist = getConfig(dir).distDir
7+
const buildDist = getConfig(buildDir).distDir
8+
const _dir = join(dir, dist)
9+
const _buildDir = join(buildDir, dist)
10+
const oldDir = join(buildDir, `${buildDist}.old`)
811

912
try {
1013
await move(_dir, oldDir)

server/build/webpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
265265
context: dir,
266266
entry,
267267
output: {
268-
path: join(buildDir || dir, '.next'),
268+
path: join(buildDir || dir, config.distDir),
269269
filename: '[name]',
270270
libraryTarget: 'commonjs2',
271271
publicPath: '/_webpack/',

server/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const cache = new Map()
55

66
const defaultConfig = {
77
webpack: null,
8-
poweredByHeader: true
8+
poweredByHeader: true,
9+
distDir: '.next'
910
}
1011

1112
export default function getConfig (dir) {

0 commit comments

Comments
 (0)