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

@coderfatjun/axios

v1.0.41

Published

axios wrapper

Downloads

3

Readme

JS的业务范围

打包文件

npm init 后设置rollup脚本文件路径,并且将type设置为module。

{
  "name": "my-axios",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "bin": {
    "rollup":"./node_modules/.bin/rollup"
  },
  "scripts": {
    "build":"rollup -c"
  },
  "dependencies": {
    "rollup": "^3.27.2"
  }
}

配置rollup.config.js

export default {
  input:'src/index.js',
  output: {
    file: "bundle.js",
    format: "cjs",
  },
}

打包加入外面的库

当我们尝试去引入外面的库,然后进行打包的时候,这时会出现报错:无法解析依赖。虽然打包时不会报错,但是运行时就会被你报了。

image-20230808105843856

官方解释https://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency

能通过两种方式解决

  • 手动配置:将依赖的库,设置到rollup.config.js中的external属性上 (没什么卵用,因为他只是抑制了警告,并没有把lodash-es打包进去)
export default {
  input:'src/index.js',
  output: {
    file: "bundle.js",
    format: "cjs",
  },
  external: ['lodash-es']
}
  • 自动:@rollup/plugin-node-resolve

    使用yarn add @rollup/plugin-node-resolve -d来安装依赖,并在rollup.cnfig.js中使用他。

import resolve from '@rollup/plugin-node-resolve'

export default {
  input:'src/index.js',
  output: {
    file: "bundle.js",
    format: "cjs",
  },
  plugins:[resolve()]
  // external: ['lodash-es']
}

引入commentJs支持

很大一部份的NPM包都是使用commentJs的方式进行开发的。而rollup只是支持ESM的格式,所以会导致很多依赖的NPM包无法正常打包。

官方解释

安装@rollup/plugin-commonjs

yarn add @rollup/plugin-commonjs

rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'

export default {
  input:'src/index.js',
  output: {
    file: "bundle.js",
    format: "cjs",
  },
  plugins:[resolve(),commonjs()]
  // external: ['lodash-es']
}

压缩打包后的代码体积

使用rollup-plugin-terser

yarn add rollup-plugin-terser -D

Rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import cssnanoPlugin from 'cssnano'
import typescript from '@rollup/plugin-typescript'
import path from 'path'
import { terser } from 'rollup-plugin-terser'

export default {
  // 入口
  input: 'src/index.ts',
  output: {
    // file: 'bundle.js',
    dir: path.dirname('dist/bundle.js'),
    format: 'es',
    exports: 'named', // 指定导出模式(自动、默认、命名、无)
    preserveModules: true, // 保留模块结构
    preserveModulesRoot: 'src', // 将保留的模块放在根级别的此路径下
  },
  plugins: [
    // 处理ts
    typescript({
      outDir: 'dist',
      declaration: true,
      declarationDir: 'dist',
    }),
    // 打包的时候会把NPM包也打包进去
    resolve(),
    // 处理NPM包中可能存在commonJS导出方式
    commonjs(),
    // 处理css
    postcss({
      plugins: [
        // 加css前缀的
        autoprefixer(),
        // 压缩css的
        cssnanoPlugin(),
      ],
      // 提取css到一个单独的文件中的
      extract: 'css/index.css',
    }),
    terser(),
  ],
  // external: ['lodash-es']
}

CSS的业务范围

引入css、less、sass、stylus

正常来说rollup是不会处理样式文件的,当我们插件处理,直接打包时,他会报错。

image-20230808114855655

rollup-plugin-postcss这个插件会将所有的 样式文件进行处理。

安装

yarn add postcss rollup-plugin-postcss --dev

rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'

export default {
  input:'src/index.js',
  output: {
    file: "bundle.js",
    format: "cjs",
  },
  plugins:[resolve(),commonjs(),postcss()]
  // external: ['lodash-es']
}

效果:

image-20230808121056469

注意⚠️:如果项目中使用到sass、less、stylus,记得安装额外的预处理

