簡體   English   中英

如何在 index.ftl(freemarker 模板)中使用 angular 包

[英]How to use angular bundles in index.ftl (freemarker template)

我正在研究具有以下結構的多模塊 Gradle 項目

    parent-app
     - backend
        - src/main
            - java
            - resources
            - webapp
        - build.gradle
     - angular-ui
        - src
            - app
            - envirnoments
            - index.html
            - index.ftl
            - main.ts
        - angular.json
        - package.json
        - webpack.config.js
        - build.gradle
     - build.gradle
     - settings.gradle

我使用index.ftl (freemarker 模板)作為我的視圖,它使用內部庫(gradle 依賴項)中的一些宏來獲取標題。 但對於其他一切,我必須使用 angular 組件/頁面。

我正在嘗試使用webpackindex.ftl文件中動態添加 angular 包(main.js、polyfill.js 等)。

配置在 angular 構建( ng build --prod )處引發minify error ,但我看到 js 文件作為腳本添加到 index.ftl 中。

誰能幫助理解這個問題以及如何解決它,以便我的 angular 包完全加載到 index.ftl 文件中而沒有任何錯誤。

下面是錯誤

Html Webpack Plugin:
<pre>
  Error: html-webpack-plugin could not minify the generated output.
  In production mode the html minifcation is enabled by default.
  If you are not generating a valid html output please disable it manually.
  You can do so by adding the following setting to your HtmlWebpackPlugin config:
  |
  |    minify: false
  |
  See https://github.com/jantimon/html-webpack-plugin#options for details.
  For parser dedicated bugs please create an issue here:
  https://danielruf.github.io/html-minifier-terser/
  Parse Error: <#macro main> 
      <app-root></app-root> 
  <#macro> 
  <#maro pagebody> 
  <#macro><script src="angular-ui/runtime.689a94f98876eea3f04c.js"></script><script src="angular-ui/polyfills.94daefd414b8355106ab.js"></script><script src="angular-ui/main.95a9937db670e12d53ac.js"></script>
  
  - htmlparser.js:244 new HTMLParser
    [angular-webpack]/[html-minifier-terser]/src/htmlparser.js:244:13
  
  - htmlminifier.js:993 minify
    [angular-webpack]/[html-minifier-terser]/src/htmlminifier.js:993:3
  
  - htmlminifier.js:1354 Object.exports.minify
    [angular-webpack]/[html-minifier-terser]/src/htmlminifier.js:1354:16
  
  - index.js:1019 HtmlWebpackPlugin.minifyHtml
    [angular-webpack]/[html-webpack-plugin]/index.js:1019:46
  
  - index.js:435 HtmlWebpackPlugin.postProcessHtml
    [angular-webpack]/[html-webpack-plugin]/index.js:435:40
  
  - index.js:260 
    [angular-webpack]/[html-webpack-plugin]/index.js:260:25
  
  - task_queues:96 processTicksAndRejections
    node:internal/process/task_queues:96:5
  
</pre>

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const fs = require('fs');
const path = require('path');

module.exports = {
  output: {
    "publicPath": "angular-ui/"
  },

  plugins: [
    new HtmlWebpackPlugin({
        "template": "./src/index.ftl",
        "filename": "../backend/src/main/webapp/WEB-INF/templates/index.ftl",
        "inject": false,
        "hash": true,
        "xhtml": true,
    }),
    {
      apply: (compile) => {
        compile.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
          fs.unlink(path.join(process.cwd(), "../backend/src/main/webapp/angular-ui/index.html"), (error) => {
            if (error) throw error;
          });
        });
      }
    }
  ]
}

索引文件

<#macro main>
    <app-root></app-root>
<#macro>
<#maro pagebody>
<% for (key in htmlWebpackPlugin.files.chunks) { %>
    <% if (htmlWebpackPlugin.files.chunks[key].entry) { %>
        <script src="<@spring.url '/<%= htmlWebpackPlugin.files.chunks[key].entry %>'/>" type="text/javascript"></script>
    <% } %>
<% } %>
<#macro>
<@header>

索引.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularUI</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

package.json

{
  "name": "angular-ui",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --open",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.2.14",
    "@angular/common": "~11.2.14",
    "@angular/compiler": "~11.2.14",
    "@angular/core": "~11.2.14",
    "@angular/forms": "~11.2.14",
    "@angular/platform-browser": "~11.2.14",
    "@angular/platform-browser-dynamic": "~11.2.14",
    "@angular/router": "~11.2.14",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^11.1.1",
    "@angular-devkit/build-angular": "~0.1102.17",
    "@angular/cli": "~11.2.17",
    "@angular/compiler-cli": "~11.2.14",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "html-webpack-plugin": "^4.5.2",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.1.5"
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "angular-ui": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.config.js"
            },
            "outputPath": "../backend/src/main/webapp/angular-ui",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "angular-ui:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "angular-ui:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "angular-ui: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.css"
            ],
            "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": "angular-ui:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "angular-ui:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "angular-ui"
}

問題是HtmlWebpackPlugin不知道如何正確解析.ftl文件。 默認情況下,插件將使用ejs-loader 請參閱https://github.com/jantimon/html-webpack-plugin/blob/main/docs/template-option.md

您需要縮小 index.ftl 文件嗎? 我會爭辯說你沒有。 沒有必要,尤其是當您可以在從服務器發送之前壓縮它時。 您應該能夠將值為false的配置屬性minify傳遞到HtmlWebpackPlugin以防止縮小錯誤。

IE

new HtmlWebpackPlugin({
  "template": "./src/index.ftl",
  "filename": "../backend/src/main/webapp/WEB-INF/templates/index.ftl",
  "inject": false,
  "hash": true,
  "xhtml": true,
  "minify": false // <-- property to add
}),

將 minify minify: false條目添加到HtmlWebpackPlugin選項應該可以修復您的即時錯誤。

但是,我還注意到index.ftl在您嘗試設置src屬性時存在語法錯誤。 在關閉src屬性之前有一個額外'/> 具體來說,您需要修改這一行:

<script src="<@spring.url '/<%= htmlWebpackPlugin.files.chunks[key].entry %>'/>" type="text/javascript"></script>

成為:

<script src="<@spring.url '/<%= htmlWebpackPlugin.files.chunks[key].entry %>" type="text/javascript"></script>

此外,在本地測試時,為了將我的 js 文件寫入文件,我需要將您的行htmlWebpackPlugin.files.chunks更改為htmlWebpackPlugin.files.js ,因為我沒有進行任何分塊。 您可能需要這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM