gpt4 book ai didi

带有 @nguniversal 的 Angular SSR 和用于 PostCSS 支持的自定义 webpack

转载 作者:行者123 更新时间:2023-12-01 23:43:36 25 4
gpt4 key购买 nike

我的 Angular 10.2.1 应用程序使用自定义 webpack 构建器在我的 .scss 文件中支持 PostCSS,这在 SPA 模式下工作得很好。

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 版本时,出现错误:

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 文件的任何信息。

这是我的完整 angular.json 文件:

{
"$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
}
}

如有任何帮助,我们将不胜感激。 :)

最佳答案

解决方案是在 nguniversal 添加的服务器工件中将 @angular-devkit/build-angular:server 替换为 @angular-builders/custom-webpack:server angular.json 像这样:

        "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-ssrprerender 人工制品,因为它们在内部调用 server。所以现在我可以将所有 nguniversal npm 脚本与我自己的自定义 webpack 配置一起使用。

关于带有 @nguniversal 的 Angular SSR 和用于 PostCSS 支持的自定义 webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64666158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com