posthtml / posthtml-css-modules

Use CSS modules in HTML
MIT License
54 stars 5 forks source link

Due to repair problems caused by Gulp require cache in the watch can … #5

Closed aemoe closed 8 years ago

aemoe commented 8 years ago

Thanks for the author, thank you for your code. When I use a gulp when using the watch mode, because of caching issues require cache, and update the json time, can not be read to json modified files, so I can not do it automatically updates results, so modified the code I hope you fixed.

maltsev commented 8 years ago

Thank you for your PR! Unfortunately, I cannot merge it as it's yet, because it's inefficient to invalidate the cache by each tree.match() iteration. I think it'd be better to purge the cache once before running tree.match(). Can you make the proper changes?

aemoe commented 8 years ago

@maltsev thank you for your advise,i will try to find a better way that delete the cache of require.When I find, I will commit again.

aemoe commented 8 years ago

@maltsev See also,yesterday I use an other way to resolve this bug, it use Node file stream. Cause of the file load every times, the cache will refresh. Shortly after,i will submit. What do you think that this way whether to resolve this bug? thx~ ~

aemoe commented 8 years ago

IMG fixed~ ~ test all pass~

maltsev commented 8 years ago

The issue is still here: by each iteration a file with CSS modules is being re-read and re-parsed. I was thinking about using some simple cache. Something like this:

var cssModulesCache = {};
function getCssModules(cssModulesPath) {
    var fullPath = require.resolve(cssModulesPath);
    if (! cssModulesCache[fullPath]) {
        delete require.cache[fullPath];
        cssModulesCache[fullPath] = require(fullPath);
    }

    return cssModulesCache[fullPath];
}

// To purge the cache insert cssModulesCache = {}; before the tree.match()
maltsev commented 8 years ago

I fixed this bug and published the new version. @aemoe please check if everything works fine for you.

aemoe commented 8 years ago

thank for fixed this bug~