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
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值