【问题标题】:$styles is undefined with webpack and typescript$styles 未使用 webpack 和 typescript 定义
【发布时间】:2020-03-27 13:25:21
【问题描述】:

我有一个打字稿 SharePoint spfx 解决方案。当我使用 webpack 编译时,我的 $styles 变得未定义,但我可以直接使用类名。

我觉得这里有配置问题,有人可以帮忙吗?

这是我的 scss:

@import "~bootstrap/scss/bootstrap";

//.alert.alert-warning.customalert {
//  text-align: center
//}

.app {
   .top {
        text-align:center;
        justify-content: center;
        .customalert {
          margin-bottom: 0px !important;
          font-size: 14px;
        }
      }
  }

这是我输出的 div:

import styles from './AppCustomizer.module.scss';
return `<div class="${styles.app}">
        <div class="${styles.top}">
          <div class="alert alert-${alertStatus} ${styles.customalert}">
            <strong>${alertTitle}</strong> ${alertDescription}
          </div>
        </div>
      </div>
    and here is my webpack.config.js:

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

    module.exports = {
      mode: "development",
      entry: ['@babel/polyfill',
        path.resolve(__dirname, './Classic/client/bootHeader.ts')],
      module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: "ts-loader",
            exclude: /node_modules/
          },
          {
            test: /\.(s*)css$/,
            use: [
              // fallback to style-loader in development
              process.env.NODE_ENV !== "production"
                ? "style-loader"
                : MiniCssExtractPlugin.loader,
              "css-loader",
              "sass-loader"
            ]
          },
          {
            test: /\.(png|jp(e*)g|svg)$/,
            use: [
              {
                loader: "url-loader",
                options: {
                  limit: 15000, // Convert images < 8kb to base64 strings
                  name: "images/[hash]-[name].[ext]"
                }
              }
            ]
          }
        ]
      },
      plugins: [
        new MiniCssExtractPlugin({
          filename: "[name].css",
          chunkFilename: "[id].css"
        })
      ],
      resolve: {
        extensions: [".tsx", ".ts", ".js"]
      },
      output: {
        filename: "classicBundleAG.js",
        path: path.resolve(__dirname, "Classic"),
        libraryTarget: "umd"
      },
      //externals: [
      //  "@microsoft/sp-loader",
      //]
    };

请注意,如果我像“customalert”一样直接访问样式,则样式会被识别,但 $styles 永远不会被识别并保持未定义。

【问题讨论】:

    标签: typescript webpack sass webpack-style-loader


    【解决方案1】:

    看起来您期待来自stylesobject。只有css-modules才有可能。要启用它,请将 modules: true 添加到您的 css-loader 配置中:

              {
                test: /\.(s*)css$/,
                use: [
                  // fallback to style-loader in development
                  process.env.NODE_ENV !== "production"
                    ? "style-loader"
                    : MiniCssExtractPlugin.loader,
                  {
                   loader: "css-loader",
                   options: {
                    modules: true
                   }
                  },
                  "sass-loader"
                ]
              },
    

    【讨论】:

    • 嗨尼克,就是这样!实际上,我更早地找到了这个解决方案,但因为我的自定义样式完全停止工作而打折了它。事实上他们正在工作,但我有一个烦人的缓存问题,只有在我对我的 scss 进行更改并重建后才纠正它自己。我发现的另一件事是,我真的不喜欢 webpack 默认执行的冗长的 unqiue 翻译类名,而没有提示原始名称是什么。我更喜欢 mystyle_xyz 之类的东西,但我设法通过在 webpack config:localIdentName:'[local]' 中使用此设置来恢复 mystyle
    猜你喜欢
    • 2017-07-12
    • 2021-02-25
    • 2017-02-25
    • 2017-09-25
    • 2018-05-08
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多