2024.06.26 - [๐ฉ๐ป๐ป dev] - ๋ชจ๋ ์์คํ ์์ ๋ชจ๋ ์์คํ ์ด ๋ฌด์์ธ์ง, ์ด๋ค ๊ฒ๋ค์ด ์๊ณ , ์ฅ๋จ์ ๋ค์ ์ดํด๋ดค๋ค.
๋ชจ๋ ์์คํ
๐ฃ๏ธ import๊ณผ require์ ์ฐจ์ด๋ฅผ ์๊ณ ๊ณ์๋์? ๐ค ๋ ธ๋ ํ๋ก์ ํธ์์๋ require ๋ง์ด ๋ณธ ๊ฑฐ ๊ฐ๊ณ , react์์ import ๋ง์ด ์ผ๋ ๊ฑฐ ๊ฐ์๋ฐ? import๊ณผ require์ ๋ํด ์๊ธฐ ์ํด์๋ ๋ชจ๋ ์์คํ ์ด๋ผ๋ ๊ฒ
pyotato-dev.tistory.com
๐ฉ๐ป๐ป ์ค๋์ turborepo๋ก nextjs์ sass๋ก ์คํ์ผ๋งํ๊ธฐ ์ํด scss์ next.config.mjs์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ค๋ค๋ณด์.
sass ๊ฐ ๋ชจ๋ ธ๋ ํฌ์ ์ ๋์๊ฐ๊ธฐ ์ํด์๋ sass compiler ์ค์ ์ ํด์ค์ผ ํ๋ค.
nextjs ๊ฐ๋ฐ ๋ฌธ์์ ๋์จ ๋๋ก ์ค์ ์ ํด์ฃผ์.
pnpm --filter $์ค์นํ๋ ค๋_๋ ํฌ install sass --save-dev
๋ค์์ ๋ฌธ์์ ๋์จ sass next.config.js ์ค์ ์ด๋ค.
const path = require('path')
module.exports = {
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
}
ํ์ง๋ง ์ฐ๋ฆฌ๋ ํธ๋ฆฌ์์ดํน์ ํฌํจํ ESM์ ์ฅ์ ๋ค์ ํ์ฉํ๊ธฐ ์ํด type:"module"๋ก ์ค์ ํด๋ค.
๋ฐ๋ผ์ CJS ๋ฌธ๋ฒ์ธ require์ ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํด ์ฃผ๋ฉด ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
}
export default nextConfig
path, __dirname์ ๋ชจ๋ ์ค์ฝํ์์ ์ฐพ์ง ๋ชปํ๊ณ ์๋ค.
์ ๊ทธ๋ด๊น?
CJS ๋ด์ ๋ด์ฅ๋ ๊ธ๋ก๋ฒ ๊ฐ๋ค์ด ESM์๋ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ด ๊ฐ๋ค์๋ __filename, exports, module, require๊ฐ ์๋ค.
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด import๋ฅผ ํด์ฃผ์.
// @ts-check
/**
* https://nextjs.org/docs/app/building-your-application/styling/sass#customizing-sass-options
*/
import path from "path"; //โ
import { fileURLToPath } from "url"; //โ
const __dirname = fileURLToPath(new URL(".", import.meta.url)); //โ
/** @type {import('next').NextConfig} */
const nextConfig = {
sassOptions: {
includePaths: [path.join(__dirname, "styles")],
},
};
export default nextConfig;
๐ ๋ง์น๋ฉด์
์ด๋ฒ์๋ ์์ฃผ ๊ฐ๋จํ ๋ด์ฉ์ธ sass๋ฅผ next.config.mjs์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ดํด๋ดค๋ค.
__filename, exports, module, require๊ฐ CJS์ ๋ด์ฅ ๊ธ๋ก๋ฒ ๋ณ์๊ธฐ ๋๋ฌธ์ import๋ฅผ ํด์ค์ผ ํ๋ค๋ ๊ฑธ ์๋กญ๊ฒ ์์๋ค.
๐ References
https://nextjs.org/docs/app/building-your-application/styling/sass#customizing-sass-options
Styling: Sass | Next.js
Style your Next.js application using Sass.
nextjs.org
https://nextjs.org/docs/app/api-reference/next-config-js
API Reference: next.config.js Options | Next.js
Learn how to configure your application with next.config.js.
nextjs.org
https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/
Alternatives to __dirname in Node.js with ES modules - LogRocket Blog
ES modules with Node.js add a new, standardized global that allows your code to run anywhere, locally or remotely.
blog.logrocket.com
https://nodejs.org/docs/latest/api/modules.html#__dirname
Modules: CommonJS modules | Node.js v22.4.1 Documentation
Modules: CommonJS modules# CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and other JavaScript runtimes. In Node.js, each file is treated as a separate mo
nodejs.org
https://nodejs.org/docs/latest/api/globals.html#__dirname
Global objects | Node.js v22.4.1 Documentation
Global objects# These objects are available in all modules. The following variables may appear to be global but are not. They exist only in the scope of CommonJS modules: The objects listed here are specific to Node.js. There are built-in objects that are
nodejs.org