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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

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
Gulp Recipes - Part Two: You might not need this plugin
2015-05-03 · via oida.dev | TypeScript, Rust

One month has passed, many questions on StackOverflow have been answered, so here’s yet another round of common Gulp issues with a simple and repeatable solution to them. Be sure to check out last month’s edition, as well as my articles on Gulp, PHP and BrowserSync and multiple Browserify bundles.

  • Confirm prompts in Gulp
  • Sync directories on your harddisk
  • Pass command line arguments to Gulp

Confirm prompts in Gulp

Imagine a Gulp task that actually takes long and has a lot of file operations going on. Or even one which deploys to some cloud storage in the end. Sometimes you want to really make sure that you or your co-workers want to run this task or execute the following steps. Just pop up a command line prompt and let the user confirm his action.

There are actually two plugins out there doing this, the one being gulp-confirm and the other being gulp-prompt, both with their own idea of how this should work. The latter one is actually a wrapper to inquirer and passes through all options unfiltered and unchanged.

So why not use inquirer directly? The API is pretty straight forward, so give it a go:

var inquirer = require('inquirer');

gulp.task('default', function(done) {
inquirer.prompt([{
type: 'confirm',
message: 'Do you really want to start?',
default: true,
name: 'start'
}], function(answers) {
if(answers.start) {
gulp.start('your-gulp-task');
}
done();
});
});

One important piece in here is the done callback. This one has to be passed and called in inquirer’s callback function to let Gulp know when this task has stopped. Otherwise it would end the task directly after calling it.

Sync directories on your harddisk

This problem has come up multiple times on StackOverflow recently: People wanting to sync two folders, having all contents available in one folder also available in another. They have been searching for the best plugin, but didn’t find anything really fitting their needs.

Before I came up with solutions, I asked a few things back:

  • Do you really want to keep both folders in sync, which means having changes in folder A appear in folder B, and vice versa?
  • Do you need this sync as part of some deployment on a remote location (if so, use gulp-rsync), or during development process, where you have a watcher running.

Turned out that the true A <-> B scenario actually never happened, it was more of an A -> B scenario during development. And for this scenario the solution was strangely easy. If you want to have your contents in another folder, you don’t need any Gulp plugin whatsoever. In fact, you don’t need any Gulp plugin at all:

// the sync
gulp.task('sync', function() {
return gulp.src('./a/**/*')
.pipe(gulp.dest('./b'));
});

// the watcher
gulp.task('watch', function() {
gulp.watch('./a/**/*', ['sync']);
})

This line will copy all files from folder a to folder b. Include the gulp-newer plugin to boost up performance as you go:

var newer = require('gulp-newer');

gulp.task('sync', function() {
return gulp.src('./a/**/*')
.pipe(newer('./b'))
.pipe(gulp.dest('./b'));
});

But this is just half of the story. A real sync also deletes files in B should they’ve been deleted in A. For that we would need a change in our watcher:

var del = require('del');
var path = require('path');

gulp.task('watch', function() {
var watcher = gulp.watch('./a/**/*', ['sync']);
watcher.on('change', function(ev) {
if(ev.type === 'deleted') {
del(path.relative('./', ev.path).replace('a/','b/'));
}
});
})

Everytime a file changes in our selected glob, we call sync. But before sync happens, the change event is called, where we dive in and do the following: Should the file have been deleted, we change its path to the corresponding file in the other folder and delete it!

There it is. Your one-way sync without any fancy Gulp plugin required.

Pass command line arguments to Gulp

One question coming up a lot is how to handle different environments and destinations. This comes probably from a Grunt-y mindset, where it was all about handling different configurations for the same task. The more I use Gulp, the less I do have the need for different configurations, because somehow everything can be handled in-stream, but there might still be some occassions where it’s necessary to flip some switches. And it would be great if we could do that directly from the command-line.

Let’s assume we want to change the output directory based on a command-line switch. Usually we want to have it in the dist folder, but based on the command-line argument destination we want to change that.

There’s a wonderful Node.js package out there called yargs, which parses everything that happens on the CLI and gives it to you in an easily devourable way:

var args = require('yargs').argv;

var dest = args.destination ? args.destination : 'dist';

gulp.task('something', function() {
return gulp.src('./src/**/*')
.pipe(sometask())
.pipe(gulp.dest(dest + '/'));
});

Line 3 is the important one. If we have the destination switch set, we set our value to it, otherwise we use the default 'dist' folder.

Run it via gulp --destination=prod on your CLI, yargs will do the rest for you.

Bottom line #

Once more I saw that the best Gulp extras are the ones which don’t require a plugin. That’s the biggest strength of Gulp, but also its greatest weakness: You have to have a good understanding of what the Node.js ecosystem looks like and where to get the right tool for the right job. With Grunt it’s easy: There is a Grunt plugin, and that’s everything you have to know. With Gulp, the less you use it, the more you are on the right way (I know how that sounds).

Everytime you face a certain task and are in search for a Gulp plugin, ask yourself:

  • Does it have anything to do with processing and transforming my files?
  • Is it something that has to occur between the globbing my files and saving it?

If you can answer one of those questions with “No”, you are most likely not in the need of yet another Gulp plugin. Try searching the Node.js ecosystem instead.

Related Articles