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

Don't delete .next folder before a replacement is built #1139

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ npm-debug.log

# other
.next
.next-*

# coverage
.nyc_output
Expand All @@ -17,4 +18,4 @@ coverage
# editors
.idea/*
*.iml
*.sublime-*
*.sublime-*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed this. I don't think we should have an editors section. gitignore is for artifacts of the project itself, not user systems

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed it myself, sorry. But I don't think I added that line? Must have been EOL.

4 changes: 2 additions & 2 deletions server/build/clean.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path'
import del from 'del'

export default function clean (dir) {
return del(resolve(dir, '.next'))
export default function clean (dir, folderName = '.next') {
return del(resolve(dir, folderName))
}
4 changes: 2 additions & 2 deletions server/build/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import path from 'path'
import zlib from 'zlib'
import glob from 'glob-promise'

export default async function gzipAssets (dir) {
const nextDir = path.resolve(dir, '.next')
export default async function gzipAssets (dir, buildFolder = '.next') {
const nextDir = path.resolve(dir, buildFolder)

const coreAssets = [
path.join(nextDir, 'commons.js'),
Expand Down
19 changes: 11 additions & 8 deletions server/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import path from 'path'
import webpack from './webpack'
import clean from './clean'
import gzipAssets from './gzip'
import replaceCurrentBuild from './replace'

export default async function build (dir) {
const [compiler] = await Promise.all([
webpack(dir),
clean(dir)
])
const distFolder = '.next'
const buildFolder = `.next-${uuid.v4()}`
const compiler = await webpack(dir, buildFolder)

await runCompiler(compiler)
await gzipAssets(dir)
await writeBuildId(dir)
const oldFolder = await replaceCurrentBuild(dir, buildFolder, distFolder)
await gzipAssets(dir, distFolder)
await writeBuildId(dir, distFolder)

clean(dir, oldFolder)
}

function runCompiler (compiler) {
Expand All @@ -34,8 +37,8 @@ function runCompiler (compiler) {
})
}

async function writeBuildId (dir) {
const buildIdPath = path.resolve(dir, '.next', 'BUILD_ID')
async function writeBuildId (dir, distFolder) {
const buildIdPath = path.resolve(dir, distFolder, 'BUILD_ID')
const buildId = uuid.v4()
await fs.writeFile(buildIdPath, buildId, 'utf8')
}
18 changes: 18 additions & 0 deletions server/build/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs'
import path from 'path'
import uuid from 'uuid'

export default function replaceCurrentBuild (dir, buildFolder, distFolder) {
const distDir = path.resolve(dir, distFolder)
const buildDir = path.resolve(dir, buildFolder)
const oldDir = path.resolve(dir, `.next-${uuid.v4()}`)

return new Promise((resolve, reject) => {
fs.rename(distDir, oldDir, () => {
fs.rename(buildDir, distDir, (err) => {
if (err) return reject(err)
resolve(oldDir)
})
})
})
}
4 changes: 2 additions & 2 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const interpolateNames = new Map(defaultPages.map((p) => {
return [join(nextPagesDir, p), `dist/pages/${p}`]
}))

export default async function createCompiler (dir, { dev = false, quiet = false } = {}) {
export default async function createCompiler (dir, buildFolder, { dev = false, quiet = false } = {}) {
dir = resolve(dir)
const config = getConfig(dir)
const defaultEntries = dev
Expand Down Expand Up @@ -228,7 +228,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
context: dir,
entry,
output: {
path: join(dir, '.next'),
path: join(dir, buildFolder || '.next'),
filename: '[name]',
libraryTarget: 'commonjs2',
publicPath: '/_webpack/',
Expand Down
2 changes: 1 addition & 1 deletion server/hot-reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class HotReloader {

async start () {
const [compiler] = await Promise.all([
webpack(this.dir, { dev: true, quiet: this.quiet }),
webpack(this.dir, null, { dev: true, quiet: this.quiet }),
clean(this.dir)
])

Expand Down