如何在 index.ftl 中使用 angular 包(freemarker 模板)
How to use angular bundles in index.ftl (freemarker template)
我正在从事 multi-module 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 模板)作为我的视图,它使用 in-house 库(gradle 依赖项)中的一些宏来获取 headers。但是对于其他所有内容,我必须使用 angular components/pages.
我正在尝试使用 webpack
在 index.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;
});
});
}
}
]
}
index.ftl
<#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>
index.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
以防止缩小错误。
即
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: 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
,因为我没有进行任何分块。您可能需要做同样的事情。
我正在从事 multi-module 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 模板)作为我的视图,它使用 in-house 库(gradle 依赖项)中的一些宏来获取 headers。但是对于其他所有内容,我必须使用 angular components/pages.
我正在尝试使用 webpack
在 index.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;
});
});
}
}
]
}
index.ftl
<#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>
index.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
以防止缩小错误。
即
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: 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
,因为我没有进行任何分块。您可能需要做同样的事情。