惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
美团技术团队
博客园 - 聂微东
博客园 - 叶小钗
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
Spread Privacy
Spread Privacy
J
Java Code Geeks
雷峰网
雷峰网
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
量子位
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
罗磊的独立博客
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Project Zero
Project Zero
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
GbyAI
GbyAI
博客园 - 【当耐特】
O
OpenAI News
Schneier on Security
Schneier on Security
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
博客园 - 司徒正美

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript 5 Inconvenient Truths about TypeScript Refactoring in Rust: Introducing Traits Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates JSX is syntactic sugar TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone
Create manageable Sass components (to use with Bower, etc.)
2014-01-13 · via oida.dev | TypeScript, Rust

Having a set of reusable and ready software components is a great thing. And for a multitude of reasons, like ensuring DRY development or boosting efficiency. A software component is a self contained unit of program code which can be accessed only by a defined interface. Or like Berkely University puts it:

A software component is a unit of composition with contractually specified interfaces and explicit context dependencies only. A software component can be deployed independently and is subject to composition by third parties

We already have a lot of components in JavaScript. In the CSS world, developing such components is a lot harder. CSS is a fairly easy language: You have a set of properties which can take certain values, and that’s it. The beauty of CSS lies in this simplicity, but same simplicity makes reusability difficult.

When using a preprocessor like Sass, we might achieve more possibilities for component building, but we still might have some of those difficulties regarding self-containment.

Handling components with Bower and Revision Control #

That’s especially true when used with a package manager like Bower. Bower was designed to take care of all your third party libraries which you can install from GitHub. Bower provides a command line interface for (un-)installing components. You might know that with

bower install konamicode.js

you just download the newest version of that component in your components folder. With the --save flag

bower install konamicode.js --save

you also add this library to a file called bower.json, which records all your installations.

{
"name": "demo",
"version": "0.0.0",
"dependencies": {
"konamicode.js": "~1.0.0"
},
"devDependencies": {}
}

The main benefit of this file is that – even if you accidentally delete one of the installations or mess up with them – a simple

bower install

will re-install all the previously installed and --saved components, even in the right version.

Now think of revision control. When you are saving your project in some sort of repository (GitHub, SVN, whatever), you would just need to commit bower.json. Because everything can be reconstructed. Just like only committing package.json and calling npm install after checkout will reconstruct all the node modules you’ve previously had installed.

Why you shouldn’t commit those files? Because not only do you save space and unnecessary transactions, you can also keep your project’s repository clean and clear, and focus just on the files that are really the contents of your project. For a more detailed explanation and even more reasons, checkout Mikeal Rogers’ excellent article on that topic.

And this just works really well for JavaScript components, because (at least most of the time) you don’t have to make any changes to the library itself. Take jQuery for example: This is just one file you download from the repo and your done with.

Modernizr is more advanced: Download Install the whole repository and build a custom modernizr.js depending on the actually used CSS and JS properties without touching the original files. No need to commit all the thousand-something files in your repository.

Also, Sass/LESS mixing libraries fulfil the requirements for being handled like this. But with other CSS – or rather Sass components – you have one big obstacle if you want to do so: They are (mostly) meant to be adapted and changed. Be it colour values in Twitter Bootstrap or font properties in Harry’s recent Typecsset.

But actually the latter one is a prime example of how to keep those kind of modules manageable.

The !default flag #

Enter the !default flag. This little command has everything we need to create default values for our variables, just like other programming languages would handle them.

To quote the Sass reference:

You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.

So, given a !default flag at the end of your variable assignment, we make sure that there is a value available no matter if your developer has assigned one or not. Take a look at this example:

$spacing: 	1rem !default;

.box {
margin: 0;
margin-bottom: $spacing;
}

If I assign a value to $spacing on any other occasion before stating the class, the class will take that value. On the other hand, if I don’t, I still make sure that my statement has some value.

This behaviour gives us one real benefit when developing Sass components: We can configure them from outside, without touching the source itself. We get a needed interface for configuration and usage, but the component is as self contained as it should be.

Harry’s Sass libraries show perfectly how this might work. Again, take Typecsset as an example. To quote (and comment) the demo code from his README:

/* YOUR configuration for typecsset, overriding the
default values from the library. Our interface to
the component
*/

$typecsset-base-font-size: 18px;
$typecsset-base-line-height: 27px;

[Your own CSS]

/* Inclusion of the -- now parametrised -- library */
@import "path/to/typecsset";

[More of your own CSS]

Another example comes from inuit.css, his CSS framework, which is a lot bigger and features actually a set of different components.

/*------------------------------------*\
$OBJECTS AND ABSTRACTIONS
\*------------------------------------*/

/**
* Which objects and abstractions would you like to use?
*/

$use-grids: false!default;
$use-flexbox: false!default;
$use-columns: false!default;
$use-nav: false!default;
$use-options: false!default;
$use-pagination: false!default;
...

All of those sub-component can be activated on demand. And that’s the real beauty of it: Instead of adding a lot CSS definitions that you have to remove, you are opting them only if you really need them. Your Sass code might look like this:

$use-grids: true;
$use-flexbox: true;

/* Next to the core definitions, just 'grids'
and 'flexbox' is going to be used */

@import "path/to/inuit";

Your very own Sass component #

If you have Sass code that you are going to reuse with different parameters, and one that is not meant to be in some sort of mixing or function, you can easily apply those learnings by yourself:

  • Every value that can change across uses should be put in a Sass variable.
  • This variable should be set to a default value in the library/component itself. Use the !default flag to apply the rule of using this very value if it has not been set yet.
  • If the value needed differs from the default value, it should be assigned outside of the library files, just before it’s inclusion.
  • If you want to register it with Bower, see their docs, especially Defining a package and Registering packages

Thanks to @yellowled for proof-reading

Related Articles