-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
Pass variables to scss files #49
Comments
Could you show an example how you'd expect things to work? I'm afraid that this is currently not possible (if I got it right). But with custom importers #31 it's probably possible. Then you can write a special loader, which generates scss code to import your dynamic vars. |
@jhnns thank you for your response I'm thinking of the webpack.DefinePlugin({
'SCSS_OPACITY': '0.4'
}); $opacity: SCSS_OPACITY;
@include opacity($opacity); but perhaps if it's easier for it to create a file with all of them and somehow matching |
Mhmmm your solution would probably work too. And when you need to access the variables also via client-side JavaScript: webpack.DefinePlugin(require("./styles/variables.js")); |
Yeah, using the |
Oh, there are use-cases where this is required... :) I'll check that! |
Using this perhaps? https://github.com/webpack/docs/wiki/list-of-plugins#dependency-injection Without looking into it I would hazard a guess it should make it easier? (hopefully) :) |
Hi! I wanted the same feature a while back but realized this is already supported by webpack. My solution looks like this: var sass = encodeURIComponent(jsonToSassVars(sassGlobals));
var webpackConfig = {
module: {
loaders:[
{test: /.scss$/, loader: "style!css!sass!prepend?data=" + sass}
]
},
} The prepend-loader and jsonToSassVars are custom work of mine. |
@kasu any chance of them being made Open Source? |
@nwhite89 Sure thing, here you go: prependLoader.js and jsonToSassVars.js. |
@kasu cheers 👍 although it would be nice to have this functionality baked into sass-loader |
@kasu really liked your work so I went and forked it into a npm loader https://www.npmjs.com/package/jsontosass-loader |
@EdwardIrby cool :)! |
Is @EdwardIrby 's solution working for you? I'd like to link the loader in our README, but I'm not sure about how safe that string replaces are 😁 ... Do you have any experience on this? |
@EdwardIrby I don't think you should return a json file but a js file that returns an json object. This way you can do custom calculations to darken or lighten a color for example or other caculations you might need. |
@nwhite89 @EdwardIrby @jhnns I think this could be what you guys need: Sassport I just found this myself and didn't use it yet, but it seems like it enables you to do what you want - sharing values between JavaScript and Sass. |
Thx @nightgrey This issue can now also be solved with the |
@jhnns can we see a working example of the data option? Having issues getting it to work Never mind looking back at earlier PR's I found my query params were overriding my sassLoader. It's a bit confusing where to options, query params or sassLoader |
I know... this will be simpler with webpack 2 where there is a dedicated way to set the options (query params didn't work so well with more complex use-cases). |
@jhnns I can get the environment variable to be passed in via I am doing something like: sassLoader: {
data: '$myColor: green;',
}, and then in a random .scss file I just use |
Nope, looks good. There must be a different error in your setup. |
@jhnns Yeah, the issue was that I wasn't including this option on the server bundle as well. It is working, thanks! |
Hey guys, had the same needs for passing vars to my sass files, so i also created a little webpack loader based on @kasu's gist. It lets you import vars directly via webpack, or by specifying a JSON or JS file. Check it out :) |
After much digging around, I found an even easier solution: https://www.npmjs.com/package/webpack-append Despite its misleading name, this package does, in fact, prepend specified text to the beginning of each file. And unlike |
How is that easier than passing the vars directly in webpack config or by specifying paths to JS/JSON files? // webpack.js
sassVars: {
vars: {
someColor: red,
someOtherColor: blue,
}
} …and you're ready to use them anywhere in your scss .foo {
color: $someColor;
} |
@epegzz This way you can basically just write Sass rather than JS. Also by prepending as a string you could theoretically just use |
@Cleod9 writing JS or JSON inside the webpack config which is JS itself feels way nicer to me. And the OP wanted to pass data potentially stored in a database. Chances are, those data already come as JSON or even JS object when he's loading them inside the webpack.js. But in the end, it's all a matter of personal preference and specific use case :) |
@epegzz True, and I appreciate your plugin btw! I think for others who land in this thread will find that at least one of these solutions will work for them (this thread is like the number one search result for "sass variables through webpack" 😉 ) |
@epegzz is it webpack2 compatible? Webpack2 doesn't allow custom properties on the webpack config, must use the |
@IAMtheIAM haven't tested yet, but no, probably not then, thx for pointing it out! :) The following should work with webpack2 though, right? // webpack.js
var sassVarsConfig = querystring.stringify({
files: [
path.resolve(__dirname, 'path/to/vars.js'),
]
});
return {
loader: ExtractTextPlugin.extract(
'style',
'css!sass!@epegzz/sass-vars-loader?' + sassVarsConfig
),
//... |
@epegzz Good job dude! That worked perfectly, thanks, I was struggling for hours to get any of these options to work with webpack 2, so that I can take a JS/JSON file and have it converted to sass, and appended before all scss files. Great! Here's my full setup:
Now the only thing I'm wondering is it your |
@epegzz I made a pull request fixing your loader, to allow users to pass "vars" directly on the webpack config, rather than in an external file. So now both options work. Please review and approve it so others can use :-) You just have to JSON.stringify anything in the vars property like this:
Then, your loader should look something like:
|
awesome @IAMtheIAM, merged and pushed to npm :) |
@epegzz I can't install your module:
|
@534N I answered in dsc8x/sass-vars-loader#6 :) |
@jhnns Is the data option working correctly for Webpack V2. @epegzz @IAMtheIAM great solution. I will try to use it as stated. |
@nagarajay Does this work for you? |
I understand that the solutions above allow us to define and use SASS variables in our For example, could we override the |
When they're using |
With using |
I think it's worth noting that @jhnns above solution is great, but the description on how to actually use it wasn't clear. The module.exports = {
entry: [...],
module: {
loaders: [...],
},
sassLoader: {
data: '@import "variables";',
includePaths: [
path.resolve(__dirname, '../src/scss/tools'),
],
},
}; |
While this is true for webpack 1, this has been changed with webpack 2. See current README. |
@CallMeXYZ did you resolve your issue ? I am having same |
@volodymyr-tsaryk check above @jhnns answers. For simple varibles you may do sth like this
|
@CallMeXYZ that worked, thanks |
So useful, glad Google led me here. Thanks all. https://webpack.js.org/loaders/sass-loader/#environment-variables |
@epegzz Your solution is great, although I'm wondering if there's a way to use it when requiring scss files. I think that it would make more sense to be able to specify the variables in your index.js file. The reason I want to do this is I would like to be able to set background-images dynamically. Something like
|
@ShawnCorrigan although I can see use-cases for your proposal, it, unfortunately, does not play well with ES6 modules :( |
@ShawnCorrigan However, you could simply call // webpack.config.js
const headerBackgroundImage = require(htmlWebpackPlguin.options.bgImage);
module.exports = {
module: {
rules: [{
test: /\.scss$/,
use: [
// …
{
loader: "@epegzz/sass-vars-loader",
options: {
vars: {
'header-background': headerBackgroundImage
}
}
}
]
}]
},
}; |
@epegzz Hmm okay. The idea was to not really modify the webpack.config.js file, although I suppose I could have another file that the webpack.config.js file uses to load the images. Thanks for the response! |
@ShawnCorrigan Sorry for the late reply, I somehow missed your response above. But yeah, you're absolutely right. In most cases, you probably want to load all vars from a file and never touch the Webpack config again :) |
Hi @volodymyr-tsaryk with one var works like a charm, but how to share multiple vars? is that possible? thanks! |
This guy no longer supports this lib. Sad. |
Anyone trying to use simple variables as described by above, the
|
If you're here through a google search, this has been renamed once more to
See #865 |
I'm wanting to pass variables from webpack to the scss files, for example opacity is able to be changed by a user and stored in the db. Ideally I'd pass the variable same way as you do to JS files via webpack and pass them straight through to the SCSS files before compilation.
The text was updated successfully, but these errors were encountered: