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

can not reload css when scss changed in dev server hot #348

Open
lichnow opened this issue Jul 16, 2018 · 12 comments
Open

can not reload css when scss changed in dev server hot #348

lichnow opened this issue Jul 16, 2018 · 12 comments
Labels
Feature New Feature

Comments

@lichnow
Copy link

lichnow commented Jul 16, 2018

in dev server environment sass file also be extract to a css file which imported in main.js.so , it can not been hoted in dev server. can use only sass-loader,style-loader to solve it in dev server enviroment in encore,or let us to configure it self by add a method isDevServer to check if in dev-server?

@weaverryan weaverryan added the Feature New Feature label Jul 28, 2018
@weaverryan
Copy link
Member

Yea, that's correct - we always extract to a CSS file, which kills HMR. We did this on purpose - because we wanted to consistently output CSS files (not have different behavior between prod & dev environments). I've always wanted to add HMR for extracted CSS files, but it's always been a "todo" in the core webpack libs. For example, HMR is listed as a "TODO" on the https://github.com/webpack-contrib/mini-css-extract-plugin that we're using in the next version.

One option is to add a method to "opt in" to not extracting to CSS when in dev-server mode (and just use the style loader). That way, for people who know what they're doing and want HMR, they can opt into this.

@FullStackAlex
Copy link

Hey Ryan,
what do you mean by "One option is to add a method to "opt in" to not extracting to CSS when in dev-server mode (and just use the style loader). That way, for people who know what they're doing and want HMR, they can opt into this."?

Is this anywhere documented? Where I have to set this option?

@FullStackAlex
Copy link

FullStackAlex commented Aug 5, 2018

Why does this package not work with Encore?
https://github.com/shepherdwind/css-hot-loader

@Lyrkan
Copy link
Collaborator

Lyrkan commented Aug 6, 2018

@tech-nomad What @weaverryan meant is that a method could be added to Encore to allow people not to use the extract-text-webpack-plugin/mini-css-extract-plugin when using --dev-server. That method doesn't exist yet.

AFAIK you should be able to use css-hot-loader with Encore, but:

  • There's no easy way to add it (see the comments in Add HMR / Hot Support #3... which probably won't work if you use the Webpack 4 version of Encore)
  • You'll have to make sure that the versioning is not enabled when using it since the plugin requires that the filenames always stay the same

@FullStackAlex
Copy link

FullStackAlex commented Aug 6, 2018

Thanks for the info @Lyrkan !
This feature is very important for my development workflow. So maybe it's less stress and work just to use original webpack for my Symfony project in this case?

@beany-vu
Copy link

I do agree with the @tech-nomad's opinion, the "live reload" feature or HMR should be available.

@AubreyHewes
Copy link

AubreyHewes commented Feb 1, 2019

@tech-nomad and all, as a workaround / until it is an option. In my projects I add the following to the webpack.config.js to enable hot style reloading in development. I accept the fact that it is not production css.

...

const cfg = Encore.getWebpackConfig();

if (process.env.NODE_ENV === 'development') {
  // allow HMR of scss/css changes
  // https://github.com/symfony/webpack-encore/issues/348#issuecomment-408631993
  const path = require('path');
  cfg.module.rules = cfg.module.rules.map(rule => {
    if (rule.use && rule.use[0] === path.resolve(__dirname, 'node_modules/mini-css-extract-plugin/dist/loader.js')) {
      rule.use[0] = require.resolve("style-loader");
    }
    return rule;
  });
}

module.exports = cfg;

This changes both the css and scss/sass module rules to use style-loader.

This requires prepending the dev-server script in the package.json
with NODE_ENV=development

    "dev-server": "NODE_ENV=development encore dev-server --hot",

And requires adding style-loader to the dependencies

@Naskalin
Copy link

Naskalin commented Feb 25, 2019

@AubreyHewes Thanks for the solution, just add, it also works for me without updating package.json if you will check !Encore.isProduction() instead process.env.NODE_ENV === 'development'

if (!Encore.isProduction()) {
    //your code is here...
}

weaverryan added a commit that referenced this issue Mar 25, 2019
…kan)

This PR was merged into the master branch.

Discussion
----------

Add Encore.disableCssExtraction() to the public API

This PR adds an `Encore.disableCssExtraction()` method that allows to disable the `mini-css-extract-plugin` and use the `style-loader` instead.

It can be used to solve various problems that, until now, required a really ugly workaround that relied on our internal implementation (for instance the following commit probably broke some builds that used previous versions of it: 6867443#diff-8beacd21a12ca072bafa4e8e3f1aae6b).

Related issues: #3, #256, #348, #527

Commits
-------

347feed Add Encore.disableCssExtraction() to the public API
@Grafikart
Copy link
Contributor

I think this issue can now be close since the commit #539 introduced the requested feature

if (!Encore.isProduction()) {
  Encore.disableCssExtraction()
}

Then, the command

./node_modules/.bin/encore dev-server --hot

Works as expected

@Kocal
Copy link
Contributor

Kocal commented Sep 17, 2019

Maybe this can be added to the webpack.config.js file from the recipe?

@Lyrkan
Copy link
Collaborator

Lyrkan commented Sep 17, 2019

Maybe this can be added to the webpack.config.js file from the recipe?

I'd prefer to merge #564 since it should then work natively with the mini-css-extract-plugin, but sadly it's still blocked because of inconsistent hashes :(

Anyway, if we do add Encore.disableCssExtraction() to the recipe, maybe the check should be on Encore.isDevServer() instead of !Encore.isProduction()?

@Kocal
Copy link
Contributor

Kocal commented Sep 17, 2019

Yes, I was thinking about using Encore.isDevServer()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature
Projects
None yet
Development

No branches or pull requests

9 participants