HTML Webpack Plugin插件 打包生成 index.html

         最近在学习用vue-cli创建项目, 对入口文件main.js如何挂载到#app产生了兴趣. 

        public目录下有个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>

最开始以为main.js将根组件App挂载到这里, 后来将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">
    <title>Vue App</title>
  <link href="/js/app.js" rel="preload" as="script"><link href="/js/chunk-vendors.js" rel="preload" as="script"></head>
  <body>
    <div id="app"></div>
  <script type="text/javascript" src="/js/chunk-vendors.js"></script><script type="text/javascript" src="/js/app.js"></script></body>
</html>

这个代码应该是后生成的, 跟原来的index.html相比增加了script标签, 经过搜索发现应该是HTML Webpack Plugin插件起作用了. 查询html-webpack-plugin文档html-webpack-plugin - npm

发现它有很多配置项, 跟此有关的配置项如下:

filename属性,可以用于配置生成的HTML文件的文件名,默认为:index.html;

template属性, 指定一个路径比如: template: './src/public/index.html', 基于这个路径下的html文件生成打包输出的新html文件

templateContent, 指定模板内容, 用于替换template属性, 

        目前来看, vue-cli构建的项目使用了templateContent属性, 并且指定的模板中有id="app"这样的代码.

        为了验证用webpack构建了一个简单项目.

将原先public目录下的index.html删除

再修改webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin')
const htmlPlugin = new HtmlWebpackPlugin({
    // template: './src/public/index.html',
    filename: 'index.html',
    templateContent: `
        <html>
        <body>
            <div id="app"></div>
        </body>
        </html>
    `
})

取消基于./src/public/index.html生成打包的html文件, 而是通过templateContent指定模板并将由Vue管理的#app也写在这里.

index.js

import $ from 'jquery';
import './css/1.css';
import App from './components/App.vue';
import Vue from 'vue';

$(function() {
    $('li:odd').css('backgroundColor', 'orange');
    $('li:even').css('backgroundColor', 'darkblue');
});

class Person {
    static info = 'aaa';
}

const vm = new Vue({
    // el: '#app',
    render: h => h(App)
}).$mount('#app');

console.log(Person.info);
console.log(App);

App.vue

<template>
    <div>
        <h1>这是 APP 根组件</h1>
    </div>
</template>

<script>
    export default {
        data(){
            return {};
        }
    }
</script>

<style scoped>
    h1 {
        color: red;
    }
</style>

通过浏览器查询源码


        <html>
        <body>
            <div id="app"></div>
        <script type="text/javascript" src="./bundle.js"></script></body>
        </html>

由此可以得出结论:

由Vue-cli构建的项目不需要index.html文件, 可以通过webpack的插件html-webpack-plugin通过配置属性而打包生成.

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果在 Webpack 打包后的 `index.html` 页面中暴露了 IP 地址,可能是因为在项目配置中使用了错误的配置或插件。为了修复这个问题,你可以尝试以下几个方法: 1. 使用 HtmlWebpackPlugin 插件:在 Webpack 配置文件中,确保你使用了 HtmlWebpackPlugin 插件,并配置了正确的选项。在 `plugins` 部分中添加以下代码: ```javascript const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { // ... plugins: [ new HtmlWebpackPlugin({ // 设置生成HTML 文件中的标题、模板和文件名等选项 }), ], // ... }; ``` 请确保正确设置了 `title`、`template` 和 `filename` 等选项,以避免暴露 IP 地址。 2. 检查 publicPath 配置:在 Webpack 配置文件中,检查 `output` 部分的 `publicPath` 配置。确保将其设置为合适的值,以避免暴露 IP 地址。例如,将其设置为 `/` 或相对路径。 ```javascript module.exports = { output: { // ... publicPath: '/', }, // ... }; ``` 3. 使用 devServer 配置:如果你正在使用 Webpack Dev Server 进行开发,确保在配置文件中正确设置了 `devServer` 部分。检查是否使用了 `public` 或 `host` 等选项,并将其设置为适当的值。 ```javascript module.exports = { // ... devServer: { // ... public: 'your-domain.com', // or // host: 'your-domain.com', // ... }, // ... }; ``` 确保将 `your-domain.com` 替换为你正确的域名或 IP 地址。 4. 检查其他配置项:检查 Webpack 配置文件中的其他可能影响 `index.html` 页面的配置项,例如 `mode`、`optimization` 等。确保这些配置项不会暴露 IP 地址。 如果尝试了以上方法后问题仍未解决,建议仔细检查你的 Webpack 配置文件和相关依赖,确保没有其他错误导致 IP 地址暴露在生成的 `index.html` 页面中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值