Giter Site home page Giter Site logo

tiny-bundle's Introduction

Tiny-Bundle

简易JS打包器,不做语法转换,只支持ESM

支持Feature

  • import文件类型
    • JS
    • JSON
    • TS
  • import方式
    • 本地文件
    • node_modules
    • URL
    • Path alias
    • Export from
    • Default
    • Namespace
    • Dynamic Import
  • 优化
    • Parallel
    • 使用require形式(这种比较简单)
    • Tree Shaking
    • Dead Code Elimination

思路

  1. 根据给定入口,进行依赖收集,获取所有待打包文件
  2. 拓扑排序,确定打包顺序
  3. runtime代码注入 (参考 runtime.go)
  4. 符号表合并
    1. 消除同名变量,作用域提升 (通过 ScopeBinding找到引用节点)
  5. 按顺序写入一份文件

Bundle规则(纯ESM情况)

这种情况可以作用域提升,所有模块都直接写在顶层

源文件 打包后
顶层函数 function 在顶层进行定义
顶层变量 var, let, const, class 顶层 var 定义
顶层语句 顶层中使用
别名导入 import { x as y } from 'bar' 无视别名,仍然使用 x
命名空间导入 import * as mm from 'baz' 无视命名空间,使用原本命名
默认导入 import z from 'foo' 使用 foo_default 代替 z (最麻烦,会有命名重复问题)
Node自带的module 维持使用import

Bundle规则(ESM、CJS混编情况)

模块需要用函数包一层

源文件 打包后
顶层函数 function 在顶层进行定义
顶层变量 var, let, const, class 顶层 var 声明,init 中定义
顶层语句 init 中使用

TODO

  • export { a as b } 的处理
  • export * 的处理
  • 单元测试
  • Dynamic Import处理

实用的包

Reference

tiny-bundle's People

Contributors

fun4wut avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.