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

export sass variables for include in JavaScript #72

Closed
svdoever opened this issue Jun 4, 2020 · 5 comments
Closed

export sass variables for include in JavaScript #72

svdoever opened this issue Jun 4, 2020 · 5 comments
Assignees

Comments

@svdoever
Copy link

svdoever commented Jun 4, 2020

I try to use the :export construct as described in https://stackoverflow.com/questions/61517117/cannot-export-sass-variables-into-javascript?noredirect=1&lq=1 to be able to use values of SASS variables in my JavaScript but can't get it working:

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    some-light-blue: $some-light-blue;
    battleship-grey: $battleship-grey;
    // etc...

}

Should this work with rollup-plugin-sass, or is there another method to accomplish this?

@gloverab
Copy link

Struggling with this same issue. Did you ever get it resolved?

@dangnelson
Copy link

Dying for this feature.

@duowb
Copy link

duowb commented Jan 26, 2022

This is my implementation, I don't know if you can use it

index.scss

:export {
  --font-size-primary: 14px;
  --color-border-primary: #3B4144;
}

test.js

import index from './index.scss'
console.log(index)

dist/src/test.es5.js

var index = {"--font-size-primary":"14px","--color-border-primary":"#3B4144"};

console.log(index);

@elycruz
Copy link
Owner

elycruz commented Aug 16, 2022

@duowb Nice! .. I will look into integrating the feature later this week, or if you want to open a pull request ..?

@elycruz elycruz self-assigned this Sep 15, 2022
elycruz added a commit that referenced this issue Sep 17, 2022
…piled css output.

- Added test/example showing how to extract sass variables to generated output *.js module files.
- Simplified 'License' section in main README.
- Updated 'LICENSE' to point to './humans.txt' for maintaners/owners list.
elycruz added a commit that referenced this issue Sep 17, 2022
elycruz added a commit that referenced this issue Sep 17, 2022
elycruz added a commit that referenced this issue Sep 17, 2022
elycruz added a commit that referenced this issue Sep 17, 2022
issue-#72 Tests, example fix/approach, and a patch to uncaught 'duplicate css' bug
elycruz added a commit that referenced this issue Sep 17, 2022
elycruz added a commit that referenced this issue Sep 17, 2022
- Re-generated package-lock.json to fix erroneous version number publish.
@elycruz
Copy link
Owner

elycruz commented Sep 17, 2022

As pointed out in #71 turns out the plugin supported this all along (lol):
Tl;dr
You need to use 'icss-utils', along with 'postcss', to process the output css and then return an object containing all exported key/value pairs to the plugin, from the 'processor' field:

test('should support processor return type `Promise<{css: string, icssExport: {}, icssImport: {}}}>', async t => {
const outputBundle = await rollup({
input: 'test/fixtures/processor-promise/with-icss-exports.js',
plugins: [
sass({
processor: (css) => new Promise((resolve, reject) => {
const pcssRootNodeRslt = postcss.parse(css),
extractedIcss = extractICSS(pcssRootNodeRslt, true),
cleanedCss = pcssRootNodeRslt.toString(),
out = Object.assign({}, extractedIcss.icssExports, {
css: cleanedCss,
});
// console.table(extractedIcss);
// console.log(out);
resolve(out);
}),
options: sassOptions,
}),
],
}),
{output} = await outputBundle.generate(generateOptions),
rslt = squash(unwrap(output));
t.true(rslt.includes(expectA2));
t.true(rslt.includes(expectB));
});

@elycruz elycruz closed this as completed Sep 17, 2022
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

5 participants