问题描述
我的 Angular 10.2.1 应用程序在我的 .scss 文件中使用自定义 webpack 构建器支持 PostCSS,这在 SPA 模式下运行良好.
My Angular 10.2.1 app has PostCSS support in my .scss files using a custom webpack builder, which works perfectly fine in SPA mode.
angular.json
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
webpack.config.js
let plugins = [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
]
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
loader: 'postcss-loader',
options: {
ident: 'postcss',
syntax: 'postcss-scss',
plugins: () => plugins
}
}
]
}
};
我决定根据官方文档使用 ng add @nguniversal/express-engine
添加 SSR 支持,但是当我尝试提供 SSR 版本时,出现错误:
I decided to add SSR support with ng add @nguniversal/express-engine
as per official documentation but when I try to serve the SSR version I get the error:
ERROR in Module build failed (from ./node_modules/@angular-devkit/build-angular/node_modules/sass-loader/dist/cjs.js):
SassError: expected ";".
╷
90 │ @apply border-pink-200 !important;
│ ^
╵
src/app/app.component.scss 90:32 root stylesheet
这很有意义,因为 ng add @nguniversal/express-engine
添加到 angular.json 的 SSR 人工制品不使用我的自定义 webpack 配置.我查看了 @nguniversal/builders
和 @angular-builders
但找不到关于如何为 nguniversal 加载自定义 webpack 文件的任何信息.
Which makes perfect sense because the SSR artefacts that ng add @nguniversal/express-engine
adds to angular.json do not use my custom webpack config. I've looked under both @nguniversal/builders
and @angular-builders
but couldn't find any information on how to load a custom webpack file for nguniversal.
这是我的完整 angular.json
文件:
Here is my full angular.json
file:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"hp24-frontend": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "hp24",
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"outputPath": "dist/hp24-frontend/browser",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/assets"
],
"styles": [
"src/styles/styles.scss"
],
"scripts": [],
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
}
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"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": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"browserTarget": "hp24-frontend:build"
},
"configurations": {
"production": {
"browserTarget": "hp24-frontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "hp24-frontend: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": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles/variables/_colors.scss",
"src/styles/styles.scss"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json",
"tsconfig.server.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "hp24-frontend:serve"
},
"configurations": {
"production": {
"devServerTarget": "hp24-frontend:serve:production"
}
}
},
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/hp24-frontend/server",
"main": "server.ts",
"tsConfig": "tsconfig.server.json"
},
"configurations": {
"production": {
"outputHashing": "media",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"sourceMap": false,
"optimization": true
}
}
},
"serve-ssr": {
"builder": "@nguniversal/builders:ssr-dev-server",
"options": {
"browserTarget": "hp24-frontend:build",
"serverTarget": "hp24-frontend:server"
},
"configurations": {
"production": {
"browserTarget": "hp24-frontend:build:production",
"serverTarget": "hp24-frontend:server:production"
}
}
},
"prerender": {
"builder": "@nguniversal/builders:prerender",
"options": {
"browserTarget": "hp24-frontend:build:production",
"serverTarget": "hp24-frontend:server:production",
"routes": [
"/"
]
},
"configurations": {
"production": {}
}
}
}
}},
"defaultProject": "hp24-frontend"
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"lib": [
"es2018",
"dom"
],
"resolveJsonModule": true,
"esModuleInterop": true,
"types": ["googlemaps"]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"debug": true
}
}
任何帮助将不胜感激.:)
Any help would be greatly appreciated. :)
推荐答案
解决方案是将 @angular-devkit/build-angular:server
替换为 @angular-builders/custom-webpack:server
在 nguniversal 添加到 angular.json
的服务器人工制品中,如下所示:
The solution was to replace @angular-devkit/build-angular:server
with @angular-builders/custom-webpack:server
in the server artefact that nguniversal adds to angular.json
like so:
"server": {
"builder": "@angular-builders/custom-webpack:server",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"outputPath": "dist/hp24-frontend/server",
"main": "server.ts",
"tsConfig": "tsconfig.server.json"
},
"configurations": {
"production": {
"outputHashing": "media",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"sourceMap": false,
"optimization": true
}
}
},
这似乎也修复了 serve-ssr
和 prerender
人工制品,因为它们在内部调用 server
.所以现在我可以在我自己的自定义 webpack 配置中使用所有 nguniversal npm 脚本.
This seems to also fix the serve-ssr
and prerender
artefacts because they internally call server
. So now I can use all the nguniversal npm scripts with my own custom webpack config.
这篇关于带有@nguniversal 的 Angular SSR 和用于 PostCSS 支持的自定义 webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!