将webpack(环境)变量传递到scss文件 [英] Pass webpack (environment) variable to scss file

查看:27
本文介绍了将webpack(环境)变量传递到scss文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于webpack来说是新手...我希望能够读取一个值,在这种情况下,尤其是从 webpack.config.js 中的 env 的值 sass 文件,因此我可以根据环境使用不同的CSS.

Very new to webpack... I would like to be able to read a value, in this case specifically the value of env from webpack.config.js in a sass file, so I can have different css based on environment.

例如:

  • env =显影,颜色=绿色
  • env =生产,颜色=蓝色

到目前为止,我一直专注于sass-loader,试图传递数据,但没有成功,当我运行 npm run build:Debug 时,始终未定义 $ env 变量代码>(此代码运行 webpack --app = all --env = development ).

So far I have focoused on sass-loader, trying to pass data, but has not worked, the $env variable is always undefined when i run npm run build:Debug (this runs webpack --app=all --env=development).

这些是我拥有的文件:

webpack.config.js

webpack.config.js

const path = require("path");
const common = require("./.webpack/webpack.common.config");

const config = [];

function addAppConfig(app) {
    app.module = common.module;
    app.resolve = common.resolve;
    config.push(app);
}

module.exports = (env, argv) => {
    switch (argv.app) {
    // Add new configs to below
    case "a":
        addAppConfig(aa);
        break;

    case "b":
        addAppConfig(bb);
        break;

    case "c":
        addAppConfig(cc);
        break;
        
    default:
    case "all":
        addAppConfig(xyz);
    }

    switch (env) {
    case "local":
        config.forEach(appConfig => {
            // The "development" mode configuration option tells webpack to use its built-in development optimizations
            // https://webpack.js.org/configuration/mode/#mode-development
            appConfig.mode = "development";
            appConfig.output.path = path.resolve(__dirname, "../dist");
            appConfig.output.publicPath = "http://localhost:3000/dist";
            appConfig.devtool = "inline-source-map";
            appConfig.devServer = {
                contentBase: path.resolve(__dirname, "./public"),
                port: "3000",
                watchContentBase: true
            };
        });

        break;

    case "development":
        config.forEach(appConfig => {
            // The "development" mode configuration option tells webpack to use its built-in development optimizations
            // https://webpack.js.org/configuration/mode/#mode-development
            appConfig.mode = "development";
            appConfig.devtool = "inline-source-map";
        });

        break;

    default:
    case "production":
        config.forEach(appConfig => {
            // The "production" mode configuration option tells webpack to use its built-in production optimizations including minification etc.
            // https://webpack.js.org/configuration/mode/#mode-production
            appConfig.mode = "production";
            appConfig.devtool = "cheap-source-map";
        });

        break;
    }

    return config;
};

webpack.common.config.js-在webpack.config.js中使用

webpack.common.config.js - this is used in webpack.config.js

module.exports = {
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.(s*)css$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "[name].css"
                        }
                    },
                    "extract-loader",
                    "css-loader",
                    "sass-loader"
                ]
            },
            {
                test: /\.(png|jp(e*)g)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            // Convert images < 8kb to base64 strings
                            limit: 8000,
                            name: "images/[name].[ext]"
                        }
                    }
                ]
            },
            {
                test: /\.(svg)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            // 80kb limit to avoid an IE11 display bug
                            limit: 80000,
                            name: "images/[name].[ext]"
                        }
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            // Convert images < 8kb to base64 strings
                            limit: 8000,
                            name: "fonts/[name].[ext]"
                        }
                    }
                ]
            }
        ]
    },
    resolve: {
        extensions: [".js", ".jsx"]
    }
};

最后:document.scss

and finally: document.scss

.aside__left, .aside__right {
    @if $env == 'development' {
        background-color: red; 
      } @else {
        background-color: green;
      }
      background-color: $desiredColor;
}

是否有一种方法可以从webpack配置中进行scss变量的映射/传递,或者基于环境的任何其他方式,都会生成不同的css?

Is there a way to have scss variable mapping/passing from webpack config, or any other way that based on environment, there would be different css generated?

推荐答案

您可以使用以下方法将变量从webpack配置传递到scss文件:

You can pass variable from webpack config to scss file with the following method:

{
    loader: "sass-loader",
    options: {
        data: "$var1: " + yourVar1+ ";"
    }
}

(c) https://github.com/webpack-contrib/sass-loader/issues/49#issuecomment-284131561

这篇关于将webpack(环境)变量传递到scss文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