Skip to content

Commit

Permalink
refactor: 경로 단축
Browse files Browse the repository at this point in the history
  • Loading branch information
d0422 committed Jun 5, 2024
1 parent 6efc0ff commit b063078
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
10 changes: 5 additions & 5 deletions apps/miniNext/src/core/createClientFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getPagesFiles } from './getPagesFiles';

export const createClientFiles = async () => {
const files = await getPagesFiles('src/pages');
const isExists = existsSync(path.resolve('public'));
if (!isExists) mkdirSync(path.resolve('public'));

files.forEach((fullFileName) => {
const fileName = fullFileName
Expand All @@ -13,17 +15,15 @@ export const createClientFiles = async () => {
if (fileName.match('/')) {
const filePaths = fileName.split('/');
const dirPath = filePaths.slice(0, -1).join('/');
const isExists = existsSync(
path.resolve(__dirname, `../../public`, dirPath)
);
const isExists = existsSync(path.resolve('public', dirPath));
if (!isExists) {
mkdirSync(path.resolve(__dirname, `../../public`, dirPath), {
mkdirSync(path.resolve('public', dirPath), {
recursive: true,
});
}
}
writeFile(
path.resolve(__dirname, `../../public/${fileName}.tsx`),
path.resolve(`public/${fileName}.tsx`),
`import * as Page from '@/pages/${fileName}';
import { hydrate } from '@core/render';
Expand Down
12 changes: 4 additions & 8 deletions apps/miniNext/src/core/getPagesFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ const PAGES_PATH = 'dist/pages';

export const getPagesFiles = async (pathString = PAGES_PATH) => {
return await new Promise<string[]>((resolve, reject) => {
readdir(
path.resolve(__dirname, '../../', pathString),
{ recursive: true },
(err, files) => {
if (err) reject(err);
resolve((files as string[]).filter((file) => file.match('.js')));
}
);
readdir(path.resolve(pathString), { recursive: true }, (err, files) => {
if (err) reject(err);
resolve((files as string[]).filter((file) => file.match('(.js)|(.tsx)')));
});
});
};
4 changes: 1 addition & 3 deletions apps/miniNext/src/core/getServerSidePropsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export const getServerSidePropsFunction = async () => {

const files = await getPagesFiles();
files.map((file) => {
const { getServerSideProps } = require(
path.resolve(__dirname, '../pages', file)
);
const { getServerSideProps } = require(path.resolve('dist/pages', file));
result[file] = getServerSideProps;
});
return result;
Expand Down
2 changes: 1 addition & 1 deletion apps/miniNext/src/core/routeMapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const routeMapper = async (app: Express) => {
const serverSideFunction = serverSideFunctions[fullFileName];
const serverSideProps = serverSideFunction?.().props;
const { default: Component } = require(
path.resolve(__dirname, '../pages', fullFileName)
path.resolve('dist/pages', fullFileName)
);
const html = createHTML(
<Component {...serverSideProps} />,
Expand Down

0 comments on commit b063078

Please sign in to comment.