gpt4 book ai didi

javascript - 将 webpack(环境)变量传递给 scss 文件

转载 作者:行者123 更新时间:2023-11-28 03:11:34 26 4
gpt4 key购买 nike

对 webpack 非常陌生...我希望能够读取一个值,在本例中具体是 env 的值来自webpack.config.jssass文件,这样我就可以根据环境有不同的CSS。

例如:

  • env = 开发,颜色 = 绿色
  • env = 生产,颜色 = 蓝色

到目前为止,我主要关注 sass-loader,试图传递数据,但没有成功,$env当我运行npm run build:Debug时变量总是未定义(这运行 webpack --app=all --env=development )。

这些是我拥有的文件:

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 中使用

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"]
}
};

最后:文档.scss

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

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

最佳答案

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

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

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

关于javascript - 将 webpack(环境)变量传递给 scss 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60058352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com