【问题标题】:Configure NG-Zorro with Sass instead of Less使用 Sass 而不是 Less 配置 NG-Zorro
【发布时间】:2020-11-20 05:18:43
【问题描述】:

有没有人在使用 SCSS 样式的 Angular 项目中设置 NG-Zorro 的经验? 我正在尝试使用 antd-scss-theme-plugin 来做到这一点,但似乎我遗漏了一些东西。

没有错误,根据文档,它应该覆盖 theme.scss 文件中的较少变量,但没有任何反应。

看看代码。

webpack.config.js

const path = require("path");


const AntdScssThemePlugin = require("antd-scss-theme-plugin");

const config = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        include: [path.resolve(__dirname, "not_exist_path")],
        exclude: /node_modules/,
        use: [
          {
            loader: "style-loader",
          },
          {
            loader: "css-loader",
          },
          AntdScssThemePlugin.themify({
            loader: "sass-loader",
            options: {
              sourceMap: process.env.NODE_ENV !== 'production',
            },
          }),
        ],
      },
      {
        test: /\.less$/,
        include: [path.resolve(__dirname, "not_exist_path")],
        use: [
          {
            loader: 'style-loader',
            options: {
              sourceMap: process.env.NODE_ENV !== 'production',
            },
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
              sourceMap: process.env.NODE_ENV !== 'production',
            },
          },
          AntdScssThemePlugin.themify('less-loader'),
        ],
      },
    ],
  },
  plugins: [
    new AntdScssThemePlugin(path.resolve(__dirname, 'src', 'assets/theme.scss')),
],
watchOptions: {
  ignored: /dist/,
},
};

module.exports = config;

angular.json

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ng-zorro-themes": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.config.js",
              "browserTarget": "ng-zorro-themes:build"
            },
            "outputPath": "dist/ng-zorro-themes",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "src/styles.scss",
              "node_modules/ng-zorro-antd/ng-zorro-antd.less"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.config.js"
            },
            "browserTarget": "ng-zorro-themes:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "ng-zorro-themes:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ng-zorro-themes:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "ng-zorro-themes:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "ng-zorro-themes:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "ng-zorro-themes"
}

theme.scss

$primary-color: red;

package.json

  "name": "ng-zorro-themes",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.0.5",
    "@angular/common": "~10.0.5",
    "@angular/compiler": "~10.0.5",
    "@angular/core": "~10.0.5",
    "@angular/forms": "~10.0.5",
    "@angular/platform-browser": "~10.0.5",
    "@angular/platform-browser-dynamic": "~10.0.5",
    "@angular/router": "~10.0.5",
    "add": "^2.0.6",
    "ng-zorro-antd": "^9.3.0",
    "rxjs": "~6.5.5",
    "sass-loader": "^7.0.1",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^10.0.0",
    "@angular-devkit/build-angular": "~0.1000.4",
    "@angular/cli": "~10.0.4",
    "@angular/compiler-cli": "~10.0.5",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "antd-scss-theme-plugin": "^1.0.8",
    "babel-plugin-import": "^1.13.0",
    "codelyzer": "^6.0.0",
    "css-loader": "^4.1.1",
    "html-webpack-plugin": "^4.3.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "less-loader": "^5.0.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.14.1",
    "protractor": "~7.0.0",
    "style-loader": "^1.2.1",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  }
}

【问题讨论】:

    标签: angular webpack sass less ng-zorro-antd


    【解决方案1】:

    根据文档,您应该通过 theme.less (https://ng.ant.design/version/8.5.x/docs/customize-theme/en) 覆盖主题变量。

    【讨论】:

    • 我试图避免更少。我想在 scss 文件中覆盖 less 变量,因为我的项目中没有使用 less。这就是我使用这个插件的原因。
    猜你喜欢
    • 2020-12-17
    • 2015-03-16
    • 2012-12-26
    • 2013-06-18
    • 2020-07-29
    • 1970-01-01
    • 2012-02-10
    • 2020-02-08
    • 2013-04-05
    相关资源
    最近更新 更多