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

推荐订阅源

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 One
2015-04-04 · via oida.dev | TypeScript, Rust

In the last two weeks I spent a good deal of time on StackOverflow, trying to solve every open Gulp question there is. The reasons for that are manifold, and besides an overall high amount of spare time and a strong tendency to masochism (it would be more if I’d watch the JavaScript channel there), there was one more reason, which I hope to address at some point in the future.

After answering about 30 questions or so, I saw that while their issues seem to be unique, the solutions to them are often nearly the same. Focussing on the problems that lie underneath, I tried to compile a list of recipes which might help you with your own gulpy hiccups.

To make it more easily digestible, I’ll start with three recipes for now, continuing this series over time.

  • Conditionally delete files
  • Stopping gulp.watch after some time
  • Same task, different configurations

Conditionally delete files

There was one user having fun with TypeScript, and putting the output of every compiled .ts file into the same directory. This directory also contains other .js files:

scripts/
├── module.ts   // the original TypeScript file
├── module.js   // the compile JavaScript file, we need to get rid of this
├── module2.ts  // the original TypeScript file
├── module2.js  // compiled JS --> delete
└── library.js  // original JavaScript file, this one should be kept

So, without changing the folder structure, how do you get rid of the compiled resources, without touching the original JavaScript files? We just cannot delete all “.js” files, because we would remove library.js also. But what we can do is select all our TypeScript files, and find the accompanying JavaScript files. To do so, we use glob (or globby) to recreate the same file selection mechanism we know from Gulp. The output is different: Instead of getting a stream of vinyl objects, we get an array of filenames. And this array we are going to manipulate by replacing the file extension (.ts) with a new one.

What we get is a new array, consisting of only JavaScript files. We pass this one to the del module:

var gulp = require('gulp');
var del = require('del');
var glob = require('glob');

// This method replaces all ".ts" occurrences at the
// end of our filenames with ".js"
var replaceTS = function(file) {
return file.replace(/.ts$/, '.js');
};

gulp.task('delete', function(done) {
glob('./scripts/**/*.ts', function(err, files) {
del(files.map(replaceTS));
done();
})
});

Please note that we don’t need any Gulp plugins or similar. Everything is done using standard node modules. One of the bigger strengths of Gulp.

Stopping `gulp.watch` after some time

In this issue, Stack Overflow user Finglish wanted to know how to stop Gulp’s watch process after being idle for some time. You can use that to be reminded of your own laziness or – more likely – to kill demanding processes should you have forgotten them. The latter one actually happens lots of time to me.

I included this one in this series because you’ll learn that gulp.watch has more to offer than just a simple watch process:

gulp.task('watch', function() {
// gulp.watch here works like you would've
// expected it. Only difference: We save the
// returned watcher in a variable.
var watcher = gulp.watch('./app/*.js', ['jshint']);

// Simple and basic JavaScript: After one hour, we call
// watcher.end to end the watch process and thus
// Gulp, too.
var timeout = setTimeout(watcher.end, 60*60*1000);

// Should one of our watched files change ...
watcher.on('change', function() {
// .. we clear the timeout call we created earlier ...
clearTimeout(timeout);
// .. and start it anew ...
timeout = setTimeout(watcher.end, 60*60*1000);
});
});

Prime example of how basic Gulp functionality mixed with a touch of basic JavaScript methods create something helpful and unique.

Same task, different configurations

This one I do get a lot: What if you want to run the same task, but with different configurations. Take this configuration object for example:

var config = [
{
src: 'project-one/scripts/**/*.js',
name: 'p1.bundle.js',
dest: 'public/compiled'
},
{
src: 'project-two/scripts/**/*.js',
name: 'p2.bundle.js',
dest: 'public/compiled'
},
{
src: 'project-three/scripts/**/*.js',
name: 'p3.bundle.js',
dest: 'private/compiled'
},
{
src: 'project-four/scripts/**/*.js',
name: 'p4.bundle.js',
dest: 'private/compiled'
}
];

We want to run all of those to a pipeline of task which is absolutely identical. The only things different are inputs and outputs. We can achieve this by creating a so called stream array:

var gulp   = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
// this node module will do the trick
var merge = require('merge2');

gulp.task('scripts', function() {
// we use the array map function to map each
// entry in our configuration array to a function
var tasks = config.map(function(entry) {
// the parameter we get is this very entry. In
// that case, an object containing src, name and
// dest.
// So here we create a Gulp stream as we would
// do if we just handle one set of files
return gulp.src(entry.src)
.pipe(concat())
.pipe(uglify())
.pipe(rename(entry.name))
.pipe(gulp.dest(entry.dest))
});
// tasks now includes an array of Gulp streams. Use
// the `merge-stream` module to combine them into one
return merge(tasks);
});

This pattern can be used by many problems. We had something similar last time when we created multiple Browserify bundles.

Bottom line #

All three recipes share one thing in common: There is no special gulp plugin or fancy core functionality doing magic tricks, it’s most of the time basic JavaScript and some already existing modules from the Node.js ecosystem. This might also be the reason those questions are asked that often on StackOverflow and consorts, because it’s a lot harder to find and requires some mental switch. Especially if you come from other build systems such as Grunt.

Related Articles