如何正确设置 webpack 配置以包含多页面(和入口)应用程序中使用的公共块?
How to properly setup webpack config to include common chunks used in multiple page (and entry) app?
假设 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.js
(home.js
, contact.js
, settings.js
)
import nav from ./nav.js
nav();
// 选项 B
为 ./nav.js
创建另一个条目,并手动将捆绑的 nav.js
添加到每个 html 以及其他捆绑文件。
entry: {
(...)
nav: "./resources/js/sections/nav.js"
}
您可以使用 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']
}),
]
}
假设 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.js
(home.js
, contact.js
, settings.js
)
import nav from ./nav.js
nav();
// 选项 B
为 ./nav.js
创建另一个条目,并手动将捆绑的 nav.js
添加到每个 html 以及其他捆绑文件。
entry: {
(...)
nav: "./resources/js/sections/nav.js"
}
您可以使用 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']
}),
]
}