* 개발 환경 : react(cra), ts, storybook, sass, tailwind, rollup
1. 이슈 사항
프로젝트 내에서 상대경로를 쓰지 않고 custom alias 사용 중인 상황
ex) @ea => ./src
rollup 에서 custom alias를 처리하기 위한 플러그인으로 @rollup/plugin-alias 가 있다.
https://www.npmjs.com/package/@rollup/plugin-alias
@rollup/plugin-alias
Define and resolve aliases for bundle dependencies. Latest version: 5.0.1, last published: 9 days ago. Start using @rollup/plugin-alias in your project by running `npm i @rollup/plugin-alias`. There are 584 other projects in the npm registry using @rollup/
www.npmjs.com
해당 플러그인은 ts, tsx 파일 내에서는 잘 매칭하여 변환을 해주지만,
이상하게 sass 파일내에서만 custom alias를 resolve하지 못하는 상황이 발생...
2. 해결 방법
원래 scss파일을 빌드하기 위해
https://github.com/egoist/rollup-plugin-postcss#readme
GitHub - egoist/rollup-plugin-postcss: Seamless integration between Rollup and PostCSS.
Seamless integration between Rollup and PostCSS. Contribute to egoist/rollup-plugin-postcss development by creating an account on GitHub.
github.com
해당 플러그인을 많이 사용하지만, scss파일 내부에서 custom-alias(@) 를 사용할 때
sass-loader에서 importer 을 추가해서 처리를 해줘야 하지만,
rollup-plugin-postcss가 커스텀 importer을 추가하지 못하는 버그(?) 가 있어서
(https://github.com/egoist/rollup-plugin-postcss/issues/397) <= 관련 이슈
rollup-plugin-styles를 사용하였다.
내가 정의한 custom-importer는 아래 형태!
const customImporter = (url) => {
if (url.startsWith('@')) {
const newUrl = url.replace('@', './src');
const filePath = path.resolve(__dirname, newUrl);
const result = {file: filePath};
return result;
}
return null;
};
styles() 내부에 추가해주면 된다.
styles({
url: {
alias: {'~@': path.resolve(__dirname, './src')},
publicPath: './assets',
hash: false
},
sass: {
importer: customImporter,
},
}),
url은 css url resolver옵션으로,
css 파일 내에서 url() 을 사용할 때 해당 custom alias을 resolve 하도록 만들어주는 속성이다.
나는 빌드됐을 때 이미지 파일들을 assets 안에 넣을 예정이라, publicPath를 assets 이라고 해줬다.
@ 앞에 ~가 붙은 이유는 아래 링크 참조..!
scss파일내에서 background: url("~@/asset/...")형태로 사용해야 하기에...
https://stackoverflow.com/questions/39535760/what-does-a-tilde-in-a-css-url-do
What does a `~` tilde in a CSS `url()` do?
E.g. @import url("~./foobar"); Saw it here, not sure if it's some package specific thing or if it's actual CSS syntax.
stackoverflow.com
'Frontend > Article' 카테고리의 다른 글
모바일 앱(App)과 웹뷰(WebView)의 통신에 관하여 (Feat. JavaScriptInterface) (2) | 2024.04.27 |
---|---|
Rollup을 이용하여 컴포넌트 라이브러리 만들기 ! (React, Ts, Sass, Storybook) (5) | 2023.11.26 |
Style Dictionary 사용 방법 정리(feat. 피그마 디자인 토큰 가공하기) (0) | 2023.09.23 |
Storybook path alias 설정 (0) | 2023.09.09 |
자바스크립트의 숫자(Number)형 (Feat. 부동 소수점 vs 고정 소수점) (0) | 2023.05.07 |