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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lib-numbers-webpack

v1.0.4

Published

[参考链接](https://v4.webpack.docschina.org/guides/author-libraries/)

Downloads

24

Readme

用webpack打包库

参考链接

创建library

  • 除了打包应用程序,webpack 还可以用于打包 JavaScript library。以下指南适用于希望简化打包策略的 library 作者

  • 假设你正在编写一个名为 webpack-numbers 的小的 library,可以将数字 1 到 5 转换为文本表示,反之亦然,例如将 2 转换为 'two'。

  • 创建这个lib得满足前端常用的调用规范(ES2015,CommonJS,AMD,script标签)

基本配置

现在,让我们以某种方式打包这个 library,能够实现以下几个目标:

  • 使用 externals 选项,避免将 lodash 打包到应用程序,而使用者会去加载它。

  • 将 library 的名称设置为 webpack-numbers。

  • 将 library 暴露为一个名为 webpackNumbers 的变量。

  • 能够访问其他 Node.js 中的 library。 此外,consumer(使用者) 应该能够通过以下方式访问 library:

  • ES2015 模块。例如 import webpackNumbers from 'webpack-numbers'。

  • CommonJS 模块。例如 require('webpack-numbers').

  • 全局变量,在通过 script 标签引入时

外部化限制本项目lodash

  • 使用 externals 选项,避免将 lodash 打包到应用程序,而使用者会去加载它。

外部化限制

对于想要实现从一个依赖中调用多个文件的那些 library:

import A from 'library/one';
import B from 'library/two';
// ===========
module.exports = {
  //...
  externals: [
    'library/one',
    'library/two',
    // 匹配以 "library/" 开始的所有依赖
    /^library\/.+$/
  ]
};

无法通过在 externals 中指定整个 library 的方式,将它们从 bundle 中排除。而是需要逐个或者使用一个正则表达式,来排除它们。

暴漏library

  • 对于用法广泛的 library,我们希望它能够兼容不同的环境,例如 CommonJS,AMD,Node.js 或者作为一个全局变量。
  • 为了让你的 library 能够在各种使用环境中可用,需要在 output 中添加 library 属性:
  • 这会将你的 library bundle 暴露为名为 webpackNumbers 的全局变量,consumer 通过此名称来 import。为了让 library 和其他环境兼容,则需要在配置中添加 libraryTarget 属性。这个选项可以控制以不同形式暴露 library。

具体

// 有以下几种方式暴露 library:

  • 变量:作为一个全局变量,通过 script 标签来访问(libraryTarget:'var')。

  • this:通过 this 对象访问(libraryTarget:'this')。

  • window:在浏览器中通过 window 对象访问(libraryTarget:'window')。

  • UMD:在 AMD 或 CommonJS require 之后可访问(libraryTarget:'umd')。

  • 如果设置了 library 但没有设置 libraryTarget,则 libraryTarget 默认指定为 var,详细说明请查看 output 文档