πŸ”§ ν”„λ‘ νŠΈ 초기 μ„ΈνŒ… (for React, ts)

robertoΒ·2022λ…„ 10μ›” 18일
0

λ‚ μ΄κ°ˆμˆ˜λ‘ ν”„λ‘ νŠΈ μ΄ˆκΈ°μ„ΈνŒ… ν™˜κ²½μ΄ λ°©λŒ€ν•΄μ§€κ³ μžˆλ‹€ ...

κΉƒν—™ ν…œν”Œλ¦Ώλ¦¬νŒŒμ§€ν† λ¦¬λ„ λ§Œλ“€κ²Έν•΄μ„œ 초기 μ„ΈνŒ…μ„€μ •μ„ λ‚˜μ—΄ν•΄λ΄€λ‹€ πŸ‘»

setting

λ…Έλ“œ ν”„λ‘œμ νŠΈμ˜ μ‹œμž‘

 npm init 

λ¦¬μ—‘νŠΈ , λ¦¬μ—‘νŠΈλ₯Ό μ›Ήμ—μ„œ 그렀쀄 λ¦¬μ—‘νŠΈλ” μ„€μΉ˜

 npm i react react-dom

νƒ€μž…μŠ€ν¬λ¦½νŠΈ μ„€μΉ˜

 npm i typescript

eslint μ„€μΉ˜ (μ½”λ“œ 검사도ꡬ [μ˜€νƒ€,μ•ˆμ“°λŠ”λ³€μˆ˜...])

 npm i -D eslint 

프리티어 μ„€μΉ˜ (μ½”λ“œ 정렬도ꡬ) , eslint 와 프리티어 μ—°κ²° ν”ŒλŸ¬κ·ΈμΈ μ„€μΉ˜

 npm i -D prettier  eslint-plugin-prettier eslint-config-prettier

프리티어 μ„€μ •νŒŒμΌ .prettierrc μ΅œμƒμœ„ν΄λ” ν•˜μœ„μƒμ„±

  
 { 
  "printWidth": 80, //ν•œμ€„ μ΅œλŒ€κΈ€μžμˆ˜
  "tabWidth": 2,
  "singleQuote": true, //μž‘μ€λ”°μ˜΄ν‘œ
  "trailingComma": "all", //μ½€λ§ˆλ’€μ—
  "semi": true
}

eslint μ„€μ •νŒŒμΌ .eslintrc μ΅œμƒμœ„ν΄λ” ν•˜μœ„μƒμ„±


 
{
  "extends": ["plugin:prettier/recommended"]
}

ts 섀정을 μœ„ν•œ tsconfig.json μ΅œμƒμœ„ν΄λ” ν•˜μœ„μƒμ„±


{
  "compilerOptions": {
    "esModuleInterop": true, //import * as (x)
    "sourceMap": true, //λŸ°νƒ€μž„μ—μ‹œ μ—λŸ¬ μœ„μΉ˜ μ°Ύμ•„μ€Œ 
    "lib": ["ES2020", "DOM"],
    "jsx": "react",
    "module": "esnext",
    "moduleResolution": "Node",
    "target": "es5",
    "strict": true, // any νƒ€μž…λ°©μ§€ 
    "resolveJsonModule": true,
    "baseUrl": ".",
    "paths": { // import ../../../ => @src/...
      "@hooks/*": ["hooks/*"],
      "@components/*": ["components/*"],
      "@layouts/*": ["layouts/*"],
      "@pages/*": ["pages/*"],
      "@utils/*": ["utils/*"],
      "@typings/*": ["typings/*"]
    }
  },
  //webpack 이 ts λ₯Ό μΈμ‹ν•˜κΈ°μœ„ν•œ μ½”λ“œ 
  "ts-node": {
    "compilerOptions": {
      "module": "commonjs",
      "moduleResolution": "Node",
      "target": "es5",
      "esModuleInterop": true
    }
  }
}

webpack

webpack κ΄€λ ¨ μ„€μ • npm λ‹€μš΄

 npm i -D webpack @babel/core babel-loader @babel/preset-env @babel/preset-react

webpack κ΄€λ ¨ TypeScript μ„€μ •λ‹€μš΄

 npm i -D @types/webpack @types/node @babel/preset-typescript
 npm i ts-node

webpack κ΄€λ ¨ css loader λ‹€μš΄

npm i style-loader css-loader

webpack 섀정을 μœ„ν•œ webpack.config.ts μ΅œμƒμœ„ν΄λ” ν•˜μœ„μƒμ„±(jsλŠ” ν™•μž₯자 ts->js)
πŸ™‹πŸ»β€β™€οΈ webpack.config.ts => λ…Έλ“œ λŸ°νƒ€μž„μ— μ‹€ν–‰λœλ‹€

