question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage in production and includePaths

See original GitHub issue

Hey! I have two questions:

First I have a hard time thinking about if and how to use this hook in production. I am using webpack with css modules for the client side and this require hook on the server side when doing server side rendering of react components. In my production environment I am planning to use ExtractTextPlugin to extract all the css. Currently we are still in development.

What I notice so far is that the require hook is setting the correct classes but it does not include any generated css in the server rendered templates. So I actually get flashes of unstyled content until client side (webpack) picks up, which then inlines the styles into the html. Is this expected behaviour? Also are there any performance hogs to be expected in production (compiling scss in runtime sounds like it). What is best practice here for setting up this hook for both development and production?

second question:

I have lots of components and lots of scss files. In these files I am importing most of the times other .scss files such as variables and mixins from a shared folder. Webpack allows a configuration for modulesDirectories, similar to this hook’s includePaths.

The problem is, that with webpack I have to prepend imports from these custom directories with a tilde. e.g.: @import '~styles/variables'; instead of @import '../../../../styles/variables';

The tilde prefix breaks this require hook, here it would work with just @import 'styles/variables'; Any idea how to solve this?

Here is my current setup:

const cssRequireHook = require('css-modules-require-hook');
const sass = require('node-sass');
const path = require('path');
const nameScope = process.env.NODE_ENV === 'test' ? '[local]' : '[name]__[local]___[hash:base64:5]';

cssRequireHook({
    generateScopedName: nameScope,
    extensions: ['.scss', '.css'],
    preprocessCss: (data, filename) => sass.renderSync({
        data,
        file: filename,
        includePaths: [path.resolve(__dirname, '../../client')]
    }).css
});

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sullenorcommented, Jun 2, 2016

@tmaximini @wuct sorry guys for the late call. I’ll try to answer according to my experience.

I don’t see any problem to use it in production, since it extends the default require function. By default Node.js caches all the require calls, so it will be slow on the start (since you need to extract tokens from all the require calls). I think, I can boost it a little better in future by caching all the calls betweens sessions.

What I notice so far is that the require hook is setting the correct classes but it does not include any generated css in the server rendered templates. So I actually get flashes of unstyled content until client side (webpack) picks up, which then inlines the styles into the html. Is this expected behaviour?

Can you describe the problem a bit more detailed?

Talking about second question. Currently, there is no support for ~ in paths, as you mentioned, but if all such calls take place in CSS it should be possible to support. I’m not familiar with sass, but with postcss it can be solve easily. Unfortunately in that case you’ll have to preprocess all the files three times — with postcss, sass and postcss again. Presumably that problem can be solved with sass also. Looks like importer option provides possibility to add some heuristic to the path resolving mechanic. Have you tried it?

Talking about webpack resolve.alias — I do see any good possibility to add it currently. Still you may checked the NODE_PATH global variable. It doesn’t provide any possibility to rename modules, but it allows you to add the custom folders in which modules will be looked for.

1reaction
tmaximinicommented, Jun 1, 2016

From my experience so far:

  1. It is definitely okay to use this hook in production. You can check the actual compile times when you run your server with DEBUG=css-modules:*. This shows that the first render is actually pretty slow as all the css modules are compiled in run-time but all subsequent requests are being served from cache and so we decided it is feasable to run this in production.

  2. As modulesDirectories is a webpack feature I found it to be not possible to do server-side rendering of css-modules using this feature, and so our team decided to always use relative paths for requiring styles.

Hope that helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

includePaths: ['node_modules'] doesn't work for scoped ...
sass-loader uses the concept that ~ without a slash is a lib import whereas ~/ is the home directory. That seems logical to...
Read more >
What does Gulp's includePaths do? - sass - Stack Overflow
The SASS compiler uses each path in loadPaths when resolving SASS @imports. loadPaths: ['styles/foo', 'styles/bar'] @import "x"; ...
Read more >
sass-loader - webpack - JS.ORG
The sass-loader uses Sass's custom importer feature to pass all queries to the Webpack ... if (relativePath === "styles/foo.scss") { return { includePaths: ......
Read more >
node-bourbon | Yarn - Package Manager
Basic Usage · with() Function · includePaths Property · Stylesheet usage.
Read more >
libc/include/paths.h - platform/bionic - android Git repositories
may be used to endorse or promote products derived from this software ... #define _PATH_DEFPATH "/product/bin:/apex/com.android.runtime/bin:/apex/com.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found