html-webpack-plugin 没有插入 options.title

游艇

我正在尝试通过 webpack 和 html-webpack-plugin 将标题动态注入 html。我正在使用车把模板引擎。但是我没有注入title. 这是我的 webpack 配置的样子 -

/*
|----------------------------------------------
| setting up webpack build process for app
| @author: jahid haque 
| @copyright: mysite, 2019
|----------------------------------------------
*/

const SriPlugin = require('webpack-subresource-integrity');
const Webpack = require('webpack');
const Path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

const Optim = {
    runtimeChunk: 'single',
    splitChunks: {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 0,
        cacheGroups: {
            vendor: {
                test: /[\\/]node_modules[\\/]/,
                name(module) {
                    const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
                    return `npm.${packageName.replace('@', '')}`;
                },
            },
        },
    },
};

const Module = {
    rules: [
        {
            test: /\.jsx$/,
            exclude: /node_module/,
            use: {
                loader: 'babel-loader',
            },
        },
        {
            test: /\.handlebars$/,
            exclude: /node_module/,
            use: {
                loader: 'handlebars-loader',
            },
        },
    ],
};


module.exports = [

    {
        entry: {
            home: Path.resolve(__dirname, './src/components/home/home.controller.jsx'),
        },
        output: {
            path: Path.resolve(__dirname, './public'),
            filename: Path.join('./js/[name].[contenthash].js'),
            publicPath: '',
            crossOriginLoading: 'anonymous',
        },
        optimization: Optim,

        module: Module,

        plugins: [
            new HtmlWebpackPlugin({
                inject: true,
                hash: true,
                title: 'home page. know how we work',
                template: './src/index.handlebars',
                filename: Path.resolve(__dirname, './public/index.html'),
            }),
            new Webpack.HashedModuleIdsPlugin(),
            new SriPlugin({
                hashFuncNames: ['sha512', 'sha384'],
                enabled: true,
            }),
            new Webpack.NamedChunksPlugin(),
            new CleanWebpackPlugin({
                cleanOnceBeforeBuildPatterns: ['./public/js/'],
            }),
            new WebpackMd5Hash(),
        ],

    },       

    {
        entry: {
            style: Path.resolve(__dirname, './src/scss/app.scss'),
        },
        output: {
            path: Path.resolve(__dirname, './public/css/'),
        },
        optimization: {
            minimizer: [
                new OptimizeCSSAssetsPlugin({}),
            ],
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: 'styles.css',
            }),
        ],
        module: {
            rules: [
                {
                    test: /\.s?css$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'sass-loader',
                    ],
                },
            ],
        },
    },
];

我的 htmlWebpack 插件设置是 -

new HtmlWebpackPlugin({
   inject: true,
   hash: true,
   title: 'welcome to home page',
   template: './src/index.handlebars',
   filename: 'index.html',
 }),

在我的index.handlebars文件中,我正在应用这个 -

<!doctype html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Test site | <%= htmlWebpackPlugin.options.title %></title>
        <meta name="description" content="">
        <meta name="keywords" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        {{>partials/head}}
    </head>

    <body class="d-flex flex-column h-100 pt-60">

        {{>partials/nav}}

        <!--[if lt IE 8]>
            <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

        <main role = 'main' class="flex-shrink-0 min-height-750">
            <div id="welcomeHome"></div>    
        </main>  

        {{>partials/footer}}

    </body>
</html>

这是 package.json

{
  "name": "chefcooks",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "start": "webpack-dev-server --mode development --open",
    "dev": "webpack --mode development --watch",
    "build": "webpack --mode production"
  },
  "repository": {
    "type": "git",
    "url": "git+https://[email protected]/hidhaque/chefcooks.git"
  },
  "author": "jahid haque",
  "license": "ISC",
  "homepage": "https://bitbucket.org/hidhaque/chefcooks#readme",
  "devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.4.3",
    "@babel/preset-env": "^7.4.3",
    "@babel/preset-es2015": "^7.0.0-beta.53",
    "@babel/preset-react": "^7.0.0",
    "@babel/runtime": "^7.4.3",
    "axios": "^0.18.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "clean-webpack-plugin": "^2.0.1",
    "css-loader": "^2.1.1",
    "eslint": "^5.16.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-react": "^7.12.4",
    "handlebars": "^4.1.1",
    "handlebars-loader": "^1.7.1",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.6.0",
    "node-sass": "^4.11.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "platform": "^1.3.5",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-loadable": "^5.5.0",
    "sass-loader": "^7.1.0",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.3.1",
    "webpack-md5-hash": "0.0.6",
    "webpack-subresource-integrity": "^1.3.2"
  }
}

但是我index.html遗骸中标题的输出-

<title>Test title | <%- HtmlWebpackPlugin.options.title %></title>

这是.babelrc-

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        "@babel/plugin-transform-runtime", 
        "@babel/plugin-syntax-dynamic-import",
        "transform-async-to-generator",
        [
            "transform-class-properties", 
            { "spec": true }, 
        ]
    ]
}

可以在这里使用一些帮助。我是新来的。非常感谢。

西格弗里德

我试图重现这个问题,但我不能,我做了这个例子,一切正常:webpack.config.js

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

module.exports = {
    mode: 'development',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.handlebars/,
                use: 'handlebars-loader',
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new WebpackHTMLPlugin({
            inject: true,
            hash: true,
            title: 'My custom title!',
            template: './template.hbs'
        })
    ]
}

车把模板:

<html>
<head>
    <meta charset="UTF-8">
    <title>{{ htmlWebpackPlugin.options.title }}</title>
</head>
<body>

</body>
</html>

