angular - url-loader 不适用于 Angular 8 的自定义 webpack
问题描述
我正在使用自定义 webpack将 webpack 与 Angular 8 一起使用,并且我想使用url-loader来内联 SVG,因为正在部署应用程序的服务器不支持 SVG 图像/svg+xml mimetype(我可以不要改变它来支持它)。但是,当我构建应用程序时,SVG 没有被内联。
角.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 做了我没想到的事情?
解决方案
By default angular handles it with file-loader.
Example code how to replace file-loader to 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;
};
Or you can write code to remove only svg
from test
field and add new rule only for it.
推荐阅读
- php - 如何从 MySQL 表中正确获取结果
- python - 有限的执行:不能为变量分配新值?
- command-line - 支持 GPU 加速和命令行的视频转换器
- firebase - 如何在 Firestore 中获取嵌套的参考数据?
- python - 任意对象的 Django select_for_update()
- apache-spark - 是否可以让两个 Spark 进程同时读取一个 Delta Table 中的流?
- java - 是否可以将自定义数据导入 Sonar 或 Sonarqube
- amcharts - amCharts 4 - valueAxis 工具提示精度太低
- debugging - 更新后调试器不显示
- php - 将多维数组的 json 转换为 csv,其中子元素也具有父值