如何使Angular-CLI在资源文件夹中将SCSS编译为CSS

时间:2017-08-29 04:56:39

标签: angular sass angular-cli

我有一个Angular 4项目,使用angular-cli 1.0.x创建。 此时,angular-cli在我的项目中升级到1.1.1。

我有一个资源文件夹,当我在其中放入一个css文件时,可以使用它。 但是当我将css文件重命名为scss时,它不会被angular-cli编译。

我需要做什么才能将scss文件编译为css?

这是我的项目的package.json:

{
  "name": "my project",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "4.1.3",
    "@angular/common": "4.1.3",
    "@angular/compiler": "4.1.3",
    "@angular/core": "4.1.3",
    "@angular/forms": "4.1.3",
    "@angular/http": "4.1.3",
    "@angular/platform-browser": "4.1.3",
    "@angular/platform-browser-dynamic": "4.1.3",
    "@angular/router": "4.1.3",
    "@angular/upgrade": "4.1.3",
    "@axinpm/ang-core": "^0.2.3",
    "chart.js": "2.5.0",
    "core-js": "2.4.1",
    "moment": "2.18.1",
    "ng2-charts": "1.5.0",
    "ng2draggable": "^1.3.2",
    "ngx-bootstrap": "1.6.6",
    "rxjs": "5.4.0",
    "ts-helpers": "1.1.2",
    "zone.js": "0.8.11"
  },
  "devDependencies": {
    "@angular/cli": "1.1.1",
    "@angular/compiler-cli": "4.1.3",
    "@types/jasmine": "2.5.47",
    "@types/node": "7.0.22",
    "codelyzer": "3.0.1",
    "jasmine-core": "2.6.2",
    "jasmine-spec-reporter": "4.1.0",
    "karma": "1.7.0",
    "karma-chrome-launcher": "2.1.1",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "karma-coverage-istanbul-reporter": "1.2.1",
    "protractor": "5.1.2",
    "ts-node": "3.0.4",
    "tslint": "5.3.2",
    "typescript": "2.4.2"
  }
}

这是我的项目.angular-cli.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "version": "1.0.0-alpha.6",
    "name": "my project"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets"],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "scss/style.scss"
      ],
      "scripts": [
        "../node_modules/chart.js/dist/Chart.bundle.min.js",
        "../node_modules/chart.js/dist/Chart.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "prefixInterfaces": false
  }
}

由于

3 个答案:

答案 0 :(得分:6)

发生这种情况是因为根据assets配置将.angular-cli.json文件夹用于静态内容:

"assets": ["assets"]

因此,通过设置该设置,所有assets文件夹内容始终会按原样复制。您必须将.scss放在assets文件夹之外,以避免由cli构建工具链输出。您也可以考虑为自定义scss文件创建一个单独的文件夹。

  

但是,如果您正确引用了scss,则无论如何都不应该制作您的应用。

如果说,您的theme.scss文件夹中有assets个文件,您可以在全球styles.scss中引用@import '/assets/theme';这样的文件,就像cli会生成建立。然后在构建生成之后,所有样式最终都会注入styles.bundle.js。此时,在运行时没有使用assets/theme.scss文件,但是在运行命令ng build的情况下,它仍会保留在assets文件夹中。因此,您最好从assets文件夹中取出它,以保持其不受不必要的文件的影响。

答案 1 :(得分:2)

要更改项目的现有样式,您需要运行此命令。

ng set defaults.styleExt scss

它将更改扩展名,您将能够访问SCSS样式。

SCSS文件保存在assets文件夹下,因为@Kuncevic提到assets文件夹包含静态内容。

答案 2 :(得分:2)

您可以创建一个名为“styles”的新文件夹,与“assets”文件夹平行,并将.scss文件放在那里,并在.scss的“样式”节点中引用.angular-cli.json文件文件:

"styles": [
  "./styles/site.scss"
]

如果仍然无效,请将其添加到.angular-cli.json的“默认值”节点:

"defaults": {
  "styleExt": "css",
}