Closed 5ulo closed 1 year ago
Hello @5ulo
thanks for the question.
The HtmlBundlerPlugin was implemented independently from webpack-handlebars-plugin and has own options these must not be 100% compatible with webpack-handlebars-plugin options.
The data
option is not a filename because it doesn't make sense:
data: require('/path/to/file.json')
How often do you change the json file? If 1-2 times, you can stop/restart Webpack.
If to reload the json file after each change, it need to reinitialize the plugin and the Handlebars engine, which slows things down.
Yeah that's the problem as the json holds data I change while I develop website. It's like dynamic database for me. For example creating new section - I just type data into json and they immediately show up in template. No need to stop/run whole webpack. The way you descibed (data required once) was the same functionality by handlebars-loader
so then I moved on to webpack-handlebars-plugin
. Now I was looking forward to use bundler and this one thing stopped me.
edit: for imagination what my worflow is - I build html templates dynamically, so I write json data while I write hbs and make loops etc..
I'm not a webpack plugins programmer so I am curious why reinitialize plugin and hbs engine. My idea was to check the json path before compilation and update the data. I found this in the webpack-handlebars-plugin
src https://github.com/sagold/handlebars-webpack-plugin/blob/master/index.js:
/**
* (Re)load input data for hbs rendering
*/
updateData() {
if (this.options.data && typeof this.options.data === "string") {
try {
const dataFromFile = JSON.parse(this.readFile(this.options.data));
this.addDependency(this.options.data);
this.data = dataFromFile;
} catch (e) {
console.error(`Tried to read ${this.options.data} as json-file and failed. Using it as data source...`);
this.data = this.options.data;
}
} else {
this.data = this.options.data;
}
}
and the the update()
is called right at the top of compile:
compileAllEntryFiles(compilation, done) {
this.updateData();
@5ulo
It's like dynamic database for me.. I just type data into json and they immediately show up in template
Thanks for clarifying. Yes, then it makes sense to do a dynamic data update in the template.
I will to add new dataFile
option for dynamically update data after changes.
Cool feature. Thank you for the useful feature request.
I note this feature on the TODO list for one of the next releases (1-2 weeeks).
data: path.join(PATH_SRC, 'data.json'),
works like a charm!! you made my day :)
Feature request
What is motivation or use case for adding/changing the behavior?
I'm migrating from HandlebarsPlugin to HtmlBundlerPlugin and I can't figure out how to pass data from data.json file to
data
entry.data
accepts onlyobject | null
and does not accept path to json.Describe the solution you'd like
Simple:
Describe alternatives you've considered
ATM the only alternative is to stick with
webpack-handlebars-plugin
where the configuration looks like this:file data.json is also watched an works fine with HMR My current try with
HtmlBundlerPlugin
is this:data.json
webpack.config.js