Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack doesn't recompile in watch mode when a partial template is changed. #15

Open
Todd-Fulton opened this issue Apr 15, 2020 · 9 comments
Labels
bug Something isn't working Hacktoberfest help wanted Extra attention is needed

Comments

@Todd-Fulton
Copy link

In html-webpack-plugin the main.html file will recompile when changed. They fixed this problem in this issue, I think with this commit, by adding the template file to the compilation dependencies.That was in 2015, so idk if there have been api changes or whatnot, but as far as my searching has gone, I think this might be where I'm having trouble right now with both webpack watch and webpack-dev-server not recompiling as I try to edit some partial templates.

@colbyfayock
Copy link
Owner

yeah this is been an issue because I haven't found a 1st class way to hook into the html-webpack-plugin pipeline early enough to support that kind of thing. right now, I'm able to achieve this by injecting the html as a string into the already compiled code. I'm open to suggestions if you're familiar enough with this, or even if you wanted to give it a shot. that might also help solve the issue here: #7

@Todd-Fulton
Copy link
Author

I'll try; though, I'm pretty new to javascript and webpack, will probably take a while (need to read all the code and some docs first) and I might not come up with a "1st class" way to do it, but it'll be a good learning experience either way.

@colbyfayock
Copy link
Owner

That would be great! I just haven't found the time lately to do a deep dive. I'd be more than happy to help answer questions or even brainstorm if you end up trying to work through, just let me know :)

@colbyfayock colbyfayock added bug Something isn't working help wanted Extra attention is needed labels Jun 23, 2020
@toabm
Copy link

toabm commented Dec 10, 2020

Hi guys, I am on the very same situation, can't figure out how to do it, but it is quite annoing having to re-compile every time you change some of the partials.... Is there anyway I could help with this issue?

@colbyfayock
Copy link
Owner

hey @toabm does this solve the issue for you? #33 (comment)

@tarponjargon
Copy link

tarponjargon commented Feb 25, 2021

Here's a quick solution for wp5:

...
plugins: [        
	apply(compiler) {
	  compiler.hooks.afterCompile.tap("Custom watcher", (compilation) => {
		[`${src}/partials`, `${root}/config`].forEach((path) =>
		  compilation.contextDependencies.add(path)
		);
	  });
	},
        ...
]

@rowild
Copy link

rowild commented Sep 2, 2021

@tarponjargon The syntax of your "apply" snippet causes an error in my setup right in front of the first curly bracket ("unexpected token"). Is that really the proper setup?

@chrisszeluga
Copy link

@rowild If you haven't solved this yet (or for anyone else in the future...) -- The snippet @tarponjargon mentioned is really just following the Webpack plugin syntax https://webpack.js.org/contribute/writing-a-plugin/#basic-plugin-architecture

You could separate this plugin out into another file, following the guide above, or just include the class in the webpack config file.

...
class WatchPartials {
  apply(compiler) {
    compiler.hooks.afterCompile.tap("Custom watcher", (compilation) => {
      [`${paths.src}/pages`].forEach((path) =>
        compilation.contextDependencies.add(path)
      );
    });
  }
}

module.exports = {
  ...

  plugins: [new WatchPartials()],
}

Just replace ${paths.src}/pages with your directory, of course.

@buendias-dev
Copy link

buendias-dev commented Jun 16, 2023

You can use webpack-watch-external-files-plugin

const WatchExternalFilesPlugin = require('webpack-watch-external-files-plugin');
...
  plugins: [
    new WatchExternalFilesPlugin({
      files: [
        './src/partials/**/*'
      ]
    }),
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants