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

Importing JSON #1585

Closed
super-cache-money opened this issue Jan 7, 2015 · 7 comments
Closed

Importing JSON #1585

super-cache-money opened this issue Jan 7, 2015 · 7 comments

Comments

@super-cache-money
Copy link

Can I import variables defined in a JSON file? This is great for DRY purposes, as often I need to share variables with other languages (like javascript). I know there are ad-hoc ways of achieving this but I guess what I'm asking is - should it not be built in?

@lolmaus
Copy link

lolmaus commented Jan 7, 2015

As a Compass user, i would do it from the config.rb: just have it read the JSON with Ruby and expose the data into Sass via a custom Sass function.

If you're not using Compass, you could probably do the same by requiring a Ruby file via the -r command line flag.

@chriseppstein
Copy link

There are Sass extensions that provide this. It is not something that we will be putting into the Sass core functions.

https://github.com/HugoGiraudel/SassyJSON
https://github.com/scottdavis/serialy-sassy

@esr360
Copy link

esr360 commented Jul 2, 2018

@chriseppstein before I open a new bug for this, please would you share the reasons for not putting this into the Sass core functionality?

The alternatives you provided are totally not suitable for any serious project (in fairness to you, you posted these over 3 years ago). Today a better alternative would be https://www.npmjs.com/package/node-sass-json-importer, but even still this doesn't work properly (you cannot just import valid JSON, you must double quote strings, which makes it largely unsuitable) but I digress.

The ability to natively import json into a sass file would be immensely useful, and for peace of mind it would be great to have a proper reason why you won't consider this.

Requiring third party extensions to follow best practices is something I think we should be striving to avoid. Without the ability to import JSON, Sass will eventually die in favour of CSS in JS.

@ArmorDarks
Copy link

I'll just leave it here

@esr360
Copy link

esr360 commented Jul 6, 2018

@ArmorDarks I'm unfortunately too dumb to really understand what that is, other than, presumably, a gate to be able to import JSON files without requiring a third party tool? I will be sure to read into properly, though, so thanks.

@ArmorDarks
Copy link

ArmorDarks commented Jul 6, 2018

You can define a function, which will read in Node environment JSON data from whatever you want and pass it to the Sass.

Here is example how to pass a function within Grunt, and the function itself, which retrieves data from Grunt (but you can read there external JSON).

@esr360
Copy link

esr360 commented Jul 6, 2018

Oh ok, that sounds very interesting, thanks for sharing the resources.

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