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

2024-01-14

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

角度.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
        }
      }
    ]
  }
};

我决定添加 SSR 支持ng add @nguniversal/express-engine根据官方文档,但是当我尝试提供 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

这是完全有道理的,因为 SSR 制品ng add @nguniversal/express-engine添加到 angular.json 不要使用我的自定义 webpack 配置。我已经看过了@nguniversal/builders https://github.com/angular/universal/tree/master/modules/builders and @angular-builders https://github.com/just-jeb/angular-builders/tree/master/packages/custom-webpack但找不到任何有关如何为 nguniversal 加载自定义 webpack 文件的信息。

这是我的完整版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
  }
}

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


解决方案是更换@angular-devkit/build-angular:server with @angular-builders/custom-webpack:server在 nguniversal 添加的服务器工件中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-ssr and prerender人工制品,因为它们内部调用server。所以现在我可以将所有 nguniversal npm 脚本与我自己的自定义 webpack 配置一起使用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 非 LL(1) 的 LL(2) 语言

    为了进一步了解解析器和语法 我正在寻找一个 希望简单的 示例language即 LL 2 但不是 LL 1 也就是说 可以由 LL 2 语法生成但不能由任何 LL 1 语法生成的语言 该课程中有有用的语言吗 也就是说 我们可以想象一种 LL
  • WOW.js 无法与 Wordpress 正常工作

    所以我试图让 wow js 和 Animate css 在我的 WordPress 模板上工作 我已经将它们全部链接得很好 它不会引发任何错误 并且动画确实有效 但由于某种原因 动画是在页面加载时触发的 而不是在页面滚动时触发的 效果是所有
  • 如何使用mysql join更新记录?

    在我的 mysql 中 我有 t1 t2 表 我想根据 t1 的字段值与 t2 的字段值匹配 从 t2 的字段值更新 t1 的字段 我尝试了以下但它没有更新 我在这里做错了什么 UPDATE t1 INNER JOIN t2 ON t1 n
  • Sinon-chai CalledWith(new Error()) 并带有确切的消息

    我需要测试这个功能 user js function getUser req res next helper get user param1 param2 err file gt if err return next err 这是我的测试功
  • 如何找到我自己的 Facebook ID? (不是应用程序范围的 ID)

    我以前可以访问我的 Facebook 个人资料页面https www facebook com https www facebook com 用户名 并将 www 替换为 graph https graph facebook com htt
  • Swift 中的 CocoaLumberjack,如何打印行号和文件名称

    我是 CocoaLumberjack 的新手 我让它在 Swift 上工作如下this https github com CocoaLumberjack CocoaLumberjack issues 405 如果我尝试打印日志 DDLogD
  • POM 中的多个依赖范围

    我的 POM 中有一个依赖项 需要将其设置为 提供 因此它不会包含在编译中 但仍然可以在我的项目中引用 当我去运行测试时 我希望相同的依赖项具有 测试 范围 这样我就不必手动将 jar 添加到我的类路径中 有没有办法做到这一点或达到类似的结
  • 在 lm lapply 调用列表中使用权重参数[重复]

    这个问题在这里已经有答案了 这是我的问题 为了可重现而虚构的数据 set seed 42 df lt data frame x rnorm 1000 y rnorm 1000 z rnorm 1000 df2 lt data frame x
  • CMake 和处理子文件夹头文件

    我正在尝试设置我的项目来构建几个包含其完整功能的动态库 每个库都有子文件夹 子文件夹库相互依赖 因此它们必须相互引用函数 我似乎已经成功地让 CMake 在项目上运行而没有错误 但是当我去构建时 我的标头无法找到彼此 似乎在构建时 包含路径
  • 带 Flux 的错误处理程序

    我有一个 React js 应用程序 正在重构它以使用 Flux 架构 并且正在努力弄清楚错误处理在坚持 Flux 模式的同时应该如何工作 目前 当遇到错误时 会触发 jQuery 事件 AppError 并且订阅此事件的通用错误处理助手会
  • 在同一图中绘制不同颜色的波[重复]

    这个问题在这里已经有答案了 如何在 matlab 中绘制具有不同颜色的波 由 1 x N 矩阵表示 可以手动提供特定颜色的范围 有关预期输出 请参见下图 这是一个简单的选项 x linspace 4 pi 4 pi 10000 some d
  • 连接两个邻接矩阵并保留值

    下列的这个问题 https stackoverflow com questions 46295687 r how to make 2 adjacency matrices compatible to eachother 是否可以连接两个邻接
  • Python 2.7,PIP:“构建轮子失败......”

    第一次使用pip 尝试使用python docs samples 教程库 https github com GoogleCloudPlatform python docs samples 当我运行 python m pip install
  • UIView-Encapsulated-Layout 与 UIViewController 中的 UICollectionView 嵌入视图中

    由于某种原因 在嵌入的视图控制器内使用集合视图会在集合视图约束上引发 UIView Encapsulated Layout 我无法设置高度或顶部 底部边距 关于为什么会发生这种情况有什么想法吗 我一直在通过故事板添加约束 我什至尝试以编程方
  • 如何在 div 中间对齐旋转文本

    我想将旋转文本垂直对齐在 100 高度 div 的中间 如下图所示 因此 每当屏幕尺寸发生变化时 文本都会保留在 div 的中间 有人有解决方案吗 http jsfiddle net SVkPU 1 http jsfiddle net SV
  • 检查 Excel 中的组合?

    我有两道多项选择题 有 9 种可能的选择 总共有 20 种答案组合 每个组合都被分配一个值 在 Excel 中 我需要检查不同组合的答案并告诉我该组合的值 这怎么可能 我需要使用什么公式 问答示例 可能的答案 这是我尝试过的方法 它有效 但
  • 如何加快file_get_contents的速度?

    这是我的代码 language GET soundtype word GET sound word urlencode word if language english url
  • 使用 Ransack gem 进行复选框搜索

    我需要过滤表中的数据 为此 我发现元搜索 https github com ernie meta search宝石 我安装了 meta search 并收到此错误 uninitialized constantActiveRecord Ass
  • PHP - SimpleXML - AddChild 与另一个 SimpleXMLElement

    我正在尝试构建一个相当复杂的 XML 文档 我的 XML 文档中有很多重复的部分 我想我应该使用多个字符串模板作为这些部分的基础文档 并使用 simplexml load string 创建 XML 元素的实例 所以我有一个 SimpleX
  • 带有 @nguniversal 的 Angular SSR 和用于 PostCSS 支持的自定义 webpack

    我的 Angular 10 2 1 应用程序使用自定义 webpack 构建器在 scss 文件中提供 PostCSS 支持 在 SPA 模式下工作得非常好 角度 json build builder angular builders cu