Skip to content

Commit 0345d32

Browse files
committed
perf: reduce bundle size
1 parent 38edd9f commit 0345d32

File tree

6 files changed

+69
-69
lines changed

6 files changed

+69
-69
lines changed

src/core/command/create-react-next/copyTemplate.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from 'fs-extra'
2-
import path from 'node:path'
1+
import { copy as fsCopy, move } from 'fs-extra'
2+
import { resolve } from 'node:path'
33
import options from '../../../core/utils/react/options'
44
import { ejsRender } from '../../../utils/ejsRender'
55
import chalk from "chalk"
@@ -18,13 +18,13 @@ async function copyTemplate() {
1818

1919
const language = options.useTypeScript ? 'react-ts' : 'react-js';
2020

21-
options.src = path.resolve(__dirname, `../template/${language}`);
21+
options.src = resolve(__dirname, `../template/${language}`);
2222

23-
const dest = options.name && path.resolve(process.cwd(), options.name)
23+
const dest = options.name && resolve(process.cwd(), options.name)
2424

2525
options.dest = dest
2626

27-
const templatePath = path.resolve(
27+
const templatePath = resolve(
2828
__dirname,
2929
`../../../../template/${language}`
3030
);
@@ -33,19 +33,19 @@ async function copyTemplate() {
3333
const filterFileFn = getFilterFile()
3434

3535
async function copy() {
36-
const targetDirectory = path.resolve(__dirname, '../');
36+
const targetDirectory = resolve(__dirname, '../');
3737
if(!dest) {
3838
return;
3939
};
40-
await fs.copy(`${targetDirectory}/template/${language}`, dest)
40+
await fsCopy(`${targetDirectory}/template/${language}`, dest)
4141
}
4242
await copy();
4343

4444
filterFileFn && await filterFileFn();
4545

46-
options.dest && await fs.move(
47-
path.resolve(options.dest, '.gitignore.ejs'),
48-
path.resolve(options.dest, '.gitignore'),
46+
options.dest && await move(
47+
resolve(options.dest, '.gitignore.ejs'),
48+
resolve(options.dest, '.gitignore'),
4949
{ overwrite: true }
5050
);
5151

src/filter/filterFiles.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
11
import options from '../core/utils/react/options';
2-
import fs from 'fs-extra';
2+
import { remove } from 'fs-extra';
33

44
export function getFilterFile() {
55
async function reactFilterFileActions() {
66
if (!options.useRouter) {
7-
fs.remove(`${options.dest}/src/pages`)
7+
remove(`${options.dest}/src/pages`)
88
}
99

1010
if (!options.useTailwind) {
11-
fs.remove(`${options.dest}/tailwind.config.js`);
12-
fs.remove(`${options.dest}/postcss.config.js`);
13-
fs.remove(`${options.dest}/src/assets/css/tailwind.css`);
11+
remove(`${options.dest}/tailwind.config.js`);
12+
remove(`${options.dest}/postcss.config.js`);
13+
remove(`${options.dest}/src/assets/css/tailwind.css`);
1414
};
1515

1616
if (options.useTailwind) {
17-
fs.remove(`${options.dest}/src/assets/css/base.css`);
17+
remove(`${options.dest}/src/assets/css/base.css`);
1818
}
1919

2020
if (options.deploy === 'none') {
21-
fs.remove(`${options.dest}/netlify.toml`);
22-
fs.remove(`${options.dest}/vercel.json`);
21+
remove(`${options.dest}/netlify.toml`);
22+
remove(`${options.dest}/vercel.json`);
2323
}
2424

2525
if (options.deploy === "netlify") {
26-
fs.remove(`${options.dest}/vercel.json`);
26+
remove(`${options.dest}/vercel.json`);
2727
}
2828

2929
if (options.deploy === "vercel") {
30-
fs.remove(`${options.dest}/netlify.toml`);
30+
remove(`${options.dest}/netlify.toml`);
3131
}
3232

3333

3434
if (options.useTailwind === false) {
35-
fs.remove(`${options.dest}/src/assets/css/tailwind.css`);
35+
remove(`${options.dest}/src/assets/css/tailwind.css`);
3636
}
3737

3838
if (options.useJavaScript) {
39-
fs.remove(`${options.dest}/src/main.tsx`);
39+
remove(`${options.dest}/src/main.tsx`);
4040
}
4141

4242
if (!options.useVitest) {
43-
fs.remove(`${options.dest}/vitest.config.ts`);
44-
fs.remove(`${options.dest}/vitest.config.js`);
45-
fs.remove(`${options.dest}/tests`);
43+
remove(`${options.dest}/vitest.config.ts`);
44+
remove(`${options.dest}/vitest.config.js`);
45+
remove(`${options.dest}/tests`);
4646
}
4747

4848
if (options.stateManagement === 'none') {
49-
fs.remove(`${options.dest}/src/store`)
50-
fs.remove(`${options.dest}/src/app`)
51-
fs.remove(`${options.dest}/src/features`)
49+
remove(`${options.dest}/src/store`)
50+
remove(`${options.dest}/src/app`)
51+
remove(`${options.dest}/src/features`)
5252
}
5353

5454
if (options.stateManagement === 'jotai') {
55-
fs.remove(`${options.dest}/src/app`)
56-
fs.remove(`${options.dest}/src/store`)
57-
fs.remove(`${options.dest}/src/features`)
55+
remove(`${options.dest}/src/app`)
56+
remove(`${options.dest}/src/store`)
57+
remove(`${options.dest}/src/features`)
5858
}
5959

6060

6161
if (options.stateManagement === 'zustand') {
62-
fs.remove(`${options.dest}/src/app`)
63-
fs.remove(`${options.dest}/src/store/appStore.ts`)
64-
fs.remove(`${options.dest}/src/store/appStore.js`)
65-
fs.remove(`${options.dest}/src/features`)
62+
remove(`${options.dest}/src/app`)
63+
remove(`${options.dest}/src/store/appStore.ts`)
64+
remove(`${options.dest}/src/store/appStore.js`)
65+
remove(`${options.dest}/src/features`)
6666
}
6767

6868
if (options.stateManagement === 'redux') {
69-
fs.remove(`${options.dest}/src/store/store.ts`)
70-
fs.remove(`${options.dest}/src/store/store.js`)
69+
remove(`${options.dest}/src/store/store.ts`)
70+
remove(`${options.dest}/src/store/store.js`)
7171
}
7272

7373
if (!options.useEslint) {
74-
fs.remove(`${options.dest}/.eslintrc.cjs`)
74+
remove(`${options.dest}/.eslintrc.cjs`)
7575
}
7676
return true
7777
}

src/utils/directoryTraverse.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as fs from 'fs'
2-
import * as path from 'path'
1+
import { readdirSync, lstatSync, existsSync } from 'fs'
2+
import { resolve } from 'path'
33

44
export function preOrderDirectoryTraverse(dir: string, dirCallback: Function, fileCallback: Function) {
5-
for (const filename of fs.readdirSync(dir)) {
6-
const fullpath = path.resolve(dir, filename)
7-
if (fs.lstatSync(fullpath).isDirectory()) {
5+
for (const filename of readdirSync(dir)) {
6+
const fullpath = resolve(dir, filename)
7+
if (lstatSync(fullpath).isDirectory()) {
88
dirCallback(fullpath)
99
// in case the dirCallback removes the directory entirely
10-
if (fs.existsSync(fullpath)) {
10+
if (existsSync(fullpath)) {
1111
preOrderDirectoryTraverse(fullpath, dirCallback, fileCallback)
1212
}
1313
continue
@@ -17,9 +17,9 @@ export function preOrderDirectoryTraverse(dir: string, dirCallback: Function, fi
1717
}
1818

1919
export function postOrderDirectoryTraverse(dir: string, dirCallback: Function, fileCallback: Function) {
20-
for (const filename of fs.readdirSync(dir)) {
21-
const fullpath = path.resolve(dir, filename)
22-
if (fs.lstatSync(fullpath).isDirectory()) {
20+
for (const filename of readdirSync(dir)) {
21+
const fullpath = resolve(dir, filename)
22+
if (lstatSync(fullpath).isDirectory()) {
2323
postOrderDirectoryTraverse(fullpath, dirCallback, fileCallback)
2424
dirCallback(fullpath)
2525
continue

src/utils/ejsRender.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ejs from "ejs";
2-
import fs from 'fs-extra'
3-
import path from "path";
1+
import { render } from "ejs";
2+
import { readFile, outputFile, remove } from 'fs-extra'
3+
import { resolve, extname, parse } from "path";
44
import { format as prettierFormatter } from "prettier/standalone"
55
import parserBabel from "prettier/parser-babel";
66
import parserEstree from "prettier/plugins/estree";
@@ -12,22 +12,22 @@ export async function ejsRender(filePath: string, name: string): Promise<void> {
1212
try {
1313
let prettierCode: string = '';
1414

15-
const file = path.parse(filePath);
15+
const file = parse(filePath);
1616

17-
const dest = path.resolve(process.cwd(), name)
17+
const dest = resolve(process.cwd(), name)
1818

19-
const readFilePath = path.resolve(dest, file.dir, `${file.name}.ejs`)
19+
const readFilePath = resolve(dest, file.dir, `${file.name}.ejs`)
2020

21-
const outputFilePath = path.resolve(dest, filePath)
21+
const outputFilePath = resolve(dest, filePath)
2222

23-
const templateCode = await fs.readFile(readFilePath)
23+
const templateCode = await readFile(readFilePath)
2424

25-
const code = ejs.render(templateCode.toString(), options);
25+
const code = render(templateCode.toString(), options);
2626

27-
const extname = path.extname(filePath).replace(/[.]/g, '')
27+
const extensionName = extname(filePath).replace(/[.]/g, '')
2828

2929
try {
30-
switch (extname) {
30+
switch (extensionName) {
3131
case 'ts':
3232
case 'tsx':
3333
case 'jsx':
@@ -62,8 +62,8 @@ export async function ejsRender(filePath: string, name: string): Promise<void> {
6262
} catch (err) {
6363
console.log(err)
6464
}
65-
await fs.outputFile(outputFilePath, prettierCode)
66-
await fs.remove(readFilePath)
65+
await outputFile(outputFilePath, prettierCode)
66+
await remove(readFilePath)
6767
} catch (error) {
6868
console.log(error)
6969
}

src/utils/emptyDir.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import * as fs from 'fs'
1+
import { existsSync, rmdirSync, unlinkSync } from 'fs'
22

33
import { postOrderDirectoryTraverse } from './directoryTraverse'
44

55
function emptyDir(dir: string) {
6-
if (!fs.existsSync(dir)) {
6+
if (!existsSync(dir)) {
77
return
88
}
99

1010
postOrderDirectoryTraverse(
1111
dir,
12-
(dir: string) => fs.rmdirSync(dir),
13-
(file: string) => fs.unlinkSync(file)
12+
(dir: string) => rmdirSync(dir),
13+
(file: string) => unlinkSync(file)
1414
)
1515
}
1616
export default emptyDir

src/utils/emptyDirName.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import fs from 'fs-extra'
2-
import path from 'path'
1+
import { existsSync, readdirSync } from 'fs-extra'
2+
import { resolve } from 'path'
33

44
export default function (name: string): boolean {
5-
const targetDir = path.resolve(process.cwd(), name);
5+
const targetDir = resolve(process.cwd(), name);
66

7-
if (!fs.existsSync(targetDir)) {
7+
if (!existsSync(targetDir)) {
88
return true
99
}
1010

11-
const files = fs.readdirSync(targetDir)
11+
const files = readdirSync(targetDir)
1212
return files.length === 0 || (files.length === 1 && files[0] === '.git')
1313
}

0 commit comments

Comments
 (0)