以防万一我会告诉你我的package.json

{
  "name": "webpack-hbs",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "webpack --config webpack.config"
  },
  "devDependencies": {
    "handlebars-loader": "^1.7.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.0"
  }
}

看着你的代码,我想知道你正在使用 React,也许,你能试试这个代码并讨论输出吗?希望这个答案能说明您的问题。干杯,西格弗里德。

更新

我已经设法解决了这个问题,你可以看到我对index.handlebars. 显然,在指出HTML-的WebPack-插件文档时使用不同的加载器将禁用ejs fallback loader,我假设的这一点,在东阳<title><%= htmlWebpackPlugin.options %></title>没有你的情况的工作,这就是为什么我已经改变了模板使用Handlebars strings interpolation我仍然认为这很奇怪,也许我还缺少其他东西,但无论如何,这现在正在起作用。总是乐于助人,欢呼 sigfried。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为没有HTML的webpack mini-css-extract-plugin提取文件创建链接标签

来自分类Dev

内联样式和html-webpack-plugin

来自分类Dev

Webpack插件'html-webpack-plugin'的EJS模板

来自分类Dev

html-webpack-plugin添加所有脚本的多个入口点

来自分类Dev

html-webpack-plugin 输出 html ,内部主体为空

来自分类Dev

没有带有preload-webpack-plugin的预提取文件

来自分类Dev

Webpack html-webpack-plugin在模板中加载收藏夹图标

来自分类Dev

如何从 webpack 中的 raw-loader 中排除 html-webpack-plugin 模板

来自分类Dev

Vue + html-webpack-plugin无法加载index.html生成

来自分类Dev

如何在 html-webpack-plugin 中使用 ./ 而不是 / bundle src 构建 index.html

来自分类Dev

带有webpack的HTML内联javascript

来自分类Dev

哈姆雷特html没有为我的家庭路线处理程序正确注册<head>或<title>标签

来自分类Dev

extract-text-webpack-plugin-提取scss结果在main.css.map中没有映射

来自分类Dev

如何从没有npm / webpack的HTML文件中导入React模块

来自分类Dev

HTML Title Attribute

来自分类Dev

jQuery将外部HTML页面<title>插入另一个HTML页面

来自分类Dev

Nodejs - 没有 webpack 包

来自分类Dev

需要没有 webpack 等的模块

来自分类Dev

HtmlWebpackPlugin-错误错误:加载程序“ [...] / html-webpack-plugin-/lib/loader.js!/[...]/src/index.html”未返回html

来自分类Dev

如何在带有HTML Webpack插件的webpack构建中包括多个图标?

来自分类Dev

使用带有 webpack 的 angularjs 时无法加载 html 文件

来自分类Dev

具有单独构建的 Webpack 2 html 文件

来自分类Dev

uglifyjs-webpack-plugin安全漏洞

来自分类Dev

Webpack worker-loader与worker-plugin

来自分类Dev

Rails RoutingError(没有匹配的路线[OPTIONS]

来自分类Dev

With_Options 没有正确执行

来自分类Dev

osx中的'fetch options中不受支持的谓词:title ==“ Test”

来自分类Dev

停止pyquery在源HTML中没有空格的地方插入空格?

来自分类Dev

如何在没有jQuery的JavaScript中将HTML插入元素之前?

Related 相关文章

  1. 1

    为没有HTML的webpack mini-css-extract-plugin提取文件创建链接标签

  2. 2

    内联样式和html-webpack-plugin

  3. 3

    Webpack插件'html-webpack-plugin'的EJS模板

  4. 4

    html-webpack-plugin添加所有脚本的多个入口点

  5. 5

    html-webpack-plugin 输出 html ,内部主体为空

  6. 6

    没有带有preload-webpack-plugin的预提取文件

  7. 7

    Webpack html-webpack-plugin在模板中加载收藏夹图标

  8. 8

    如何从 webpack 中的 raw-loader 中排除 html-webpack-plugin 模板

  9. 9

    Vue + html-webpack-plugin无法加载index.html生成

  10. 10

    如何在 html-webpack-plugin 中使用 ./ 而不是 / bundle src 构建 index.html

  11. 11

    带有webpack的HTML内联javascript

  12. 12

    哈姆雷特html没有为我的家庭路线处理程序正确注册<head>或<title>标签

  13. 13

    extract-text-webpack-plugin-提取scss结果在main.css.map中没有映射

  14. 14

    如何从没有npm / webpack的HTML文件中导入React模块

  15. 15

    HTML Title Attribute

  16. 16

    jQuery将外部HTML页面<title>插入另一个HTML页面

  17. 17

    Nodejs - 没有 webpack 包

  18. 18

    需要没有 webpack 等的模块

  19. 19

    HtmlWebpackPlugin-错误错误:加载程序“ [...] / html-webpack-plugin-/lib/loader.js!/[...]/src/index.html”未返回html

  20. 20

    如何在带有HTML Webpack插件的webpack构建中包括多个图标?

  21. 21

    使用带有 webpack 的 angularjs 时无法加载 html 文件

  22. 22

    具有单独构建的 Webpack 2 html 文件

  23. 23

    uglifyjs-webpack-plugin安全漏洞

  24. 24

    Webpack worker-loader与worker-plugin

  25. 25

    Rails RoutingError(没有匹配的路线[OPTIONS]

  26. 26

    With_Options 没有正确执行

  27. 27

    osx中的'fetch options中不受支持的谓词:title ==“ Test”

  28. 28

    停止pyquery在源HTML中没有空格的地方插入空格?

  29. 29

    如何在没有jQuery的JavaScript中将HTML插入元素之前?

热门标签

归档