-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cna): make
create-next-app
even smaller (#53241)
The PR follows #53146 and #53115. The PR does 3 things: - Replaces a [very heavy dependency `cpy`](#53146 (comment)) with a more lightweight copy helper. - The `fs.cp(src, dest, {recursive: true})` API is not used, as it is still experimental: <img width="1630" alt="image" src="https://github.com/vercel/next.js/assets/40715044/c61a454a-3a96-4658-a389-fbb68c241f18"> - Update `cross-spawn` to the latest version `7.0.3` - The only breaking change introduced in `cross-spawn@7.x` is dropping Node.js 8 supports, which allows `cross-spawn` to drop a dependency. Since `create-next-app` requires Node.js 16.8.0, I assume bumping `cross-spawn` would be safe. - Update `fast-glob` to the latest version `3.3.1` to remove more KiBs (pointed out by @imranbarbhuiya) - The breaking change introduced in `fast-glob@3.x` is dropping Node.js 8 supports and some options changes. Together the PR removes another 202 KiB from the `create-next-app/dist/index.js`. The size of `create-next-app/dist/index.js` is now 616 KiB. <img width="583" alt="image" src="https://github.com/vercel/next.js/assets/40715044/4deb5e36-a63b-4501-b67c-29ea06e30578">
- Loading branch information
Showing
5 changed files
with
152 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { async as glob } from 'fast-glob' | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
interface CopyOption { | ||
cwd?: string | ||
rename?: (basename: string) => string | ||
parents?: boolean | ||
} | ||
|
||
const identity = (x: string) => x | ||
|
||
export const copy = async ( | ||
src: string | string[], | ||
dest: string, | ||
{ cwd, rename = identity, parents = true }: CopyOption = {} | ||
) => { | ||
const source = typeof src === 'string' ? [src] : src | ||
|
||
if (source.length === 0 || !dest) { | ||
throw new TypeError('`src` and `dest` are required') | ||
} | ||
|
||
const sourceFiles = await glob(source, { | ||
cwd, | ||
dot: true, | ||
absolute: false, | ||
stats: false, | ||
}) | ||
|
||
const destRelativeToCwd = cwd ? path.resolve(cwd, dest) : dest | ||
|
||
return Promise.all( | ||
sourceFiles.map(async (p) => { | ||
const dirname = path.dirname(p) | ||
const basename = rename(path.basename(p)) | ||
|
||
const from = cwd ? path.resolve(cwd, p) : p | ||
const to = parents | ||
? path.join(destRelativeToCwd, dirname, basename) | ||
: path.join(destRelativeToCwd, basename) | ||
|
||
// Ensure the destination directory exists | ||
await fs.promises.mkdir(path.dirname(to), { recursive: true }) | ||
|
||
return fs.promises.copyFile(from, to) | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.