Although @Felix Eklöf has recommended a very nice approach by programmatically renaming files. Here's something more simple, suitable, and convenient.
Just import both the styles in your component and depending on the variable from .env
file use the one that is needed. Next.js automatically tree-shakes extra classes that are not used, so you don't have to worry about performance in production or making production have large CSS.
import styles1 from "your-styles1.module.scss";
import styles2 from "your-styles2.module.scss";
const styles = process.env.HOSTNAME === "host1" ? styles1 : styles2;
Much straightforward and easier to implement. Right?
Update
If you are looking for conditionally adding global styles. Use link
inside next/head
instead.
First, put your styles inside public
directory. Then, withing your _app.jsx
// do not import global styles that are scopped to specific HOST,
export default function(...) {
const styleSheetLink = getStyleSheetLink(process.env.HOSTNAME) // some logic to detect appropreate stylesheet.
...
return (
<>
<Head>
<link href={styleSheetLink} .../>
</Head>
...
</>
)
...
}
Update
To enable CSS Optimization, you need to install critters
and enable CSS Optimization.
// next.config.js
...
experimental: { optimizeCss: true }
...
}
And
yarn add critters