【发布时间】: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所以我没有得到弹性