英文:
Angular 7 stripping out duplicate properties in SCSS
问题
Here is the code you provided with the requested translations:
Angular SASS (`.scss`) stripping out properties. How do I prevent this so I can embed duplicate properties for cross-browser compatibility?
To duplicate, create a new angular 7.2 project
{
"name": "default-app",
"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": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.9",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
}
Add a "teststyle" class to any element in a component.
<div style="text-align:center" class="teststyle">Hello
</div>
<router-outlet></router-outlet>
In the component's corresponding .scss file apply
.teststyle
{
background: #a64bf4;
background: -webkit-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: -o-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: -moz-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: linear-gradient(right, #6ab443, #04a95f, #00d421);
}
The angular pre-processor strips out everything except the first property and last property.
The corresponding CSS looks like this. -webkit-linear-gradient, -o-linear-gradient, and -moz-linear-gradient are not in the transpiled CSS/JS file. This is a transpilation issue, not a browser issue.
When I use straight CSS I see this, which is correct.
Also including angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"default-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/default-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [],
"es5BrowserSupport": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "default-app:build"
},
"configurations": {
"production": {
"browserTarget": "default-app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "default-app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"default-app-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor
<details>
<summary>英文:</summary>
Angular SASS (`.scss`) stripping out properties. How do I prevent this so I can embed duplicate properties for cross browser compatibility?
To duplicate, create a new angular 7.2 project
```lang-json
{
"name": "default-app",
"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": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.9",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
}
Add a "teststyle" class to any element in a component.
<div style="text-align:center" class="teststyle">Hello
</div>
<router-outlet></router-outlet>
In the component's corresponding .scss file apply
.teststyle
{
background: #a64bf4;
background: -webkit-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: -o-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: -moz-linear-gradient(right, #6ab443, #04a95f, #00d421);
background: linear-gradient(right, #6ab443, #04a95f, #00d421);
}
The angular pre-processor strips out everything except the first property and last property.
The corresponding css looks like this. -webkit-linear-gradient, -o-linear-gradient and -moz-linear-gradient are not int he transpiled css/js file. This is a tarnspilation issue, not a browser issue.
When I use straight css I see this, which is correct.
Also including angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"default-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/default-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [],
"es5BrowserSupport": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "default-app:build"
},
"configurations": {
"production": {
"browserTarget": "default-app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "default-app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"default-app-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "default-app:serve"
},
"configurations": {
"production": {
"devServerTarget": "default-app:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "default-app"
}
答案1
得分: 1
Angular使用Autoprefixer来添加所需的供应商前缀并移除不需要的前缀。基本上,你不需要担心添加任何前缀,因为它们会自动添加给你。被移除的前缀是因为它们不需要。
查看:https://caniuse.com/?search=linear-gradient 以获取使用和支持信息。
-
-webkit-linear-gradient
仅需要在以下情况下使用:- Chrome 4-25(2010年至2013年发布) - 全球浏览器占比0%
- Safari 4-6.1(2010年至2013年发布) - 全球浏览器占比0.01%
- iOS Safari 3.2-6.1(2010年至2013年发布) - 全球浏览器占比0.01%
- Android浏览器 2.1-4.3(2009年至2013年发布) - 全球浏览器占比0.13%
-
-o-linear-gradient
仅需要在 Opera 11.5 和 Opera mobile 12(2011年至2012年发布)中使用,全球浏览器占比0%。 -
-moz-linear-gradient
仅需要在 Firefox 3.5-15(2010年至2012年发布)中使用,浏览器占比0.01%。
如果你仍然希望/需要在2018年发布的Angular版本中为已有10年历史且使用率为0%的浏览器提供支持,你可以简单地添加一个src/browserslist
文件(相关问题请参考这里),你可以在这里阅读关于该文件格式的信息。
英文:
Angular uses Autoprefixer to add any needed vendor prefixes and remove unneeded ones. Basically you don't need to worry about adding any since they will be added for you. The ones that are being removed are because they are not needed.
Check out: https://caniuse.com/?search=linear-gradient for usage and support
-
-webkit-linear-gradient
is only needed for- Chrome 4-25 (released 2010-2013) - 0% worldwide browsers
- Safari 4-6.1 (released 2010-2013) - 0.01% worldwide browsers
- iOS Safari 3.2-6.1 (released 2010-2013) - 0.01% worldwide browsers
- Android browser 2.1-4.3 (released 2009-2013) - 0.13% worldwide browsers
-
-o-linear-gradient
is only needed for Opera 11.5 and Opera mobile 12 (released 2011-2012) which is 0% of worldwide browsers -
-moz-linear-gradient
is only needed for Firefox 3.5-15 (released 2010-2012) which is 0.01% of browsers
If you still want/need to support CSS gradients for 10+ year old browsers with 0% usage in a version of Angular that came out in 2018 you can just add a src/browserslist
file (related question here) which you can read about that file format here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论