angular - url-loader 不适用于 angular 8 的自定义 webpack

标签 angular webpack angular8 urlloader

我正在使用 custom webpack将 webpack 与 Angular 8 一起使用,我想使用 url-loader内联 SVG,因为应用程序部署到的服务器不支持 SVG 图像/svg+xml mimetype(我无法更改它以支持它)。但是,当我构建应用程序时,SVG 没有被内联。

Angular .json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "Emergent-Event": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "less"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            },
            "outputPath": "dist/Emergent-Event",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              ...
            ],
            "styles": [
              ...
            ],
            "scripts": [
              ...
            ]
          },
          "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"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "browserTarget": "Emergent-Event:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "Emergent-Event:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "Emergent-Event: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": [
              ...
            ],
            "styles": [
              ...      
            ],
            "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": "Emergent-Event:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "Emergent-Event:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "Emergent-Event"
}

额外的webpack.config.js:
module.exports = {
    module: {
      rules: [
        {
          test: /\.(svg)$/i,
          use: [
            {
              loader: 'url-loader'
            }
          ]
        }
      ]
    }
  };

我也尝试过 svg-url-loader 和 svg-inline-loader 库,但我无法让它们内联 SVG。我不知道还能尝试什么。我的配置是否有问题,或者 Angular 是否在做我不期望的事情?

最佳答案

默认情况下,angular 使用文件加载器处理它。
如何将 file-loader 替换为 base64-inline-loader 的示例代码:

// your custom webpack config
module.exports = config => {
  config.module.rules = config.module.rules
    .filter(rule => rule.loader !== 'file-loader')
    .concat({
      test: /\.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
      loader: 'base64-inline-loader',
      options: {
        name: '[name].[ext]'
      }
    });

  return config;
};

或者您可以编写代码仅删除 svg来自 test字段并仅为它添加新规则。

关于angular - url-loader 不适用于 angular 8 的自定义 webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59052919/

相关文章:

linux - 在VPS上执行长时间的uglifyjs

javascript - @babel/polyfill (Babel 7 beta) 导致 SyntaxError

javascript - 检查字符串是否包含子字符串(不区分大小写)

javascript - Angular 2 alpha 22,如何通过属性将值传递给组件?

node.js - 将 Angular 应用程序从版本 6 升级到版本 7 后如何修复对等依赖警告

ios - 在 Info.plist 上添加任意负载后,NativeScript 上的 Http 请求不起作用

node.js - 无法安装@angular/cli

jquery - TypeError : $(. ..).tooltip 不是 webpack 的函数

javascript - 如何在 ngOnInit 中使用条件测试函数

arrays - Angular 8 : Remove empty Object from array