I'm trying to switch a sass import URL based on environment. I've reviewed many other questions and answers and this has been fighting me every step of the way. @import does not allow for dynamic strings so instead I'm attempting to use a mixin for each string and @if/@else based on an environment variable however I cannot access the root environment variable from the other sass file.
webpack.config.js
const scssRule = config.module.rules.find(rule => rule.test.test(".scss"));
const scssLoaderConfig = scssRule.use.find(({loader}) => loader === "sass-loader");
scssLoaderConfig.options = scssLoaderConfig.options || {};
scssLoaderConfig.options["implementation"] = require("sass"); //dart-sass
scssLoaderConfig.options["additionalData"] = `$env:"${env}";`; //Pass env to sass
theme.scss
//NOTE: This $env line is not actually in the file. It gets injected by webpack via sass-loader additionalData
$env: "preprod";
@forward "icons" as icon_*;
//More @forward directives such as colors
_icons.scss
@mixin importIconsPreprod {
@import "https://preprod.example.com/icons.css";
}
@mixin importIconsProd {
@import "https://example.com/icons.css";
}
@mixin importIcons() {
//SassError: Undefined variable.
@if $env == "preprod" {
@include importIconsPreprod;
} @else {
@include importIconsProd;
}
//More styles here
}
Desired Implementation
@use "theme-package" as theme;
//Import icons from theme.icon namespace
@include theme.icon_importIcons;