【问题标题】:Use autoprefixer with angular11使用 angular 11 的 autoprefixer
【发布时间】:2021-04-21 21:28:30
【问题描述】:

我将 angular11 与 SCSS 一起使用。例如,我有以下课程:

.freeSpace {
    flex: 1;
    display: flex;
    align-items: flex-end;
    width: 100%;
    max-width: 600px;
}

但是,当我做ng build --prod 时,它并没有转向:

.freeSpace {
    -webkit-box-flex: 1;
        -ms-flex: 1;
            flex: 1;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: end;
        -ms-flex-align: end;
            align-items: flex-end;
    width: 100%;
    max-width: 600px;
}

如何强制 Angular 使用 autoprefixer?

我的angular.json 文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "we19-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "skipTests": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "../backend/angular",
            "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"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "we19-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "we19-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "we19-app: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": "we19-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "we19-app:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "we19-app"
}

我希望我的最终scss 文件看起来像前缀。 当我在浏览器中打开我的应用程序时,会显示“常规”(未转换为自动前缀代码)scss 代码。

另外,当我手动将以下内容附加到我的 scss 时:

.freeSpace {
    -webkit-box-flex: 1;
        -ms-flex: 1;
            flex: 1;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: end;
        -ms-flex-align: end;
            align-items: flex-end;
    width: 100%;
    max-width: 600px;
}

当我执行ng build --prod 并在我的浏览器中查看应用程序时,所有这些“额外的”scss(例如display: -ms-flexbox)都被忽略了

我的 .browserslistrc:

# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
# For additional information regarding the format and rule options, please see:
# https://github.com/browserslist/browserslist#queries

# For the full list of supported browsers by the Angular framework, please see:
# https://angular.io/guide/browser-support

# You can see what browsers were selected by your queries by running:
#   npx browserslist

last 1 Chrome version
last 1 Firefox version
last 2 Edge major versions
last 2 Safari major versions
last 2 iOS major versions
Firefox ESR
not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line.

【问题讨论】:

  • 为什么要构建一个 Angular 应用程序并为无法运行 Angular 应用程序的浏览器创建 CSS(-ms-flex 仅用于 IE10,-webkit-box 用于 chrome 版本〜20)这些浏览器无法运行您的 Angular 应用程序,他们不了解它的 Javascript。所以没有理由在你的 CSS 中使用它。
  • @cloned 我想支持移动原生应用。简单地说,当我在 Safari 中打开应用程序时,它缺少 display: -webkit-box 所以我没有得到弹性

标签: css angular sass


【解决方案1】:

你可以修改.browserslistrc文件如下

Safari 6
IE 10

这会产生以下 css

.freeSpace {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: end;
    -ms-flex-align: end;
    align-items: flex-end;
    width: 100%;
    max-width: 600px;
}

【讨论】:

  • 我应该用Safari 6替换last 2 Safari major versions吗?
  • 是的,只需将not IE 11 替换为IE 10 并将last 2 Safari major versions 替换为Safari 6
【解决方案2】:

如果你检查the support page for angular,你会发现它只支持最近的浏览器。

因此,如果您构建应用程序,它只会添加必要的前缀。如果您检查for support on flexbox,您将看到所有当前浏览器都支持display: flexdisplay: -ms-box 仅 IE10 支持,Angular 不支持。所以用不必要的前缀来膨胀你的 CSS 是没有意义的。这就是它被过滤掉的原因。

【讨论】:

  • 但是angular怎么不支持Safari浏览器呢?
  • Safari 可以根据我发布的链接理解 display: flex。您需要哪个版本的 safari 前缀?
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2019-06-05
  • 2020-01-28
  • 1970-01-01
  • 2020-04-11
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多