角度升级从5到6:资产路径必须从项目源根开始

时间:2018-05-06 18:13:07

标签: angular font-awesome

我将现有应用从Angular 5升级到了6.

运行ng serve后,我收到以下错误:

  

node_modules / font-awesome / fonts资源路径必须以   项目源根。错误:node_modules / font-awesome / fonts资产   path必须以项目源根开头。       at assetPatterns.map.assetPattern(/home/kay/Documents/<>/<>/node_modules/@angular-devkit/build-angular/src/utils/normalize-asset-patterns.js:37:23 )

如下所示,我有一个资产数组,它从节点模块包中提取字体。这在Angular 5中不是问题。

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "assets": [
              "src/assets",
              "src/favicon.ico",
              "../node_modules/font-awesome/fonts"
            ],
            "styles": [
              "src/styles.scss",
              "src/theme.scss",
              "node_modules/font-awesome/css/font-awesome.min.css"
            ],
            "scripts": [
              "node_modules/hammerjs/hammer.min.js",
              "node_modules/auth0-js/build/auth0.min.js",
              "node_modules/moment/min/moment.min.js",
              "node_modules/ua-parser-js/dist/ua-parser.min.js",
              "node_modules/d3/dist/d3.min.js",
              "node_modules/wordcloud/src/wordcloud2.js",
              "node_modules/chart.js/dist/Chart.bundle.min.js",
              "node_modules/progressbar.js/dist/progressbar.min.js"
            ]
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "demo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "demo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "scripts": [
              "node_modules/hammerjs/hammer.min.js",
              "node_modules/auth0-js/build/auth0.min.js",
              "node_modules/moment/min/moment.min.js",
              "node_modules/ua-parser-js/dist/ua-parser.min.js",
              "node_modules/d3/dist/d3.min.js",
              "node_modules/wordcloud/src/wordcloud2.js",
              "node_modules/chart.js/dist/Chart.bundle.min.js",
              "node_modules/progressbar.js/dist/progressbar.min.js"
            ],
            "styles": [
              "src/styles.scss",
              "src/theme.scss",
              "node_modules/font-awesome/css/font-awesome.min.css"
            ],
            "assets": [
              "src/assets",
              "src/favicon.ico",
              "node_modules/font-awesome/fonts"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "demo-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "demo:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "e2e/tsconfig.e2e.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "demo",
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "app",
      "styleext": "scss"
    },
    "@schematics/angular:directive": {
      "prefix": "app"
    }
  }
}

6 个答案:

答案 0 :(得分:8)

编辑1

好的,试试这个:

"assets": [
    {"glob": "**/*", "input": "./node_modules/youpackage", "output": "/where_you_want_theasset/"}
]

参考: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/asset-configuration.md

原始答案: 因为它帮助了我(我的所有资产都在src中)

更新后我得到了同样的错误,

尝试将应用的根添加到angular.json

对我来说,sourceRoot和root是相同的

"root": "src",
  "sourceRoot": "src",
像这样:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "src",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",

答案 1 :(得分:4)

好吧, 澄清Abann的解决方案。 对于sourceRoot以外的资产,您必须使用长格式。我确实认为documentation对此可能更加清楚。声明

  

您可以使用此扩展配置从项目外部复制资产。例如,您可以从节点程序包中复制资产:

但实际上,它应该说必须使用

"assets": [
          "src/assets",
          "src/favicon.ico",
           {
            "glob": "*",
            "input": "node_modules/font-awesome/fonts",
            "output": "assets/fonts/"
          }
        ],

assets/fonts/是您可能在SASS / SCSS文件的$fa-font-path变量替代中配置的路径。

答案 2 :(得分:0)

将sourceRoot更改为&#39;。&#39;似乎已经解决了这个并允许我编译,并且表面测试显示一切正常。但不相信这不会引入其他意想不到的后果。

答案 3 :(得分:0)

我将项目升级到角度6.x后得到了相同的错误消息。我的项目引用了PrimeNG,它使用了字体很棒。将PrimeNG升级到6.0.0-alpha.2后,错误已经消失(他们努力工作以获得角色6官方支持)。

因此,如果您遇到此问题,请验证您的依赖项是否也支持Angular 6.x,并且已安装的软件包是最新的。

答案 4 :(得分:0)

这是真的,但是必须写一些不同的东西。这对我有用,在我的有角JSON中

<div class="status" ng-repeat="emp in groups" ng-attr-id="{{emp[attr]}}">
</div>

div.status:not(:first-child) {
  display: none;
}

答案 5 :(得分:0)

在将新项目添加到现有工作区并将开发配置从现有项目的The src/assets asset path must start with the project source root部分复制并粘贴到新项目的serve部分之后,我收到了serve错误消息我的angular.json文件中的项目,但无法更新browserTarget属性以反映新项目的名称。错误消息抱怨现有项目,这对我来说似乎很奇怪,因为我确定我没有修改与该项目有关的任何内容。

错误

请注意,projects.project2.architect.serve.configurations.development.browserTarget的值为project1:build:development,而不是project2:build:development

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "schematics": {
    ...
  },
  "projects": {
    "project1": {
      ...
      "sourceRoot": "projects/project1/src",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              "projects/project1/src/assets"
            ],
            ...
          },
          "configurations": {
            "development": {
              ...
            },
            "production": {
              ...
            }
          }
        },
        "serve": {
          ...
          "configurations": {
            "development": {
              "browserTarget": "project1:build:development"
            },
            "production": {
              "browserTarget": "project1:build:production"
            }
          }
        },
        ...
      }
    },
    "project2": {
      ...
      "sourceRoot": "projects/project2/src",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              "projects/project2/src/assets"
            ],
            ...
          },
          "configurations": {
            "development": {
              ...
            },
            "production": {
              ...
            }
          }
        },
        "serve": {
          ...
          "configurations": {
            "development": {
              "browserTarget": "project1:build:development"
            },
            "production": {
              "browserTarget": "project2:build:production"
            }
          }
        },
        ...
      }
    }
  },
  ...
}

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "schematics": {
    ...
  },
  "projects": {
    "project1": {
      ...
      "sourceRoot": "projects/project1/src",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              "projects/project1/src/assets"
            ],
            ...
          },
          "configurations": {
            "development": {
              ...
            },
            "production": {
              ...
            }
          }
        },
        "serve": {
          ...
          "configurations": {
            "development": {
              "browserTarget": "project1:build:development"
            },
            "production": {
              "browserTarget": "project1:build:production"
            }
          }
        },
        ...
      }
    },
    "project2": {
      ...
      "sourceRoot": "projects/project2/src",
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "assets": [
              "projects/project2/src/assets"
            ],
            ...
          },
          "configurations": {
            "development": {
              ...
            },
            "production": {
              ...
            }
          }
        },
        "serve": {
          ...
          "configurations": {
            "development": {
              "browserTarget": "project2:build:development"
            },
            "production": {
              "browserTarget": "project2:build:production"
            }
          }
        },
        ...
      }
    }
  },
  ...
}