Closed kuworking closed 4 months ago
I generally don't use the html-webpack-plugin
on multi-page sites.
I simply use https://github.com/shellscape/webpack-manifest-plugin to generate a manifest.
and a small node function to drop the optimized asset into my templates.
// src/_manifest.json (generated by the manifest plugin)
{
"main.js": "/main.cde180c7fa03d44900ac.js",
"style.css": "/style.cbbc8fc8b3316cdac96d.css",
}
// utils/shortcodes.js
const fsp = require('fs').promises
const path = require('path')
const webpackAsset = async name => {
const manifestData = await fsp.readFile(
path.resolve(__dirname, '../src/_manifest.json'),
)
const manifest = JSON.parse(manifestData)
return manifest[name]
}
module.exports = {
webpackAsset,
}
// .eleventy.js
const shortcodes = require('./utils/shortcodes')
module.exports = config => {
// 11ty can use files set in .gitignore. Needed for webpack manifest
config.setUseGitIgnore(false)
// Copy assets folder into build - https://www.11ty.dev/docs/copy/#how-passthrough-file-copy-handles-input-directories
config.addPassthroughCopy("src/templates/assets")
// Add shortcodes - https://www.11ty.dev/docs/shortcodes/ (import my shortcodes for use with eleventy)
for (const [name, func] of Object.entries(shortcodes)) {
config.addShortcode(name, func)
}
return {
dir: {
output: 'build',
input: 'src/templates',
data: '../data',
includes: '_includes',
layouts: '_layouts',
},
"dataTemplateEngine": false
}
}
...
<!-- Generic Base Template -->
<link rel="stylesheet" href="{% webpackAsset 'style.css' %}" />
<script src="{% webpackAsset 'main.js' %}"></script>
This way you can run webpack --watch
and your eleventy --serve at the same time with no hassle.
First of all, many thanks for answering :)
As far as I understand, your code is actually equivalent to using html-webpack-plugin
in the sense that both files build a manifest-like file
I mean, I (think I) could use html-webpack-plugin
in a similar manner than you're using webpack-manifest-plugin
(better said, this is what I'm currently doing)
The difference as I see it is that if you are using shortcodes
, then it does work
But if you are using jsx
imports with the react/render
method, then it does not work, regardless of how you have created the manifest
From your post I do realize the problem is not in generating a webpack file too soon (and a webpack problem), but in the way jsx
is integrated in eleventy
Unfortunately this issue slipped under the radar—I’m finally circling back through some of the old issues on the tracker but I’d love a confirmation that this old one is still relevant before I start debugging! If I don’t hear anything in the next two weeks, I’ll likely close it as stale—sorry (and thank you!)
Hi @zachleat this is no longer relevant (for me), so feel completely free to close it. In any case, thanks for your dedication!
Thank you!
I am using
webpack
to transpile js, css, ... and to add hashes to these filesTo be able to use those files, I am using
html-webpack-plugin
to create ajson
file with the final namesThen I am using this json file in
jsx
files (templates based on https://github.com/signalkuppe/eleventy-react) where I justimport
it to read it and to inject the script through<script dangerouslySetInnerHTML={{ __html: xxxx }}></script>
This works if I
build
the site, and it also works if Iserve
the site without--watch
and--parallel
But if I use both properties
Then the file created by
html-webpack-plugin
is not yet there and the process gives an error (of not finding the file)If I don't delete the file between runs, the 2nd run works since it does find the file there
I am wondering whether there is something I can do about it, something like making
11ty
to delay its first execution, to give time towebpack
to write these things