You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按照Material Design文档配置Top App Bar时Sass混入报错

问题:Material Design Top App Bar SCSS配置后Webpack构建失败

按照Material Design Top App Bar的SCSS文档配置后,Webpack无法构建SCSS代码。在styles.scss中尝试引入icon-button.core-styles混入时失败,IntelliJ提示“Cannot find mixin 'core-styles'”,同时Webpack抛出以下错误:

错误信息

ERROR in ./src/styles.scss
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Undefined mixin.
  ╷
6 │ @include icon-button.core-styles;
  │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  src\styles.scss 6:1  root stylesheet
SassError: SassError: Undefined mixin.
  ╷
6 │ @include icon-button.core-styles;
  │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  src\styles.scss 6:1  root stylesheet
    at Object.loader (D:\...\webpack\node_modules\sass-loader\dist\index.js:69:14)

webpack 5.75.0 compiled with 1 error and 1 warning in 1615 ms

相关代码文件

src/styles.scss

@use "@material/icon-button";
@use '@material/top-app-bar/mdc-top-app-bar';
@use "@material/icon-button/styles";


@include icon-button.core-styles;

webpack.config.js

const autoprefixer = require('autoprefixer');
const path = require('path');

module.exports = {
    entry: {
        // index: './src/_index.ts',
        styles: './src/styles.scss',
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js', 'scss'],
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, './../public/dist'),
        clean: true,
    },
    watch: true,
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    {loader: 'file-loader', options: {name: '[name].css'}},
                    {loader: 'extract-loader'},
                    {loader: 'css-loader'},
                    {loader: 'postcss-loader', options: {postcssOptions: {plugins: [autoprefixer()]}}},
                    {
                        loader: 'sass-loader',
                        options: {/* Prefer Dart Sass*/
                            implementation: require('sass'), /* See https://github.com/webpack-contrib/sass-loader/issues/804*/
                            webpackImporter: false,
                            sassOptions: {includePaths: ['./node_modules']},
                        },
                    }],
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
};

package.json

{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "build": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.15.8",
    "@babel/preset-env": "^7.15.8",
    "autoprefixer": "^10.4.13",
    "babel-loader": "^9.1.2",
    "css-loader": "^6.7.3",
    "extract-loader": "^5.1.0",
    "file-loader": "^6.2.0",
    "postcss-loader": "^6.2.0",
    "sass": "^1.58.0",
    "sass-loader": "^12.2.0",
    "ts-loader": "^9.2.6",
    "typescript": "^4.4.4",
    "webpack": "^5.75.0",
    "webpack-cli": "^5.0.1",
    "webpack-dev-server": "^4.3.1"
  },
  "dependencies": {
    "@material/icon-button": "^14.0.0",
    "@material/top-app-bar": "^14.0.0"
  }
}

解决方案

问题根源是MDC v14版本中,core-styles混入并不在@material/icon-button主模块中,而是位于@material/icon-button/styles子模块内。修改styles.scss代码即可解决:

调整后的src/styles.scss

@use '@material/top-app-bar/mdc-top-app-bar';
// 给styles模块指定icon-button命名空间,便于调用
@use "@material/icon-button/styles" as icon-button;

// 调用正确模块下的core-styles混入
@include icon-button.core-styles;

如果不想自定义命名空间,也可以使用默认的styles命名空间:

@use '@material/top-app-bar/mdc-top-app-bar';
@use "@material/icon-button/styles";

@include styles.core-styles;

另外,若后续仍出现模块解析问题,可检查webpack配置中sass-loader的webpackImporter选项——当前设置为false,若影响模块查找,可尝试改为true,不过当前配置的includePaths已正确指向node_modules,这部分大概率无需调整。

内容的提问来源于stack exchange,提问作者vincent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:36:10