Gre*_*nov 3 handlebars.js webpack html-webpack-plugin
是否可以通过html-webpack-plugin加载.json文件并将其传递给我的车把文件?
我目前的设定
const path = require('path');
const fs = require('fs');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const extractCss = new ExtractTextPlugin({
filename: './css/app.css'
});
const pages = fs
.readdirSync(path.resolve(__dirname, 'src/hbs/pages'))
.filter(fileName => fileName.endsWith('.hbs'));
module.exports = {
context: path.resolve(__dirname, "src"),
entry: './js/main.js',
output: {
path: path.resolve(__dirname, './build'),
filename: 'js/app.js',
},
...
...
plugins: [
new CleanWebpackPlugin(['build']),
extractCss,
...pages.map(page => new HtmlWebpackPlugin({
template: 'hbs/pages/'+page,
filename: page
}))
],
module: {
rules: [
//babel-loader
{
test: /\.js$/,
include: /src/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['env']
}
}
},
...
...
//handlebars-loader
{
test: /\.(hbs)$/,
loader: "handlebars-loader",
query: {
helperDirs: [
__dirname + "/hbs/helpers"
]
}
}
]
}
};
Run Code Online (Sandbox Code Playgroud)
我想拥有一个包含list1.json,list2.json ...这样的文件的文件夹,它将非常庞大,因此我不会弄乱我的.hbs文件。
就像是
...pages.map(page => new HtmlWebpackPlugin({
template: 'hbs/pages/'+page,
filename: page,
data: myData.json
}))
Run Code Online (Sandbox Code Playgroud)
会很好
欢呼,格雷戈尔;)
小智 5
您可以使用html-webpack-plugin的templateParameters选项:
new HtmlWebpackPlugin({
templateParameters:require('path/to/data.json')
}),
Run Code Online (Sandbox Code Playgroud)
templateParameters:允许覆盖模板中使用的参数
| 归档时间: |
|
| 查看次数: |
1023 次 |
| 最近记录: |