import path from 'path';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import webpack, { Configuration as WebpackConfiguration } from "webpack";
import { Configuration as WebpackDevServerConfiguration } from "webpack-dev-server";

interface Configuration extends WebpackConfiguration {
  devServer?: WebpackDevServerConfiguration;
}

import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

const isDevelopment = process.env.NODE_ENV !== 'production';

const config: Configuration = {
  name: 'μ‚¬μš©μž λͺ…λͺ…',
  mode: isDevelopment ? 'development' : 'production',
  devtool: !isDevelopment ? 'hidden-source-map' : 'eval',
  resolve: {
  //바벨이 μ²˜λ¦¬ν•  ν™•μž₯자 λͺ©λ‘
    extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
    alias: {
      '@hooks': path.resolve(__dirname, 'hooks'),
      '@components': path.resolve(__dirname, 'components'),
      '@layouts': path.resolve(__dirname, 'layouts'),
      '@pages': path.resolve(__dirname, 'pages'),
      '@utils': path.resolve(__dirname, 'utils'),
      '@typings': path.resolve(__dirname, 'typings'),
    },
  },
  //κ²°κ³Όλ¬Ό 이름 './client
  entry: {
    app: './client',
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'babel-loader',
        options: {
          presets: [
            [
              '@babel/preset-env',
              {
                targets: { browsers: ['IE 10'] },
                debug: isDevelopment,
              },
            ],
            '@babel/preset-react',
            '@babel/preset-typescript',
          ],
          env: {
            development: {
              plugins: [require.resolve('react-refresh/babel')],
            },
          },
        },
        exclude: path.join(__dirname, 'node_modules'),
      },
      {
        test: /\.css?$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new ForkTsCheckerWebpackPlugin({
      async: false,
      // eslint: {
      //   files: "./src/**/*",
      // },
    }),
    new webpack.EnvironmentPlugin({ NODE_ENV: isDevelopment ? 'development' : 'production' }),
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].js',
    publicPath: '/dist/',
  },
  devServer: {
    historyApiFallback: true, // react router
    port: 3090,
    devMiddleware: { publicPath: '/dist/' },
    static: { directory: path.resolve(__dirname) },
  },
};

if (isDevelopment && config.plugins) {
  config.plugins.push(new webpack.HotModuleReplacementPlugin());
  config.plugins.push(new ReactRefreshWebpackPlugin());
}
if (!isDevelopment && config.plugins) {
}

export default config;

package.json

{
  "name": "settings",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack serve --env development",
    "build": "cross-env NODE_ENV=production webpack"
  },
  "author": "ZeroCho",
  "license": "MIT",
  "dependencies": {
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.1",
    "ajv": "^8.11.0",
    "axios": "^0.26.1",
    "core-js": "^3.15.1",
    "cross-env": "^7.0.3",
    "react": "^17.0.1",
    "react-dom": "^17.0.2",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "typescript": "^4.4.2"
  },
  "devDependencies": {
    "@babel/core": "^7.13.8",
    "@babel/preset-env": "^7.13.8",
    "@babel/preset-react": "^7.12.13",
    "@babel/preset-typescript": "^7.13.0",
    "@pmmmwh/react-refresh-webpack-plugin": "^0.5.9",
    "@types/fork-ts-checker-webpack-plugin": "^0.4.5",
    "@types/node": "^16.11.26",
    "@types/react-router-dom": "^5.1.7",
    "@types/webpack": "^5.28.0",
    "@types/webpack-dev-server": "^4.0.3",
    "babel-loader": "^8.2.2",
    "css-loader": "^6.2.0",
    "eslint": "^8.13.0",
    "eslint-config-prettier": "^8.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "fork-ts-checker-webpack-plugin": "^7.2.3",
    "prettier": "^2.2.1",
    "react-refresh": "^0.12.0",
    "style-loader": "^3.2.1",
    "ts-node": "^10.9.1",
    "webpack": "^5.24.2",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^4.11.1"
  }
}

directory file μ„€μ •

(상단 디렉토리 이미지참쑰)

μ΅œμƒμœ„ index.html 파일 생성

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>hello world</title>
</head>
<body>
<div id="app"></div>
<script src="/dist/app.js"></script>
</body>
</html>

μ΅œμƒμœ„ ν•˜μœ„ src/index.tsx 파일 생성

import React from 'react';
import {render} from 'react-dom';

import App from './layout/App'

render (<App/>, document.querySelector('#app'));

μ΅œμƒμœ„ ν•˜μœ„ layout 디렉토리생성 layout/App.tsx 파일 생성

import React from 'react';

const App = () => {
    return <div>setting</div>;
};

export default App;

link

ν™˜κ²½μ„€μ • 이해λ₯Ό λ„ν™”μ£ΌλŠ” 링크

profile
medium 으둜 μ΄μ „ν–ˆμŠ΅λ‹ˆλ‹€

0개의 λŒ“κΈ€