【问题标题】:How to properly setup webpack config to include common chunks used in multiple page (and entry) app?如何正确设置 webpack 配置以包含多页面(和条目)应用程序中使用的常见块?
【发布时间】:2019-01-18 14:15:13
【问题描述】:

想象一下 html 文件的结构如下:

./home.html
./settings.html
./contact.html

还有下面的js文件

./nav.js <-- common - used in all html files
./home.js <-- used only in home.html, below follow the same rule
./settings.js
./contact.js

还有来自node_modules的一些模块:

"jquery" "moment"

在需要时被导入:

./home.js
import $ from "jquery";
(...)

我已经设置 webpack 将每个入口点作为每个文件名。现在如何在每个文件中包含常见的 js 文件,例如“./nav.js”?

entry: {
  home: "./resources/js/sections/home.js",
  settings: "./resources/js/sections/settings.js",
  (...)
} 
(...)
output: {
  filename: '[name].js',
}

// 选项A

像每个子页面中的另一个模块一样导入原始nav.jshome.jscontact.jssettings.js

import nav from ./nav.js
nav();

// 选项 B

./nav.js 创建另一个条目并手动将捆绑的 nav.js 添加到每个 html 以及其他捆绑的文件中。

entry: {
  (...)
  nav: "./resources/js/sections/nav.js"
}

【问题讨论】:

    标签: javascript webpack config chunks


    【解决方案1】:

    您可以使用HtmlWebPackPlugin 将脚本动态附加到您的 HTML 页面。

    首先安装插件:

    npm install html-loader html-webpack-plugin --save-dev
    

    然后使用配置:

    const HtmlWebPackPlugin = require("html-webpack-plugin");
    
    module.exports = {
      entry: {
        nav: './resources/js/sections/nav.js',
        home: './resources/js/sections/home.js',
        settings: './resources/js/sections/settings.js',
        contact: './resources/js/sections/contact.js',
      },
      output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'), // folder where all tranformed files will be placed
      },
      rules: [
        {
          test: /\.html$/,
          use: [{ 
            loader: "html-loader", 
            options: { minimize: true } 
          }]
        }
      ],
      plugins: [
        // create an instance of HtmlWebPackPlugin for every page of a multipage website
        new HtmlWebPackPlugin({
          template: "./resources/html/home.html", // take html from this path
          filename: "./home.html", // name it 'home.html' and insert to the root of output folder
          chunks: ['nav', 'home'] // insert dymamically nav.js and home.js to home.html
        }),
        new HtmlWebPackPlugin({
          template: "./resources/html/settings.html",
          filename: "./settings.html",
          chunks: ['nav', 'settings']
        }),
        new HtmlWebPackPlugin({
          template: "./resources/html/contact.html",
          filename: "./contact.html",
          chunks: ['nav', 'contact']
        }),
      ]
    }
    

    【讨论】:

    • 谢谢。据我所知,HtmlWebPackPlugin 将创建一个新的 html 文件,该文件将覆盖我当前的 html,其中充满了骨架 html 标记和大量内容??
    • 它将保持您的 html 文件原样,但会在 body 标签的末尾附加脚本
    • 谢谢,我想这就是这样做的方法,但我的 html 文件也是通过 php 模板化的,因此不存在 &lt;body&gt; 标签。
    • 你试过配置吗?如果没有正文,Webpack 可能会将脚本附加到 HTML 的末尾。祝你好运!
    • 您好,它确实在文件末尾输出了它,虽然不太正确,但没关系...它也不包括 home.html 与供应商共享的任何其他文件 - 我意思是vendors~homevendors-home-contact 等等。但是,嘿,我学到了一些新东西:)
    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多