GithubHelp home page GithubHelp logo

error-note's Introduction

Hi there ๐Ÿ‘‹

Linkedin: lallaheee GitHub lallaheee

github stats

visitor

error-note's People

Stargazers

 avatar

Watchers

 avatar

error-note's Issues

CRA + TypeScript + Storybook absolute Path ์„ค์ •

CRA + TypeScript + Storybook ์—์„œ alias ๋กœ import ๊ฒฝ๋กœ ์„ค์ •ํ•˜๊ธฐ


import Button '../../components/atoms/Button';
import Button '@atoms/Button';

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ import ๊ฒฝ๋กœ๊ฐ€ ๊ธธ์–ด์กŒ๋‹ค.
์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ณ  ์‹ถ์–ด ๋” ๋Šฆ๊ธฐ ์ „์—, alias ์„ค์ •์„ ํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค.
webpack์ด๋‚˜ babel์—์„œ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,
CRA์—์„œ๋Š” webpack๊ณผ babel์˜ ์„ค์ •์ด ๊ฐ์ถฐ์ ธ์žˆ๋‹ค.
์ด๋ฅผ ์œ„ํ•ด์„  eject๋ฅผ ํ•˜๊ฑฐ๋‚˜, CRA์˜ ์„ค์ •์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ๋Š” craco ๋‚˜ react-app-rewired ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

๋ฐฉ๋ฒ•๋“ค

1) .env์— NODE_PATH=src/ ์ถ”๊ฐ€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ ์šฉ ์•ˆ๋จ

NODE_PATH=src/

CRA์—์„œ .env ํŒŒ์ผ์— ์œ„์™€ ๊ฐ™์ด NODE_PATH๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ import ๊ฒฝ๋กœ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
์ด ๋ฐฉ๋ฒ•์€ ๋”ฐ๋กœ ๊ฒฝ๋กœ ์ด๋ฆ„์„ ์ •ํ•  ์ˆ˜ ์—†์–ด ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์•˜๋Š”๋ฐ, ์‹œ๋„ํ•ด๋ณด๋‹ˆ ์ ์šฉ๋˜์ง€ ์•Š์•˜๋‹ค.
์Šฌํ”„๊ฒŒ๋„ typescript์—์„œ๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

2) eject ํ•˜๊ธฐ

$ yarn eject

eject๋Š” cra๋กœ ๊ตฌ์„ฑํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆจ๊ฒจ์ ธ์žˆ๋Š” ์„ค์ •์„ ๋ฐ–์œผ๋กœ ์ถ”์ถœํ•˜๋Š” ๋ช…๋ น์ด๋‹ค.
eject ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด Webpack, babel, ESlint ๋“ฑ์˜ ์„ค์ •์ด ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์—, alias ์„ค์ •์„ ํ•˜๋ฉด ๋œ๋‹ค.

ํ•˜์ง€๋งŒ, ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด ๋˜๋Œ๋ฆด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— CRA ๋ฌธ์„œ์—์„œ ์•ต๊ฐ„ํ•˜๋ฉด ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํฌํ•จํ•œ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ์ ํ˜€์žˆ๋‹ค. cra eject ๋ฅผ ๊ตฌ๊ธ€์— ๊ฒ€์ƒ‰ํ–ˆ๋”๋‹ˆ ๋Œ€๋ถ€๋ถ„์˜ ๊ธ€๋“ค์ด eject ํ•˜์ง€ ์•Š๊ณ , eject ์—†์ด XX ํ•˜๊ธฐ ์˜€๋‹ค. ํ ..๋‹ค์Œ๋ฒˆ์— ๊ผญ ejectํ•ด์„œ ์ด๊ฒƒ์ €๊ฒƒ ํ•ด๋ด์•ผ์ง€..ใ…Ž

+Eject์‹œ ๋ฌธ์ œ์  ์ฐพ์•„๋ณด๊ธฐ

3) craco

craco ๋Š” Create React App Configuration Override์˜ ์•ฝ์ž์ด๋‹ค. ๋ง ๊ทธ๋Œ€๋กœ CRA์˜ ์„ค์ •์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“ˆ์ด๋‹ค. How to modify config of Create React App - without Ejecting! ์ด ๊ธ€์„ ์ฝ๊ณ  ์ •์ƒ ์ž‘๋™์ด ๋์œผ๋‚˜, storybook์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์•˜๋‹ค. storybook๋„ ์„ค์ •์ด ํ•„์š”ํ–ˆ๋‹ค.
storybook/preset-create-react-app ์„ ์‚ฌ์šฉ ์ค‘์ด์˜€๊ณ , ๋‹ค์Œ์˜ ๊ธ€๋“ค์„ ์ฝ์œผ๋ฉฐ ์‹œ๋„ํ–ˆ๋‹ค.

