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

Posted

技术标签:

【中文标题】带有 @nguniversal 的 Angular s-s-r 和用于 PostCSS 支持的自定义 webpack【英文标题】:Angular s-s-r with @nguniversal and a custom webpack for PostCSS support 【发布时间】:2021-02-16 07:50:14 【问题描述】:

我的 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 添加 s-s-r 支持,但是当我尝试提供 s-s-r 版本时,我收到错误:

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 的 s-s-r 人工制品不使用我的自定义 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-s-s-r": 
          "builder": "@nguniversal/builders:s-s-r-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
  

任何帮助将不胜感激。 :)

【问题讨论】:

【参考方案1】:

解决方案是在 nguniversal 添加到 angular.json 的服务器工件中将 @angular-devkit/build-angular:server 替换为 @angular-builders/custom-webpack:server,如下所示:

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

【讨论】:

以上是关于带有 @nguniversal 的 Angular s-s-r 和用于 PostCSS 支持的自定义 webpack的主要内容,如果未能解决你的问题,请参考以下文章

工作区中未设置 Angular 12 通用配置“开发”

如何使用 Firebase 功能部署 Angular 9 通用应用程序

Angular Universal:添加服务工作者不允许页面的特定标题

在 plesk 中设置服务器端渲染的 Angular 应用程序

带有 Angular Material 声明的 Angular 编译警告

带有表单和 Angular 日期的 Angular/HTML 输入本地日期时间