简体   繁体   English

Angular 7,svg和sass如何设置相对路径

[英]Angular 7, svg and sass how to set relative path

Hi we are currently trying to update a Angular 6 application to Angular 7. But we have one issue. 您好我们正在尝试将Angular 6应用程序更新为Angular 7.但是我们有一个问题。 In Angular 6 building using "ng build". 在Angular 6建筑中使用“ng build”。 Inlines the svg in the css file like this: 像这样在css文件中内联svg:

    background: url('data:image/svg+xml,<%3Fxml version%3D"1.0" encoding%3D"utf-8"%3F>...');

} }

But after upgarding to Angular 7 the svg is moved to the dest folder and the path is set like this: 但是在升级到Angular 7后,svg被移动到dest文件夹,路径设置如下:

    background-image: url('zoom-in.svg');

Other assets are not set to this. 其他资产不适用于此。 Our solution has an other structure so we need to either have it as the it was in Angular 6, or not have the cli move copy and change the paths of the svg files inn our css. 我们的解决方案有另一种结构,所以我们需要像在Angular 6中那样拥有它,或者没有cli移动副本并改变我们的CSS中的svg文件的路径。

So is there a setting in the cli to turn off handling of svg files, so we can you the relative paths we use for other assets in the solution? 那么在cli中是否有一个设置来关闭svg文件的处理,那么我们可以为解决方案中的其他资产使用相对路径吗?

[edit] I found that the code that sets the paths are not the the file-loader as I thought originally. [编辑]我发现设置路径的代码不是我原先想象的文件加载器。 I tried updating i using the ngx-build-plus , witch is a tool that makes it possible to override the webpack settings in Angular. 我尝试使用ngx-build-plus更新i,女巫是一种工具,可以覆盖Angular中的webpack设置。

After some digging into the cli code i found the postcssPluginCreator returns a PostcssCliResources plugin, that takes an filename in, here I could change the generated paths. 在深入研究cli代码之后,我发现postcssPluginCreator返回一个PostcssCliResources插件,它接受一个文件名,在这里我可以改变生成的路径。 I tried to use the [path] to get the real paths, but that never finished building. 我尝试使用[path]来获取真实的路径,但是从未完成构建。 But I could set something else there like filename: test/[name].[ext] and it worked. 但是我可以设置其他类似文件名: test/[name].[ext]并且它有效。

This was a little to hacky for us to use as I had to override/create the postcssPluginCreator function and make webpack use that. 这对我们来说有点难以使用,因为我必须覆盖/创建postcssPluginCreator函数并使webpack使用它。

We ended up setting the target folder of the cli to match the route name of the mvc page. 我们最终设置了cli的目标文件夹以匹配mvc页面的路由名称。

I hope the angular cli team at least enables us to add the path part as an setting in the angular.json file. 我希望angular cli团队至少允许我们将路径部分添加为angular.json文件中的设置。

The code not to use, but shows what I tried: 代码不使用,但显示我尝试的内容:

import {
     PostcssCliResources,
     RawCssLoader,
     RemoveHashPlugin,
     SuppressExtractedTextChunksWebpackPlugin,
   } from "../node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/webpack.js";

const postcssPluginCreator = function (loader: any) {
     return [
       postcssImports({
         resolve: (url: string) => url.startsWith('~') ? url.substr(1) : url,
         load: (filename: string) => {
           return new Promise<string>((resolve, reject) => {
             loader.fs.readFile(filename, (err: Error, data: any) => {
               if (err) {
                 reject(err);
                 return;
               }

               const content = data.toString();
               resolve(content);
             });
           });
         },
       }),
       PostcssCliResources({
         baseHref:'',
         deployUrl:'',
         loader,
         filename: `test/[name].[ext]`, // here is the path set.
       }),
       autoprefixer({ grid: true }),
     ];
   };

看起来这是设计的: https//github.com/angular/angular-cli/issues/12731#issuecomment-432621166 (但我仍然不清楚为什么那么糟糕,这就是为什么我问了一个问题https://github.com/angular/angular-cli/issues/6315 ,您可以发表评论和订阅)。

An update to the issue https://github.com/angular/angular-cli/issues/6315 . 问题的更新https://github.com/angular/angular-cli/issues/6315 Linked to a change where they have added a new option called "resourcesOutputPath". 链接到他们添加了名为“resourcesOutputPath”的新选项的更改。 That adds a path to the resources so that all resources are put into a sub path/folder. 这会添加资源路径,以便将所有资源放入子路径/文件夹中。 You can't get the same as with the file-loader in webpack with the [path] flag, but at least one can separate the source code and resources. 你不能使用[path]标志与webpack中的文件加载器相同,但至少有一个可以分离源代码和资源。

To use it I had to update my cli version to 7.2.3 (i also updated the scheatics-cli to 0.12.3 but don't know if that is needed to) 要使用它,我必须将我的cli版本更新到7.2.3(我还将scheatics-cli更新为0.12.3,但不知道是否需要)

Here is an example of how to use it: 以下是如何使用它的示例:

 "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "Scripts/clidest",
        "index": "app/index.html",
        "main": "app/startUp.ts",
        "tsConfig": "app/tsconfig.app.json",
        "polyfills": "app/polyfills.ts",
        "resourcesOutputPath": "resources",
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM