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

Variables undefined when use sass-loader.options.data attribute #36

Open
netstuff opened this issue Aug 21, 2019 · 19 comments
Open

Variables undefined when use sass-loader.options.data attribute #36

netstuff opened this issue Aug 21, 2019 · 19 comments

Comments

@netstuff
Copy link

netstuff commented Aug 21, 2019

Hello! I use sass-vars-loader with Vue Cli 3 and I try to move common scss-files to app config:

const path = require('path');

module.exports = {
  chainWebpack: (config) => {
    const moduleTypes = ['vue-modules', 'vue', 'normal-modules', 'normal'];
    moduleTypes.forEach(moduleType =>
      config.module
        .rule('scss')
        .oneOf(moduleType)
        .use('@epegzz/sass-vars-loader')
        .loader('@epegzz/sass-vars-loader')
        .options({
          syntax: 'scss',
          files: [
            path.resolve(__dirname, 'src/assets/styles/contracts/variables.json'),
            path.resolve(__dirname, 'src/assets/styles/contracts/icons.json')
          ],
        })
    )
  },

  css: {
    loaderOptions: {
      sass: {
        data: `
          @import '~@/assets/styles/vars';
          @import '~@/assets/styles/util';
          @import '~@/assets/styles/func';
          @import '~@/assets/styles/mixins';
          @import '~@/assets/styles/colors';
        `,
      },
    },
  },
}

But on app loaded I have got an error: "Undefined variable: $palette".
It works properly, when I importing common scss-files directly, but I want to import it all in one place.

Can you help me?

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

So what you're basically saying is, that the @import '~@/assets/styles/...' is not working?

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

Ah, no, the imports are working, but the files don't see the vars from the sass-vars-loader, right?

@netstuff
Copy link
Author

Thank you for reply, @epegzz!

Ah, no, the imports are working, but the files don't see the vars from the sass-vars-loader, right?

Yes, it is! I think there is inproper loading order. But I am not sure.

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

Basically what the sass-vars-loader is doing, is converting this:

@import '~@/assets/styles/vars';
@import '~@/assets/styles/util';
@import '~@/assets/styles/func';
@import '~@/assets/styles/mixins';
@import '~@/assets/styles/colors';

to this:

$var1=value1
$var2=value2
...
@import '~@/assets/styles/vars';
@import '~@/assets/styles/util';
@import '~@/assets/styles/func';
@import '~@/assets/styles/mixins';
@import '~@/assets/styles/colors';

So, as far as I understand Sass, the code in the @imported files should have access to those global vars 🤔

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

I'm not really familiar with the chainWebpack method. Could it be that the css command is actually overwriting the results from sass-vars-loader?

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

What about putting the @imports in a .sass file and load it as part of the sass-vars-loader?

const path = require('path');

module.exports = {
  chainWebpack: (config) => {
    const moduleTypes = ['vue-modules', 'vue', 'normal-modules', 'normal'];
    moduleTypes.forEach(moduleType =>
      config.module
        .rule('scss')
        .oneOf(moduleType)
        .use('@epegzz/sass-vars-loader')
        .loader('@epegzz/sass-vars-loader')
        .options({
          syntax: 'scss',
          files: [
            path.resolve(__dirname, 'src/assets/styles/contracts/variables.json'),
            path.resolve(__dirname, 'src/assets/styles/contracts/icons.json'),
            path.resolve(__dirname, 'src/assets/styles/imports.scss') // <--- 
          ],
        })
    )
  }
}

@epegzz
Copy link
Owner

epegzz commented Aug 23, 2019

Hm, this would actually not work, since the sass files always come first. That's a stupid design! 🙈 😆

CleanShot 2019-08-23 at 13 45 55@2x

I could change that. It's basically #23

@netstuff
Copy link
Author

What about putting the @imports in a .sass file and load it as part of the sass-vars-loader?

I have tried it and have got error while parsing .scss-file :(

@dschox
Copy link

dschox commented Oct 15, 2019

Could you please implement this? #23
I'm in the same boat. Changing the order manually inside node_modules works for me.
Thank You! :)

@epegzz
Copy link
Owner

epegzz commented Oct 20, 2019

@dschox unfortunately I'm really busy lately and won't have the time to work on this currently. But I'm happy to merge any incoming PR :)

@dschox
Copy link

dschox commented Oct 21, 2019

@epegzz I think a PR is out of my league :)
Is it possible you can do this this year? ;)

@epegzz
Copy link
Owner

epegzz commented Oct 21, 2019

Is it possible you can do this this year? ;)

oh yeah, absolutely 👍

@dschox
Copy link

dschox commented Dec 17, 2019

@epegzz Any news on this? :)

@epegzz
Copy link
Owner

epegzz commented Dec 17, 2019

The year is not over yet! 😆

@epegzz
Copy link
Owner

epegzz commented Jan 11, 2020

@dschox file order is now respected in v6.0.0. Thanks for your patience 👍

@dschox
Copy link

dschox commented Jan 14, 2020

Thank You very much! And happy new year :)

@franciscolourenco
Copy link

@epegzz I ran into the same issue and this works around the problem, thanks! 👍

@dschox
Copy link

dschox commented Feb 21, 2020

I think we can close this...

@franciscolourenco
Copy link

franciscolourenco commented Feb 21, 2020

Maybe a mention in the README to the alternative data/prependData option.

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

No branches or pull requests

4 participants