GVKun编程网logo

如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html?(在开发网页之前首先应建立什么来管理网页及图像的内容)

9

关于如何在开发中的任何路线上提供HtmlWebpackPlugin版本的index.html?和在开发网页之前首先应建立什么来管理网页及图像的内容的问题就给大家分享到这里,感谢你花时间阅读本站内容,更

关于如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html?在开发网页之前首先应建立什么来管理网页及图像的内容的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于05webpack中html-webpack-plugin的2个作用、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、chainWebpack 和 htmlWebpackPlugin搭配使用、html-webpack-plugin 详细配置等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html?(在开发网页之前首先应建立什么来管理网页及图像的内容)

如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html?(在开发网页之前首先应建立什么来管理网页及图像的内容)

如何解决如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html??

阅读 this question 后,我了解到 HtmlWebpackPlugin 在 http://localhost:8080/index.html 提供 index.html 的内存版本并注入包(在我的情况下为 {{1 }} 和 vendors.js) 到文件中。

我在 app.js 中准备了一个 index.html 模板,它是这样的:

/src

当我访问位于 <!doctype html> <html> <head> <Meta charset="utf-8"> </head> <body> <div id="app"></div> <!-- htmlwebpackplugin scripts should inject below --> </body> </html> 的主页时,我在 HTML 检查器中得到了正确的信息:

http://localhost:8080

但是,如果我直接在我的应用程序中访问另一条路线,例如 <!doctype html> <html> <head> <Meta charset="utf-8"> </head> <body> <div id="app"></div> <!-- htmlwebpackplugin scripts should inject below --> <script src="/vendors.js"></script> <script src="/app.js"></script> </body> </html> (由 Node/Express 作为服务器端渲染提供),那么我会得到原始版本的 http://localhost:8080/shop,而没有捆绑的 { ` 标记底部的 {1}} 个文件。

如果 HtmlWebpackPlugin 的 index.html 只在根和内存中提供,那么它如何作为 Node/Express 的模板向浏览器发送 HTML? Node/Express 通过查看 .js 并在磁盘上使用该文件来处理路由,而无需 HtmlWebpackPlugin 创建的注入包。

简单的答案是手动将 index.htmlsrc/index.html 添加到 vendor.js 中,但会混淆生产构建,因为它包含对生产中不存在的这些开发文件的引用(它们存在,但具有散列名称)。

我错过了什么/做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

05webpack中html-webpack-plugin的2个作用

05webpack中html-webpack-plugin的2个作用

  <!-- 15  html-webpack-plugin的2个作用
     下载 cnpm i html-webpack-plugin -D   作用在==>内存中生成页面
可以在package.json中去查看是否有 在webpack中 导入在内存中生成的HTML页面的插件
// 只要是webpack的插件 都要放入 plugins 这个数组中去 const htmlwebpackPlugin=require("html-webpack-plugin") plugins: [ new webpack.HotModuleReplacementPlugin(),这是热跟新的配置 new htmlwebpackPlugin({ 创建一个 在内存中生成 HTML页面的插件 template:path.join(__dirname,'./src/index.html'),1)">指定模板页面,将来会根据指定的页面路径,去生成内存中的 页面 filename:"index.html" 指定生成的页面名称 }) ] // 当我们使用html-webpack-plugin之后,我们不需要手动处理bundle.js的引用路径, (我们可以将index.html中的 <script src="../dist/bundle.js"></script>注释掉 ) 因为这个插件,已经帮我们自动创建一个 合适的script,并且引用了正确的路径 这个插件有两个作用的 在内存中帮我们生成一个页面 帮我们自动创建一个合适的script标签 并且引入正确的路径

运行的命令 npm run dev

 

完整代码

 package.json

{
  "devDependencies": {
    "html-webpack-plugin": "^4.5.0",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "dev": "webpack-dev-server --open --port 3000 --contentBase src --hot"
  },
  "dependencies": {
    "jquery": "^3.5.1"
  }
}

 

src下的main.js

import $ from "jquery";
$(function () {
  console.log("我是重新删除了哈");
  console.log("哈在手我的删除动阀案说现在辞职 法十分哈儿");
});

 

webpack.config.js

const path = require("path");
const htmlwebpackPlugin = require("html-webpack-plugin");
var webpack = require("webpack");
module.exports = {
  entry: path.join(__dirname, "./src/main.js"), //入口文件 使用webpack要打包哪一个文件
  output: {
    //输出相关的配置
    path: path.join(__dirname, "./dist"), //指定打包好的文件会输出到哪一个目录(dist)下去
    filename: "testindex.js", //指定打包好的文件的名称叫什么名字
  },
 
  plugins: [
    new webpack.HotModuleReplacementPlugin(), //这是热跟新的配置

    new htmlwebpackPlugin({
      //创建一个 在内存中生成 HTML页面的插件
      template: path.join(__dirname, "./src/index.html"), //指定模板页面,将来会根据指定的页面路径,去生成内存中的 页面
      filename: "index.html", //指定生成的页面名称
    }),
  ],
};

 

src下的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="/testindex.js"></script>   注释了--> 
</head>

<body>
 
    <div>12</div>
    <div>222</div>
    <div>222</div>
</body>
</html>
 

 

645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug

645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug

-

  • 前端小菜鸟,喜欢前端,不断学习
  • 微信:jie178463596
  • 微信小群:纯粹讨论技术、面试、工作为主,划水少,拒绝广告

认识Plugin


CleanWebpackPlugin


HtmlWebpackPlugin


生成的index.html分析


自定义HTML模板


自定义模板数据填充


DefinePlugin的介绍


DefinePlugin的使用


copyWebpackPlugin


目录结构


wk.config.js

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { DefinePlugin } = require('webpack'); // DefinePlugin是webpack内置插件
const copyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: "./src/main.js",
  output: {
    filename: "js/bundle.js",
    // 必须是一个绝对路径
    path: path.resolve(__dirname, "./build"),
    // assetmodulefilename: "img/[name].[hash:6][ext]"
  },
  module: {
    rules: [
      {
        // 规则使用正则表达式
        test: /\.css$/, // 匹配资源
        use: [
          // { loader: "css-loader" },
          // 注意: 编写顺序(从下往上, 从右往做, 从后往前)
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 1
            }
          },
          "postcss-loader"
        ],
        // loader: "css-loader"
      },
      {
        test: /\.less$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 2
            }
          },
          "postcss-loader",
          "less-loader"
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        // type: "asset/resource", file-loader的效果
        // type: "asset/inline", url-loader
        type: "asset",
        generator: {
          filename: "img/[name].[hash:6][ext]"
        },
        parser: {
          dataUrlCondition: {
            maxSize: 100 * 1024
          }
        }
      },
      {
        test: /\.ttf|eot|woff2?$/i,
        type: "asset/resource",
        generator: {
          filename: "font/[name].[hash:6][ext]"
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: "哈哈 webpack",
      template: "./public/index.html"
    }),
    new DefinePlugin({
      // 要包裹两层引号
      BASE_URL: '"./"'
    }),
    new copyWebpackPlugin({
      patterns: [
        {
          // to: xxx, // 不用写,默认会使用output.path
          from: "public",
          globOptions: {
            ignore: [
              "**/index.html",
              "**/.DS_Store",
              "**/abc.txt"
            ]
          }
        }
      ]
    })
  ]
}


publuc/index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <Meta charset="utf-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

build/index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <Meta charset="utf-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    
    <link rel="icon" href="./favicon.ico">
    
    <title>杰帅的webpack</title>
  <script defer src="js/bundle.js"></script></head>
  <body>
    <noscript>
      <strong>We're sorry but 杰帅的webpack doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

chainWebpack 和 htmlWebpackPlugin搭配使用

chainWebpack 和 htmlWebpackPlugin搭配使用

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


...

chainWebpack: config => {
    config
        .plugin(''html'')
            .use(HtmlWebpackPlugin)
            .tap(options  => {
                options.BASE_URL = ''sss''
                options.template = __dirname + ''/public/index.html''
                options.title = ''sssssssssssss''
            return options
            })        
    }

注意看vue-cli3引用的html-webpack-plugin默认配置

 constructor (options) {
    // Default options
    this.options = _.extend({
      template: path.join(__dirname, ''default_index.ejs''),
      templateParameters: templateParametersGenerator,
      filename: ''index.html'',
      hash: false,
      inject: true,
      compile: true,
      favicon: false,
      minify: false,
      cache: true,
      showErrors: true,
      chunks: ''all'',
      excludeChunks: [],
      chunksSortMode: ''auto'',
      meta: {},
      title: ''Webpack App'',
      xhtml: false
    }, options);
  }

 

html-webpack-plugin 详细配置

html-webpack-plugin 详细配置

下面我们介绍一下该插件的配置项。查看源码我们会发现如下代码。

this.options = _.extend({
  template: path.join(__dirname, ''default_index.ejs''),
  templateParameters: templateParametersGenerator,
  filename: ''index.html'',
  hash: false,
  inject: true,
  compile: true,
  favicon: false,
  minify: false,
  cache: true,
  showErrors: true,
  chunks: ''all'',
  excludeChunks: [],
  chunksSortMode: ''auto'',
  meta: {},
  title: ''Webpack App'',
  xhtml: false
}, options);

简单介绍一下各个含义

title:{String} 用来生成页面的 title 元素
template:{String} 源模板文件
inject:{Boolean|String} 放置js资源。true || ''head'' || ''body'' || false,如果设置为 true 或者 body,所有的 javascript 资源将被放置到 body 元素的底部,''head'' 将放置到 head 元素中。false则不会引入。
hash:{Boolean} 将添加一个唯一的 webpack 编译 hash 到所有包含的脚本和 CSS 文件,对于解除 cache 很有用
favicon:{String} 添加特定的 favicon 路径到输出的 HTML 文件中
cache:{Boolean} 只有文件修改后才会重新打包文件
minify:{Boolean|Object} true if mode is ''production'', otherwise false,
    {
      collapseWhitespace: true,//是否去除html中的空格、换行符,元素内的不会去除的
      removeComments: true,//是否去除html注释
      removeRedundantAttributes: true,//
      removeScriptTypeAttributes: true,//
      removeStyleLinkTypeAttributes: true,//
      useShortDoctype: true//
    }

我们今天的关于如何在开发中的任何路线上提供 HtmlWebpackPlugin 版本的 index.html?在开发网页之前首先应建立什么来管理网页及图像的内容的分享就到这里,谢谢您的阅读,如果想了解更多关于05webpack中html-webpack-plugin的2个作用、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、chainWebpack 和 htmlWebpackPlugin搭配使用、html-webpack-plugin 详细配置的相关信息,可以在本站进行搜索。

本文标签: