diff --git a/package.json b/package.json index e55ad9dab..92e65619b 100755 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "ipx": "^0.6.0", "is-https": "^3.0.2", "node-fetch": "^2.6.1", + "p-limit": "^3.1.0", "requrl": "^3.0.2", "ufo": "^0.6.11", "upath": "^2.0.1" diff --git a/src/generate.ts b/src/generate.ts index 7dfefb8ed..208a3cc61 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -6,6 +6,7 @@ import { mkdirp } from 'fs-extra' import { dirname, join, relative, resolve, extname } from 'upath' import fetch from 'node-fetch' import { joinURL, hasProtocol, parseURL } from 'ufo' +import pLimit from 'p-limit' import { ModuleOptions, MapToStatic, ResolvedImage } from './types' import { hash, logger } from './utils' @@ -27,15 +28,16 @@ export function setupStaticGeneration (nuxt: any, options: ModuleOptions) { nuxt.hook('generate:done', async () => { const { dir: generateDir } = nuxt.options.generate + const limit = pLimit(8) const downloads = Object.entries(staticImages).map(([url, name]) => { if (!hasProtocol(url)) { url = joinURL(options.internalUrl, url) } - return downloadImage({ + return limit(() => downloadImage({ url, name, outDir: resolve(generateDir, '_nuxt/image' /* TODO: staticImagesBase */) - }) + })) }) await Promise.all(downloads) }) diff --git a/yarn.lock b/yarn.lock index 7bdb56d97..2fae348a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8752,7 +8752,7 @@ p-limit@^2.0.0, p-limit@^2.2.0: dependencies: p-try "^2.0.0" -p-limit@^3.0.2: +p-limit@^3.0.2, p-limit@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b" integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ== @@ -12337,8 +12337,10 @@ watchpack@^1.7.4: resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.7.5.tgz#1267e6c55e0b9b5be44c2023aed5437a2c26c453" integrity sha512-9P3MWk6SrKjHsGkLT2KHXdQ/9SNkyoJbabxnKOoJepsvJjJG8uYTR3yTPxPQvNDI3w4Nz1xnE0TLHK4RIVe/MQ== dependencies: + chokidar "^3.4.1" graceful-fs "^4.1.2" neo-async "^2.5.0" + watchpack-chokidar2 "^2.0.1" optionalDependencies: chokidar "^3.4.1" watchpack-chokidar2 "^2.0.1"