导入全局CSS自定义变量

时间:2018-12-06 18:21:50

标签: css webpack postcss postcss-import

我正在尝试将webpack与postcss一起使用,以导入一个包含我的css自定义变量的theme-variables.css文件。

//theme-variables.css
:root {
    --babyBlue: blue;
}

基本上,我希望任何导入了主题变量的css都能够访问这些css自定义属性并使用postcss-css-variables解析为静态值。

//style.css
@import "./theme-variable.css";

div {
    display: flex;
    color: var(--babyBlue);
}

成为

//main.css
div {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    color: blue;
}

但是我不断出现webpack错误variable --babyBlue is undefined and used without a fallback

main.js最终看起来像这样:

:root {
    --babyBlue: blue;
}
div {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    color: undefined;
}

这是我的webpack(index.js需要styles.js):

const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: { main: "./src/index.js" },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: "css-loader",
            options: { importLoaders: 1 }
          },
          {
            loader: "postcss-loader",
            options: {
              ident: "postcss",
              plugins: loader => [
                require("postcss-css-variables")(),
                require("postcss-cssnext")(),
                require("autoprefixer")(),
                require("postcss-import")()
              ]
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
};

1 个答案:

答案 0 :(得分:0)

解决方案: 必须首先安装postcss-import插件,但是Postcss-loader的插件却不会像webpack加载器那样以相反的顺序出现。

此问题已解决:

var imahov = $(this).attr("alt");
var prim = "{$link->getImageLink($product->link_rewrite, ";
var fin = ", 'small_default')}";