npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ls-bundle

v1.0.1

Published

🚀基于esbuild和swc的bundler,开箱即用

Downloads

4

Readme

ls-bundle

🚀 zero config bundler, base on esbuild swc

📖 介绍

  1. 基于 esbuild 开发,打包覆盖cjsesm和普通file
  2. 支持自定义 Plugin 式开发,完美支持 esbuild 的构建功能
  3. 借助 swc 能力,支持构建至 ES5 环境

⚙️ 安装

# 建议当前项目中安装
pnpm i ls-bundle  -D

基础使用

ls-bundle [...files]

文件默认会构建至 dist目录下。

支持多入口

ls-bundle src/index.ts src/cli.ts

会在dist目录下产出index.jscli.js

# 构建结果为 dist/index.js dist/cli.js
ls-bundle --entry src/index.ts --entry src/cli.ts

也可以指定构建后的文件名称

# 构建结果为 dist/foo.js 和 dist/bar.js
ls-bundle --entry.foo src/index.ts --entry.bar src/cli.ts

也可以在 ls-bundle.config.ts 中配置:

export default defineConfig({
  // 输出 dist/a.js 和 dist/b.js
  entry: ['src/a.ts', 'src/b.ts'],
  // 输出 dist/foo.js 和 dist/bar.js
  entry: {
    foo: 'src/a.ts',
    bar: 'src/b.ts',
  },
});

设置 exclude

默认情况下,除了生产环境下所依赖的模块(peerDependenciesdependencies)外,会自动构建其他的模块,如果不希望构建,可以使用--external避免构建。

自定义配置

可以使用如下配置

  • ls-bundle.config.ts
  • ls-bundle.config.js
  • ls-bundle.config.cjs
  • ls-bundle.config.json
  • package.json中的ls-bundle

也可以使用defineConfig来进行定制化配置。

import { defineConfig } from 'ls-bundle';

export default defineConfig({
  entry: ['src/index.ts'],
  splitting: false,
  sourcemap: true,
  clean: true,
});

也可以在package.json中进行配置。

{
  "ls-bundle": {
    "entry": ["src/index.ts"],
    "splitting": false,
    "sourcemap": true,
    "clean": true
  }
}

生成声明文件

ls-bundle index.ts --dts

以上指令会导出./dist/index.js./dist/index.d.ts,当导出多种构建格式时,每种构建格式都会生成一个声明文件。

如果有多个入口文件,每个入口文件都会生成一个对应的.d.ts文件。因此,如果想对单个入口文件生成声明文件时,请使用 --dts <entry>`` 格式,例如--dts src/index.ts`。

请注意,--dts不会解析 .d.ts 文件中使用的外部(比如node_modules)类型,如果这是某种要求,可以使用 --dts-resolve

只导出声明文件

--dts-only 指令等同于tscemitDeclarationOnly。可以使用此指令只生成声明文件。

生成 sourcemap

ls-bundle index.ts --sourcemap

会导出 ./dist/index.js and ./dist/index.js.map

如果有多个入口文件,每个入口文件都会生成相对于的.map文件。

构建产物格式

支持ESMCJSIIFE

可以一次性构建多种类型:

ls-bundle src/index.ts --format esm,cjs,iife

将会生成以下文件结构:

dist
├── index.mjs         # esm
├── index.global.js   # iife
└── index.js          # cjs

如果package.json中的type配置为module,产出结果会有所不同:

dist
├── index.js          # esm
├── index.global.js   # iife
└── index.cjs         # cjs

如果不想使用诸如.mjs或者.cjs这类文件后缀,或者当前环境不支持此后缀,可以使用--legacy-output

ls-bundle src/index.ts --format esm,cjs,iife --legacy-output

会构建成:

dist
├── esm
│   └── index.js
├── iife
│   └── index.js
└── index.js

代码分割

目前代码分隔只支持ESM的产物类型,并且默认是开启的,如果想针对CJS的文件类型设置代码分隔,请设置--splitting,会启用esbuild的代码分隔功能。

对应地,如果想关闭代码分隔,请使用--no-splitting

目标环境

此处默认使用tsconfig中的compilerOptions.target,也可以使用--target来手动声明。

支持 ES5

可以使用--target es5指令来将代码编译构建至 ES5 版本,代码首先会构建成ES2020,然后借助 SWC 编译成ES5

watch 模式

ls-bundle src/index.ts --watch

启动watch模式,这意味着在初始构建后,encode-bundle 会监听文件变化。

可以使用--ignore-watch来取消指定文件的监听。

ls-bundle src src/index.ts --watch --ignore-watch folder1 --ignore-watch folder2

成功回调

ls-bundle src/index.ts --watch --onSuccess "node dist/index.js"

--onSuccess会返回Promise类型的函数,可以执行类似如下功能

import { defineConfig } from 'ls-bundle';

export default defineConfig({
  async onSuccess() {
    const server = http.createServer((req, res) => {
      res.end('niubi!');
    });
    server.listen(3000);
    return () => {
      server.close();
    };
  },
});

压缩代码

可以使用--minify来压缩代码

ls-bundle src/index.ts --minify

或者使用terser而不是 esbuild 来压缩代码

ls-bundle src/index.ts --minify terser

tree shaking

esbuild默认开启tree shaking,但是特殊情况下(如:external 模块或者未使用的引用)等情况还是有些问题。

提供--treeshake指令来启用rolluptree shaking

针对更多帮助,请使用ls-bundle --help