Skip to content

Instantly share code, notes, and snippets.

This article primarily focuses on understanding the concept of Webpack Tree Shaking rather than delving deeply into the underlying code implementation. Code examples can be found at https://github.com/hardfist/treeshaking-cases.

One of the challenging aspects of Webpack Tree Shaking is that it involves multiple optimizations working together. Webpack's own use of the term "Tree Shaking" is somewhat inconsistent, often broadly referring to optimizations for dead code elimination. Tree Shaking is defined as:

Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. 
It relies on the static
@hardfist
hardfist / overview.md
Last active October 11, 2023 09:50
[High Level Architecture(Draft)]

本文档介绍 Rspack 整体执行流程以帮助开发者能够更好的了解 Rspack,更多的子模块的细节实现参考各子功能模块的介绍。

本文档基于 0.3.5 版本,因为 Rspack 本身架构一直在迭代,因此本文档有些内容可能会过时,但本文档会尽量保持更新。

Design Goals

  • 兼容 Webpack 体系 尽可能的复用 Webpack 生态,实现 Webpack 生态向 Rust 化的渐进式迁移,兼容 Wepback 的 API 是核心的设计准则,我们在性能和兼容 Webpack 架构上做了一些取舍,有时会为了兼容 Webpack 架构牺牲一些性能优势。
  • Native 优先:为了兼顾性能和兼容性,我们不仅提供了 JS 的 Plugin 和 Loader,也提供了 Rust 版本的 Plugin 和 Loader,并且优先推荐使用 Rust Loader 和 Rust Plugin。
  • 渐进式迁移:整个 JS 的生态工具链大部分都还是使用 JS,我们的目标是可以渐进式的将生态的工具链从 JS 往 Rust 迁移,因此我们后续会将越来越多的 JS 的 Loader 和 Plugin 替换为 Rust 实现。
业务中碰到的一个常见的业务需求是处理一个文件内容,如果文件格式不对或者不存在就返回一个默认值
如小程序编译的场景下,是可以允许ttss文件不存在的,通常我们要写如下逻辑
```
let content: string;
try {
content = fs.readFileSync(ttssPath,'utf-8');
}catch(){
content = '';
}
// 后续transform ttss
{
"0 debug pnpm:scope": {
"selected": 28,
"total": 28,
"workspacePrefix": "/Users/admin/lynx/lynx-mono"
},
"1 debug pnpm:package-import-method": {
"method": "clone"
},
"2 debug pnpm:package-manifest": {
@hardfist
hardfist / .gitlab-ci.yml
Created September 24, 2019 08:40 — forked from superjose/.gitlab-ci.yml
This is an example of a .gitlab-ci.yml that is required for Continuous Integration on GitLab projects.
# Reference: https://www.exclamationlabs.com/blog/continuous-deployment-to-npm-using-gitlab-ci/
# GitLab uses docker in the background, so we need to specify the
# image versions. This is useful because we're freely to use
# multiple node versions to work with it. They come from the docker
# repo.
# Uses NodeJS V 9.4.0
image: node:9.4.0
# And to cache them as well.
type Expr = Add | Multi | Constant
type Add = {
kind: 'add',
left: Expr,
right: Expr
}
type Multi = {
kind: 'multi',
left: Expr,
right: Expr
import { pipe } from 'lodash/fp'
import {promises } from 'fs';
type f<T > = (x:T) => Promise<T>
const delay = (ms:number) => (msg:string)=> new Promise<string>(resolve => {
setTimeout(() => {
console.log('msg:', msg, typeof msg);
resolve(msg+msg);
}, ms);
})
import { compose, pipe } from 'lodash/fp'
type f<T> = (x:T) => T[];
const unit = <T>(x:T) => [x];
const bind = <T>(f:f<T>) => (list: T[]) => {
return list.reduce((x,current) => [...x,...f(current)], [] as T[]);
}
const repeat = <T>(x:T) => [x,x,x];
import { compose } from 'lodash/fp'
type tuple = [number,string];
const unit = (x:number) => [x,''] as tuple;
const bind = (f: (a:number) => [number,string]) => (tuple:[number,string]) => {
const [x,s] = tuple;
const [y,t] = f(x);
return [y, s+ t] as tuple
}
// const compose = <A,B,C>(f: (b:B) =>C ,g: (a:A) => B) => (x:A) => f(g(x))
@hardfist
hardfist / list.c
Created March 29, 2018 03:04
list
#include<iostream>
using namespace std;
struct Node{
int val;
Node * next;
public: Node(int _val, Node* _next = NULL):val(_val),next(_next){
};
};
typedef Node* List;