1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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

This commit is contained in:
Thomas Lindstrøm 2017-02-15 00:14:30 +01:00 committed by Guillermo Rauch
parent 3766cd0013
commit acc1983f80
7 changed files with 38 additions and 16 deletions

3
.gitignore vendored
View file

@ -9,6 +9,7 @@ npm-debug.log
# other # other
.next .next
.next-*
# coverage # coverage
.nyc_output .nyc_output
@ -17,4 +18,4 @@ coverage
# editors # editors
.idea/* .idea/*
*.iml *.iml
*.sublime-* *.sublime-*

View file

@ -1,6 +1,6 @@
import { resolve } from 'path' import { resolve } from 'path'
import del from 'del' import del from 'del'
export default function clean (dir) { export default function clean (dir, folderName = '.next') {
return del(resolve(dir, '.next')) return del(resolve(dir, folderName))
} }

View file

@ -3,8 +3,8 @@ import path from 'path'
import zlib from 'zlib' import zlib from 'zlib'
import glob from 'glob-promise' import glob from 'glob-promise'
export default async function gzipAssets (dir) { export default async function gzipAssets (dir, buildFolder = '.next') {
const nextDir = path.resolve(dir, '.next') const nextDir = path.resolve(dir, buildFolder)
const coreAssets = [ const coreAssets = [
path.join(nextDir, 'commons.js'), path.join(nextDir, 'commons.js'),

View file

@ -4,16 +4,19 @@ import path from 'path'
import webpack from './webpack' import webpack from './webpack'
import clean from './clean' import clean from './clean'
import gzipAssets from './gzip' import gzipAssets from './gzip'
import replaceCurrentBuild from './replace'
export default async function build (dir) { export default async function build (dir) {
const [compiler] = await Promise.all([ const distFolder = '.next'
webpack(dir), const buildFolder = `.next-${uuid.v4()}`
clean(dir) const compiler = await webpack(dir, buildFolder)
])
await runCompiler(compiler) await runCompiler(compiler)
await gzipAssets(dir) const oldFolder = await replaceCurrentBuild(dir, buildFolder, distFolder)
await writeBuildId(dir) await gzipAssets(dir, distFolder)
await writeBuildId(dir, distFolder)
clean(dir, oldFolder)
} }
function runCompiler (compiler) { function runCompiler (compiler) {
@ -34,8 +37,8 @@ function runCompiler (compiler) {
}) })
} }
async function writeBuildId (dir) { async function writeBuildId (dir, distFolder) {
const buildIdPath = path.resolve(dir, '.next', 'BUILD_ID') const buildIdPath = path.resolve(dir, distFolder, 'BUILD_ID')
const buildId = uuid.v4() const buildId = uuid.v4()
await fs.writeFile(buildIdPath, buildId, 'utf8') await fs.writeFile(buildIdPath, buildId, 'utf8')
} }

18
server/build/replace.js Normal file
View file

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

View file

@ -23,7 +23,7 @@ const interpolateNames = new Map(defaultPages.map((p) => {
return [join(nextPagesDir, p), `dist/pages/${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) dir = resolve(dir)
const config = getConfig(dir) const config = getConfig(dir)
const defaultEntries = dev const defaultEntries = dev
@ -228,7 +228,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
context: dir, context: dir,
entry, entry,
output: { output: {
path: join(dir, '.next'), path: join(dir, buildFolder || '.next'),
filename: '[name]', filename: '[name]',
libraryTarget: 'commonjs2', libraryTarget: 'commonjs2',
publicPath: '/_webpack/', publicPath: '/_webpack/',

View file

@ -35,7 +35,7 @@ export default class HotReloader {
async start () { async start () {
const [compiler] = await Promise.all([ 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) clean(this.dir)
]) ])