diff --git a/packages/nuxi/package.json b/packages/nuxi/package.json index c361065438..56bb30acf5 100644 --- a/packages/nuxi/package.json +++ b/packages/nuxi/package.json @@ -27,6 +27,7 @@ "@nuxt/kit": "3.0.0", "@types/clear": "^0", "@types/mri": "^1.1.1", + "@types/rimraf": "^3", "chokidar": "^3.5.2", "clear": "^0.1.0", "clipboardy": "^3.0.0", @@ -42,6 +43,7 @@ "p-debounce": "^4.0.0", "pathe": "^0.2.0", "pkg-types": "^0.2.1", + "rimraf": "^3.0.2", "scule": "^0.2.1", "superb": "^4.0.0", "tiged": "^2.12.0", diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index 084eb637fb..b142869b56 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -3,6 +3,7 @@ import consola from 'consola' import { writeTypes } from '../utils/prepare' import { loadKit } from '../utils/kit' +import { clearDir } from '../utils/fs' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -19,6 +20,8 @@ export default defineNuxtCommand({ const nuxt = await loadNuxt({ rootDir }) + await clearDir(nuxt.options.buildDir) + await writeTypes(nuxt) nuxt.hook('error', (err) => { diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 662c0fc9ee..00df9bfa6c 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -7,6 +7,7 @@ import { createServer, createLoadingHandler } from '../utils/server' import { showBanner } from '../utils/banner' import { writeTypes } from '../utils/prepare' import { loadKit } from '../utils/kit' +import { clearDir } from '../utils/fs' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -51,6 +52,7 @@ export default defineNuxtCommand({ await currentNuxt.close() } const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false }) + await clearDir(newNuxt.options.buildDir) prepare(newNuxt) currentNuxt = newNuxt await currentNuxt.ready() diff --git a/packages/nuxi/src/utils/fs.ts b/packages/nuxi/src/utils/fs.ts index e646b35211..9597b1911b 100644 --- a/packages/nuxi/src/utils/fs.ts +++ b/packages/nuxi/src/utils/fs.ts @@ -1,4 +1,6 @@ import { promises as fsp } from 'fs' +import { promisify } from 'util' +import rimraf from 'rimraf' // Check if a file exists export async function exists (path: string) { @@ -9,3 +11,8 @@ export async function exists (path: string) { return false } } + +export async function clearDir (path: string) { + await promisify(rimraf)(path) + await fsp.mkdir(path, { recursive: true }) +} diff --git a/packages/nuxi/src/utils/prepare.ts b/packages/nuxi/src/utils/prepare.ts index 67e7b6a122..9aa0f317fb 100644 --- a/packages/nuxi/src/utils/prepare.ts +++ b/packages/nuxi/src/utils/prepare.ts @@ -76,9 +76,16 @@ export const writeTypes = async (nuxt: Nuxt) => { consola.success('Generated', cyan(relative(process.cwd(), declarationPath))) - const tsConfigPath = resolve(nuxt.options.buildDir, 'tsconfig.json') - await fsp.mkdir(nuxt.options.buildDir, { recursive: true }) - await fsp.writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2)) + async function writeFile () { + const tsConfigPath = resolve(nuxt.options.buildDir, 'tsconfig.json') + await fsp.writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2)) + } + + // This is needed for Nuxt 2 which clears the build directory again before building + // https://github.com/nuxt/nuxt.js/blob/dev/packages/builder/src/builder.js#L144 + nuxt.hook('builder:prepared', writeFile) + + await writeFile() } function renderAttrs (obj: Record) { diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 12c9314ca9..6405714e85 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -29,7 +29,6 @@ "chokidar": "^3.5.2", "consola": "^2.15.3", "defu": "^5.0.0", - "fs-extra": "^10.0.0", "globby": "^11.0.4", "hash-sum": "^2.0.0", "hookable": "^5.0.0", @@ -45,7 +44,6 @@ "vue-router": "^4.0.12" }, "devDependencies": { - "@types/fs-extra": "^9.0.13", "@types/hash-sum": "^1.0.0", "unbuild": "latest", "vue-meta": "next" diff --git a/packages/nuxt3/src/core/builder.ts b/packages/nuxt3/src/core/builder.ts index b1e47bf921..5d5cac6cb1 100644 --- a/packages/nuxt3/src/core/builder.ts +++ b/packages/nuxt3/src/core/builder.ts @@ -1,12 +1,8 @@ import chokidar from 'chokidar' import { Nuxt } from '@nuxt/kit' -import fse from 'fs-extra' import { createApp, generateApp } from './app' export async function build (nuxt: Nuxt) { - // Clear buildDir once - await fse.emptyDir(nuxt.options.buildDir) - const app = createApp(nuxt) await generateApp(nuxt, app) diff --git a/yarn.lock b/yarn.lock index 266b12df90..6cc80d85c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3386,6 +3386,16 @@ __metadata: languageName: node linkType: hard +"@types/glob@npm:*": + version: 7.1.4 + resolution: "@types/glob@npm:7.1.4" + dependencies: + "@types/minimatch": "*" + "@types/node": "*" + checksum: 6911a956448f5eddea1e4371f814bf92072e8ceedba83de6ce2a6745938a6f0327376e1c0072fa0d7b3b73d84e255aafda53c1dff148225cfe542a8cc5d54b02 + languageName: node + linkType: hard + "@types/hash-sum@npm:^1.0.0": version: 1.0.0 resolution: "@types/hash-sum@npm:1.0.0" @@ -3457,7 +3467,7 @@ __metadata: languageName: node linkType: hard -"@types/minimatch@npm:^3.0.3": +"@types/minimatch@npm:*, @types/minimatch@npm:^3.0.3": version: 3.0.5 resolution: "@types/minimatch@npm:3.0.5" checksum: c41d136f67231c3131cf1d4ca0b06687f4a322918a3a5adddc87ce90ed9dbd175a3610adee36b106ae68c0b92c637c35e02b58c8a56c424f71d30993ea220b92 @@ -3566,6 +3576,16 @@ __metadata: languageName: node linkType: hard +"@types/rimraf@npm:^3": + version: 3.0.2 + resolution: "@types/rimraf@npm:3.0.2" + dependencies: + "@types/glob": "*" + "@types/node": "*" + checksum: b47fa302f46434cba704d20465861ad250df79467d3d289f9d6490d3aeeb41e8cb32dd80bd1a8fd833d1e185ac719fbf9be12e05ad9ce9be094d8ee8f1405347 + languageName: node + linkType: hard + "@types/semver@npm:^7": version: 7.3.8 resolution: "@types/semver@npm:7.3.8" @@ -13685,6 +13705,7 @@ fsevents@~2.3.2: "@nuxt/kit": 3.0.0 "@types/clear": ^0 "@types/mri": ^1.1.1 + "@types/rimraf": ^3 chokidar: ^3.5.2 clear: ^0.1.0 clipboardy: ^3.0.0 @@ -13701,6 +13722,7 @@ fsevents@~2.3.2: p-debounce: ^4.0.0 pathe: ^0.2.0 pkg-types: ^0.2.1 + rimraf: ^3.0.2 scule: ^0.2.1 superb: ^4.0.0 tiged: ^2.12.0 @@ -13781,7 +13803,6 @@ fsevents@~2.3.2: "@nuxt/nitro": 3.0.0 "@nuxt/vite-builder": 3.0.0 "@nuxt/webpack-builder": 3.0.0 - "@types/fs-extra": ^9.0.13 "@types/hash-sum": ^1.0.0 "@vue/reactivity": 3.2.20 "@vue/shared": 3.2.20 @@ -13789,7 +13810,6 @@ fsevents@~2.3.2: chokidar: ^3.5.2 consola: ^2.15.3 defu: ^5.0.0 - fs-extra: ^10.0.0 globby: ^11.0.4 hash-sum: ^2.0.0 hookable: ^5.0.0