build on tmp dir (#1150)

This commit is contained in:
Naoyuki Kanezawa 2017-02-15 22:03:33 +09:00 committed by Guillermo Rauch
parent ff547752f0
commit 141ab99888
7 changed files with 39 additions and 35 deletions

1
.gitignore vendored
View file

@ -9,7 +9,6 @@ npm-debug.log
# other # other
.next .next
.next-*
# coverage # coverage
.nyc_output .nyc_output

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, folderName = '.next') { export default function clean (dir) {
return del(resolve(dir, folderName)) return del(resolve(dir, '.next'))
} }

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, buildFolder = '.next') { export default async function gzipAssets (dir) {
const nextDir = path.resolve(dir, buildFolder) const nextDir = path.resolve(dir, '.next')
const coreAssets = [ const coreAssets = [
path.join(nextDir, 'commons.js'), path.join(nextDir, 'commons.js'),

View file

@ -1,22 +1,29 @@
import { tmpdir } from 'os'
import { join } from 'path'
import fs from 'mz/fs' import fs from 'mz/fs'
import uuid from 'uuid' import uuid from 'uuid'
import path from 'path' import del from 'del'
import webpack from './webpack' import webpack from './webpack'
import clean from './clean'
import gzipAssets from './gzip' import gzipAssets from './gzip'
import replaceCurrentBuild from './replace' import replaceCurrentBuild from './replace'
export default async function build (dir) { export default async function build (dir) {
const distFolder = '.next' const buildDir = join(tmpdir(), uuid.v4())
const buildFolder = `.next-${uuid.v4()}` const compiler = await webpack(dir, { buildDir })
const compiler = await webpack(dir, buildFolder)
await runCompiler(compiler) try {
const oldFolder = await replaceCurrentBuild(dir, buildFolder, distFolder) await runCompiler(compiler)
await gzipAssets(dir, distFolder) await gzipAssets(buildDir)
await writeBuildId(dir, distFolder) await writeBuildId(buildDir)
} catch (err) {
console.error(`> Failed to build on ${buildDir}`)
throw err
}
clean(dir, oldFolder) await replaceCurrentBuild(dir, buildDir)
// no need to wait
del(buildDir)
} }
function runCompiler (compiler) { function runCompiler (compiler) {
@ -37,8 +44,8 @@ function runCompiler (compiler) {
}) })
} }
async function writeBuildId (dir, distFolder) { async function writeBuildId (dir) {
const buildIdPath = path.resolve(dir, distFolder, 'BUILD_ID') const buildIdPath = join(dir, '.next', 'BUILD_ID')
const buildId = uuid.v4() const buildId = uuid.v4()
await fs.writeFile(buildIdPath, buildId, 'utf8') await fs.writeFile(buildIdPath, buildId, 'utf8')
} }

View file

@ -1,18 +1,16 @@
import fs from 'fs' import { rename } from 'mz/fs'
import path from 'path' import { join } from 'path'
import uuid from 'uuid'
export default function replaceCurrentBuild (dir, buildFolder, distFolder) { export default async function replaceCurrentBuild (dir, buildDir) {
const distDir = path.resolve(dir, distFolder) const _dir = join(dir, '.next')
const buildDir = path.resolve(dir, buildFolder) const _buildDir = join(buildDir, '.next')
const oldDir = path.resolve(dir, `.next-${uuid.v4()}`) const oldDir = join(buildDir, '.next.old')
return new Promise((resolve, reject) => { try {
fs.rename(distDir, oldDir, () => { await rename(_dir, oldDir)
fs.rename(buildDir, distDir, (err) => { } catch (err) {
if (err) return reject(err) if (err.code !== 'ENOENT') throw err
resolve(oldDir) }
}) await rename(_buildDir, _dir)
}) return 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, buildFolder, { dev = false, quiet = false } = {}) { export default async function createCompiler (dir, { dev = false, quiet = false, buildDir } = {}) {
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, buildFolder, { dev = false, q
context: dir, context: dir,
entry, entry,
output: { output: {
path: join(dir, buildFolder || '.next'), path: join(buildDir || dir, '.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, null, { dev: true, quiet: this.quiet }), webpack(this.dir, { dev: true, quiet: this.quiet }),
clean(this.dir) clean(this.dir)
]) ])