美文网首页
10.配置多个 HTML 文件

10.配置多个 HTML 文件

作者: coffee1949 | 来源:发表于2019-08-03 09:09 被阅读0次

之前我们只写了一个 html 文件,就是 src/index.html,但是有时候我们是需要多个的,这个时候,怎么办呢?

之前我们是这么做的,用了 html-webpack-plugin 这个插件来输出 html 文件。

webpack.config.js

...
new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  minify: {
    collapseWhitespace: true,
  },
  hash: true,
})
...

之前怎么做,现在还是怎么做,我们复制一下,改个名字不就好吗?

webpack.config.js

new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  minify: {
    collapseWhitespace: true,
  },
  hash: true,
}),
new HtmlWebpackPlugin({
  template: './src/contact.html',
  filename: 'contact.html',
  minify: {
    collapseWhitespace: true,
  },
  hash: true,
})

src/contact.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Contact</title>
</head>
<body>
  <p>Contact page</p>
</body>
</html>

image

image

有个问题,contact.html 使用的 js 和 css 跟 index.html 是一模一样的

如果我要让 contact.html 使用跟 index.html 不同的 js,如何做呢?(只要保证 js 不同,css 也会不同的,因为 css 也是由 js 里 import 的嘛)

那我们来改造一下:

...

module.exports = {
  entry: {
    "app.bundle": './src/app.js',
    // 这行是新增的。
    "contact": './src/contact.js'
  },
  ...
  plugins: [
    new CleanWebpackPlugin(pathsToClean),
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      minify: {
        collapseWhitespace: true,
      },
      hash: true,
      // 这行是新增的。
      excludeChunks: ['contact']
    }),
    new HtmlWebpackPlugin({
      template: './src/contact.html',
      filename: 'contact.html',
      minify: {
        collapseWhitespace: true,
      },
      hash: true,
      // 这行是新增的。
      chunks: ['contact']
    }),
    new ExtractTextPlugin('style.css')
  ],
  ...
};

上面的 excludeChunks 指的是不包含, chunks 代表的是包含。

src/contact.js

console.log('hi from contact js')

结果:

image

image

这样就 OK 了。

先说这么多。

相关文章

  • 10.配置多个 HTML 文件

    之前我们只写了一个 html 文件,就是 src/index.html,但是有时候我们是需要多个的,这个时候,怎么...

  • webpack配置多个入口文件和多个输出

    多个入口,多个出口的添加 webpack基础的配置 多个入口(html文件),多个出口的配置demo

  • webpack 配置多文件

    1 基础 webpack 入口 配置 2 配置多个HtmlWebpackPlugin 生成多个html文件 Htm...

  • Tyscript入门笔记

    gulp配置 练习配置 目录结构 ├── dist (编译后的文件夹)│ ├── html (html文件夹...

  • Nginx部署多个服务(图片、下载、Web)

    配置多个服务其实就是在配置文件中增加多个location并指定位置配置文件一般存放在: 更改配置文件如下:

  • koa-views&&ejs

    配置一(ejs文件) 配置二(html文件) 配置公共数据 引入公共的html 按照视频上的写法,但是一直报错,还...

  • spring-boot使用多个配置文件切换

    spring-boot 开发过程经常使用多个配置文件,在不同的环境下切换不同的配置。 多个配置文件下我们只在主配置...

  • Ruby yaml文件中使用环境变量ENV

    项目代码需要连接多个数据库, 数据库配置使用了自定义的yaml文件,而如果需要在yaml文件中像在html中使用<...

  • springboot2.4版本,多配置文件

    yml文件中多配置加载顺序 使用---在一个yml文件中分割多个配置,如果启用多个配置中有一样的配置项会相互覆盖,...

  • Django配置相关

    配置文件为settings.pytemplaste文件夹(存放html文件的)告诉Django去哪找html文件静...

网友评论

      本文标题:10.配置多个 HTML 文件

      本文链接:https://www.haomeiwen.com/subject/nlbddctx.html