Biz*_*zet 3 javascript node.js webpack webpack-dev-server pug
这似乎是一个业余问题,但我无法使用 Webpack 创建其他页面
这是我的 webpack.config.js 文件
var HtmlWebpackPlugin = require('html-webpack-plugin')
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var webpack = require('webpack')
var path = require('path')
var isProd = process.env.NODE_ENV === 'production' // true or false
var cssDev = ['style-loader', 'css-loader', 'sass-loader']
var cssProd = ExtractTextPlugin.extract({
fallback: 'style-loader',
loader: ['css-loader', 'sass-loader'],
publicPath: '/dist'
})
var cssConfig = isProd ? cssProd : cssDev
module.exports = {
entry: {
app: './src/app.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js'
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
use: 'eslint-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: cssConfig
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.pug$/,
use: 'pug-loader'
}
]
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
hot: true,
open: true
},
plugins: [
new HtmlWebpackPlugin({
title: 'Project Demo',
hash: true,
alwaysWriteToDisk: true,
template: './src/index.pug'
}),
new HtmlWebpackHarddiskPlugin(),
new ExtractTextPlugin({
filename: 'app.css',
disable: !isProd,
allChunks: true
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
]
}
Run Code Online (Sandbox Code Playgroud)
基本上,我的配置文件当前使用HtmlWebpackPlugin到输入我的index.pug从src文件夹,然后输出 index.html到所述dist文件夹中。
但是,如果我想创建不同的页面怎么办?例如,关于页面。
我知道如果我about.pug在我的src文件夹中创建一个,它不会被 Webpack 处理,因为我已经将它包含在我的配置文件中。但是,我不确定如何实现这一点。
我怎么能输入不同的 pug 文件,例如about.pug gallery.pug contact.pug在我的src文件夹中,然后将所有这些输出放在我的dist文件夹中about.html gallery.html contact.html
正如@Derek 所提到的,您可以以编程方式生成HtmlWebpackPlugin实例列表。
const path = require('path')
const fs = require('fs')
// Store .html file names in src/ in an array
const pages =
fs
.readdirSync(path.resolve(__dirname, 'src'))
.filter(fileName => fileName.endsWith('.html'))
module.exports = {
// Use .map() on the array of file names to map each file name
// to the plugin instance, then spread the mapped array into
// the plugins array.
plugins: [
...pages.map(page => new HtmlWebpackPlugin({
template: page,
filename: page
}))
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4958 次 |
| 最近记录: |