dilanx/craco#43
https://storybook.js.org/docs/configurations/custom-webpack-config/#using-your-existing-config
storybookjs/presets#109

craco์˜ createWebpackDevConfig ์„ ์ด์šฉํ•˜์—ฌ, storybook์˜ custom webpack ์„ค์ •์„ ํ•˜๋ คํ–ˆ์œผ๋‚˜, ๊ฒฝ๋กœ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜์˜€๋‹ค.

4) react-app-rewired + tsconfig

๊ทธ๋ ‡๊ฒŒ react-app-rewired๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
react-app-rewired๋ฅผ ์ด์šฉํ•˜์—ฌ alias๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ตฌ๊ธ€๋งํ•˜๋ฉด ๋‚˜์˜จ๋‹ค.

ํ•˜์ง€๋งŒ tsconfig๊ณผ webpack alias๋ฅผ ๋‘˜ ๋‹ค ์ง€์ •ํ•ด์ค˜์•ผ ํ–ˆ๊ณ , ํ”„๋กœ์ ํŠธ์™€ ์Šคํ† ๋ฆฌ๋ถ ๋‘˜๋‹ค ์ •์ƒ ์ž‘๋™์ด ๋˜์—ˆ์ง€๋งŒ
๋‘๋ฒˆ ์ ์–ด์•ผํ•ด์„œ ์ฐ์ฐํ•จ์„ ์ง€์šธ ์ˆ˜ ์—†์—ˆ๋‹ค. (๋˜‘๊ฐ™์€ ๋‚ด์šฉ์ด ์ค‘๋ณต๋˜๋Š” ๊ฑฐ ์‹ซ์–ดํ•˜๋Š” ์Šคํƒ€์ผ)

tsconfig.paths.json์— path๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ด๋ฅผ tsconfig.json ์—์„œ extends๋ฅผ ํ•˜๊ณ ,
react-app-rewired์—์„œ ํ•„์š”ํ•œ ์„ค์ •ํŒŒ์ผ์ธ config-overrides.js์—์„œ ๋˜ํ•œ tsconfig.paths.json์„ ์ฝ์–ด๋“ค์–ด ์›นํŒฉ ํฌ๋งท์— ๋งž๊ฒŒ ๋ฐ”๊ฟ” ์„ค์ •ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์Šคํ† ๋ฆฌ๋ถ์˜ main.js์—์„œ config-overrides.js ์„ค์ •์„ ์ฝ์—ˆ๋‹ค.

๋งˆ์Œ์— ๋“ ๋‹ค.

STEP 1

yarn add --dev react-app-rewired

STEP2

//tsconfig.paths.json
{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "~/*": ["./src/*"],
            "@atoms/*": ["./src/components/atoms/*"],
            //๊ธฐํƒ€ ๊ฒฝ๋กœ ์„ค์ •
        }
    }
}

ํ›„ tsconfig.json์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€

//tsconfig.json
{
  "extends": "./tsconfig.paths.json",
  //...tsconfigs
}

STEP3

config-overrides.js ์ž‘์„ฑ

const tsconfigPaths = require("./tsconfig.paths.json")["compilerOptions"][
    "paths"
];

const path = require("path");

const alias = {};
const resolve = (dir) => path.resolve(__dirname, dir);
const removeSlashStar = (arg) => arg.split("/*")[0];

Object.entries(tsconfigPaths).forEach(([key, [value]]) => {
    const name = removeSlashStar(key);
    const path = removeSlashStar(value);

    alias[name] = resolve(path);
});

module.exports = function (config, env) {
    config.resolve.alias = Object.assign(config.resolve.alias, alias);

    return config;
};

STEP5

package.json ์ˆ˜์ •

    "scripts": {
        "start": "react-app-rewired start",
        "build": "react-app-rewired build",
        "test": "react-app-rewired test",
ยทยทยท 

STEP6

์Šคํ† ๋ฆฌ๋ถ ์„ค์ •

//.storybook/main.js
module.exports = {
    ...configs,
    webpackFinal: (config) => {
        return require("../config-overrides")(config);
    },
};


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.