sass-extract-loader icon indicating copy to clipboard operation
sass-extract-loader copied to clipboard

how to extract variables from files imported from node_modules?

Open briancappello opened this issue 8 years ago • 8 comments

Just started playing around with this, seems pretty cool! One problem I ran into however, as the title states:

file: _variables.scss

@import "~some-vendor/scss/_vars.scss";

$foo-bar: 5;

file: styles.js

const variables = require('sass-extract-loader?{"includePaths":["node_modules"]}!./_variables.scss')

This does not work :(

ERROR in ./node_modules/sass-extract-loader?{"includePaths":["node_modules"]}!./frontend/app/styles/_variables.scss
Module build failed: Error: File to import not found or unreadable: ~skeleton-scss/scss/base/_variables.scss.

However, if I remove the tilda from the @import statement it does work (but i believe that breaks the scss, no?)

Am I missing anything, or might this be a bug?

briancappello avatar Oct 09 '17 03:10 briancappello

@briancappello thanks for the report. The problem is that node-sass uses a custom loader to let webpack handle all the imports (stuff like ~ and so on), while sass-extract also uses a custom loader to be able to extract variables over imports. This means it won't be able to handle the special webpack imports in the sass files. I will have to do some work to bridge these two mechanisms together so that it wont break node-sass imports. I'm currently working on some fixes in the core library, will have a look at this next.

jgranstrom avatar Nov 13 '17 11:11 jgranstrom

There's an example in the docs:

import colors from 'sass-extract-loader!@node_modules/my-module/colors.scss'

@node_modules is an alias I added to my Webpack config.

adi-works avatar Jan 24 '18 16:01 adi-works

Any progress on this? Ran into this problem today.

RyanMulready avatar Jul 03 '18 15:07 RyanMulready

Any progress on this? Ran into this problem today.

Same here.

osvaldasvalutis avatar Jan 09 '19 09:01 osvaldasvalutis

Using relative paths for importing 3rd-party sass-variables seems to works fine.

Example:

variables.scss

@import '../../../node_modules/@blueprintjs/core/src/common/variables';
// other imports, local styles etc

app.styles.js

export const theme = require('sass-extract-loader?{"plugins": ["sass-extract-js"]}!../sass/variables.scss');

/* hack-hack */

export const GlobalStyles = createGlobalStyle`
    .app {
      background: ${theme.ptDarkAppBackgroundColor};
    }
`;

I guess, the problem is that when we do imports in scss-files, it's processed by sass-loader. And as a follows, sass-loader takes care about tilde (~) to resolve it is an node_modules import. But there is no sass-loader and tilde couldn't be resolved. Take a look for sass-loader similar issue, I believe it may be fixed by proper Webpack resolving configuration.

nickensoul avatar Jan 18 '19 12:01 nickensoul

@adi518, seems like your solution is a bit differ with @briancappello's question. Yep, we can import variables to provide access to them (or accumulate) in js-modules directly. In particular, if we use them in js-files only. But in my case I need to re-define default sass variables from 3rd-party components and then export them to the styled-components and there others.

nickensoul avatar Jan 18 '19 12:01 nickensoul

Any progress? It's already more than a year.... The proposed workarounds aren't valid, as npm module resolution needs to do his work, and a node module could be installed higher up....

dietergeerts avatar May 17 '19 12:05 dietergeerts

To add to this, webpack aliases are also not supported.

deleemillie avatar Aug 15 '19 04:08 deleemillie