Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

HMR isn't triggered when changing CSS files included via @import #723

Closed
mummybot opened this issue Aug 12, 2017 · 3 comments
Closed

HMR isn't triggered when changing CSS files included via @import #723

mummybot opened this issue Aug 12, 2017 · 3 comments

Comments

@mummybot
Copy link
Contributor

mummybot commented Aug 12, 2017

When using any preprocessor, if I change a CSS file that is included via @import then HMR doesn't pick up that a file has changed. If I save the initial CSS file that was in the JS then I can see the changes in the included file.

header.css

@import "shared/core.css"; /* Changes to core.css won't be picked up until header.css  is saved. */

header {
/* Changes here will update via HMR as this file is included via JSX */

I have a basic project with a nested CSS file and folder structure. I have tried using PostCSS and many import plugins, as well as converting everything to SASS using node-sass. When I run fusebox for the first time, the CSS is correctly compiled. If I change a CSS file which is directly imported by the JS then HMR works as expected. If I modify a CSS file included via @import then HMR doesn't automatically update the page.

//fuse.js
const { FuseBox, WebIndexPlugin, CSSPlugin, BabelPlugin, CopyPlugin, PostCSSPlugin } = require("fuse-box");

const fuse = FuseBox.init({
    homeDir: "src",
    output: "dist/$name.js",
    cache: true,
    plugins: [
        [
        PostCSSPlugin([
            require("postcss-import")({
                path: ["src"]
            })
        ]),
        CSSPlugin()],
        BabelPlugin(),
        WebIndexPlugin({
            template: "src/index.html"
        })
    ]
});

fuse.dev({
    root: 'dist',
    port: 8080
});

fuse.bundle("vendor")
    .instructions(`~index.jsx`);

fuse.bundle("app")
    .instructions(`!>[index.jsx]`)
    .watch("src/**").hmr();

fuse.run();

FYI This is a problem for a lot of CSS @import libraries in Webpack as well. Webpack provides the functionality for library owners to addDependencyTo to pass the files to Webpack that should be watched.

webpack-contrib/postcss-loader#8
postcss/postcss-import#233

    //webpack.config.js
    var postcssImport = require('postcss-import');
    ...
    postcss: function(webpack) {
        return [
            postcssImport({ addDependencyTo: webpack }), // Must be first item in list
            precss,
            autoprefixer
        ];
    },
@mummybot mummybot changed the title HMR isn't triggered when changing a CSS files included via @import HMR isn't triggered when changing CSS files included via @import Aug 12, 2017
@nchanged
Copy link
Contributor

hi @mummybot and you are using the latest beta right?

It happens because of this block:

const lastFile = file.context.convertToFuseBoxPath(bundle.lastChangedFile);
if (isStylesheetExtension(bundle.lastChangedFile) && lastFile === file.info.fuseBoxPath) {
emitRequired = true;
}

We should find a solution, if you have any ideas, just let me know

@nchanged
Copy link
Contributor

Implemented in fuse-box@2.2.3-beta.1 for SassPlugin and PostCSS
http://fuse-box.org/plugins/postcss-plugin#paths-for-hmr

SassPlugin doesn't require any configuration, PostCSS would need it if you are planning on doing magical imports

@mummybot
Copy link
Contributor Author

Hey guys, this is awesome (just revisiting this now after a project hiatus). Thanks very much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants