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

request: import once functionality #10

Open
TxHawks opened this issue Feb 23, 2016 · 9 comments
Open

request: import once functionality #10

TxHawks opened this issue Feb 23, 2016 · 9 comments

Comments

@TxHawks
Copy link

TxHawks commented Feb 23, 2016

An import-once functionality in the default, reference and dynamic loaders would be really nice.

See node-sass-import-once and eyeglass's import-once

@davidkpiano
Copy link
Owner

I'll be releasing this soon! Here's a proof of concept: https://github.com/davidkpiano/sassport/blob/master/src/modules/once.js

You can actually straight up use this module right now:

var importOnce = require('sassport/lib/modules/once');

//...

sassport([ importOnce ]).render(...);

Then in your SCSS:

@import 'some-partial !once';

// ...

@TxHawks
Copy link
Author

TxHawks commented Feb 23, 2016

Is it possible to define once as the default behavior? And is it possible to mix loaders?

I mean do something like

@import 'some-partial !once !reference';

Sorry for the barrage of questions

@davidkpiano
Copy link
Owner

It's definitely possible to use multiple loaders, and they will run left to right: https://github.com/davidkpiano/sassport/blob/master/test/loaders.js#L52

So your example will work, and it will first run it through once and then through reference.

I can definitely see having a defaultLoaders configuration, what do you think?

sassport([ once, reference ], {
  defaultLoaders: ['once']
}).render(...);

@TxHawks
Copy link
Author

TxHawks commented Feb 23, 2016

Looks really good.

@TxHawks
Copy link
Author

TxHawks commented Feb 23, 2016

I assume the import-once functionality is not yet available in the version that's NPM, right?

@davidkpiano
Copy link
Owner

It actually is available, just not documented: https://github.com/davidkpiano/sassport/blob/master/test/loaders.js#L86

But I will be making a separate package for this module, most likely called sassport-once.

@TxHawks TxHawks changed the title request: import one functionality request: import once functionality Feb 24, 2016
@TxHawks
Copy link
Author

TxHawks commented Feb 24, 2016

Would this work with sub-dependencies? I think this would be the most interesting use case, as it would enable efficient modules.

The scenario is in mind is this:

// _module-a.scss
@import `node_modules/module-b`;

// styles.scss
@import `node_modules/module-a`;
@import `node_modules/module-b`;

ideally, an import-once loader should recognize the module-b was already imported inside module-a and not import it again in style.scss

@davidkpiano
Copy link
Owner

Yes, it will. In Sass, importing is a blocking synchronous action, meaning that this sequence of imports will happen:

  • import module-a (current stack: [module-a])
    • import module-b (current stack: [module-a, module-b])
  • skip module-b since it was already imported by checking current stack

@TxHawks
Copy link
Author

TxHawks commented Feb 24, 2016

👍

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

No branches or pull requests

2 participants