安装对应的依赖:

  • For Sass install node-sass: yarn add node-sass --dev
  • For Stylus Install stylus: yarn add stylus --dev
  • For Less Install less: yarn add less --dev

That's it, you can now import .styl .scss .sass .less files in your library.

添加css的前缀

安装

yarn add autoprefixer -D

更新package.json

效果

image-20230808121515267

提取到单独的css文件中

postcss插件能把项目中的css提取到单独的一个css文件中。

通过修改rollup.config.js中关于postcss的配置就能实现。

image-20230808125351055

image-20230808125337168

压缩css的体积

单独提取的css文件可以看到有很多不必要的换行。这也是一个可以优化文件体积的点。

安装

yarn add cssnano -D

修改rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import cssnanoPlugin from 'cssnano'

export default {
  input: 'src/index.js',
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  plugins: [
    resolve(),
    commonjs(),
    postcss({
      plugins: [autoprefixer(), cssnanoPlugin()],
      extract: 'css/index.css',
    }),
  ],
  // external: ['lodash-es']
}

image-20230808125835494

引入ts

引入@rollup/pulgin-typescript

安装ts

yarn add typescript tslib -d

安装rollup的插件

yarn add @rollup/plugin-typescript -d

修改rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import cssnanoPlugin from 'cssnano'
import typescript from '@rollup/plugin-typescript'

export default {
  // 入口
  input: 'src/index.ts',
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  plugins: [
    // 处理ts
    typescript(),
    // 打包的时候会把NPM包也打包进去
    resolve(),
    // 处理NPM包中可能存在commonJS导出方式
    commonjs(),
    // 处理css
    postcss({
      plugins: [
        // 加css前缀的
        autoprefixer(),
        // 压缩css的
        cssnanoPlugin(),
      ],
      // 提取css到一个单独的文件中的
      extract: 'css/index.css',
    }),
  ],
  // external: ['lodash-es']
}

导出类型声明文件

修改rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import cssnanoPlugin from 'cssnano'
import typescript from '@rollup/plugin-typescript'

export default {
  // 入口
  input: 'src/index.ts',
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
  plugins: [
    // 处理ts
    typescript({
      outDir: 'dist',
      declaration: true,
      declarationDir: 'dist',
    }),
    // 打包的时候会把NPM包也打包进去
    resolve(),
    // 处理NPM包中可能存在commonJS导出方式
    commonjs(),
    // 处理css
    postcss({
      plugins: [
        // 加css前缀的
        autoprefixer(),
        // 压缩css的
        cssnanoPlugin(),
      ],
      // 提取css到一个单独的文件中的
      extract: 'css/index.css',
    }),
  ],
  // external: ['lodash-es']
}

修正项目打包输出路径

这时我们能看到导出的类型和打包后生成的文件都不在这个地方的?这显然是不对的。所以我们要修正,让他们在同一个地方

image-20230808135929347

rollup.config.js

import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import cssnanoPlugin from 'cssnano'
import typescript from '@rollup/plugin-typescript'
import path from 'path'

export default {
  // 入口
  input: 'src/index.ts',
  output: {
    // file: 'bundle.js',
    dir: path.dirname('dist/bundle.js'),
    format: 'es',
    exports: 'named', // 指定导出模式(自动、默认、命名、无)
    preserveModules: true, // 保留模块结构
    preserveModulesRoot: 'src', // 将保留的模块放在根级别的此路径下
  },
  plugins: [
    // 处理ts
    typescript({
      outDir: 'dist',
      declaration: true,
      declarationDir: 'dist',
    }),
    // 打包的时候会把NPM包也打包进去
    resolve(),
    // 处理NPM包中可能存在commonJS导出方式
    commonjs(),
    // 处理css
    postcss({
      plugins: [
        // 加css前缀的
        autoprefixer(),
        // 压缩css的
        cssnanoPlugin(),
      ],
      // 提取css到一个单独的文件中的
      extract: 'css/index.css',
    }),
  ],
  // external: ['lodash-es']
}