question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow optional @imports

See original GitHub issue

I believe it would be helpful to framework design and stylesheet modularity if there were a way to declare an @import which would fail gracefully if a file was not found.

Jumpt to the TL;DR

Syntax

To make it’s behavior obvious this could be a flag !optional borrowing from the style of !default and !important. It might look like this:

@import "path/to/partial" !optional;

Use case

In Octopress I’m trying to make it easy for people to easily install themes and plugins (with their Sass assets) and I want it to be easy for users to customize these theme and plugin stylesheets. At the same time I want it to be possible for users to enjoy Octopress without installing any themes or plugins. As a result I have come up with this method for managing Sass.

Let’s look at an Octopress stylesheet directory

/* A standard installation contains these two stylesheets */

site.scss         # Site import index (no not touch)
_style.scss       # Users add styles, import styles here

/* Created when a theme or plugin is installed */

plugins/
  widget-a/       # Plugins stylesheets are dir name spaced
    _style.scss   # Plugin Sass assets (imported automatically)

theme/
  _index.scss     # Theme import index (imported automatically)
  _settings.scss  # Sets Theme variables ($var: true !default;)
  core/           # Core styles (normalize, layout, color, etc)
  modules/        # Styles for components (article, sidebar, etc)

The root stylesheets site.scss uses optional imports and globbing to automatically import stylesheets from a theme or plugins.

@import "theme/index" !optional;
@import "plugins/*/style" !optional;

@import "style";             // Auto import user stylesheets

Here !optional imports would allow me to create a zero configuration install experience for end users. Users can install themes or plugins (or not) and they will automatically be imported. Also if I can reasonably expect them not to have any reason to change the site.scss file, making it easier for me to make changes to that file if needed.

Additionally if I wanted to allow users to easily override theme or plugin settings, I could change the site.scss like this:

@import "theme-settings" !optional;
@import "theme/index" !optional;
@import "plugin-settings" !optional;
@import "plugins/*/style" !optional;

@import "style";             // Auto import user stylesheets

This will allow a user to create a specific file to set default settings for a theme or plugin before it is imported. Without optional imports, a user must make changes to core files.

TL;DR

If imports can respect an !optional flag, frameworks like Octopress can create a framework which allows users to install new stylesheets without having to manage imports. Essentially this would allow zero-configuration installation for Sass stylesheets.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:3
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
migabercommented, Aug 29, 2016

Optional @import is really important

6reactions
jslegerscommented, Sep 12, 2016

Any update on this?

I’ve literally been waiting for optional imports to be implemented for >2 years now…

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's Python good practice for importing and offering ...
In general, it's best to let Python handle import logic — it's been ... that does the optional import (using importlib ) and...
Read more >
Python Type Hints - How to Handle Optional Imports
Import, or import not, there is no try. This post is not about importing typing.Optional , but instead imports that are themselves optional....
Read more >
Optional imports | Modular Programming with Python
Optional imports. Try opening the Python interactive interpreter and entering the following command: import nonexistent_module. Copy.
Read more >
Optional Imports in React Native - Instamobile
This optional import helps us achieve some form of backwards compatibility especially since React Native core modules are being abstracted to be ...
Read more >
Is it okay to do optional imports? : r/learnpython - Reddit
IconTheme import getIconPath from xdg.Exceptions import Error as XDGError # etc __builtin__.__import__ = realimport # remove import hook, let ImportError ...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found