npm 多个入口点 [英] npm multiple entry points
问题描述
我正在制作一个 NPM 包,我想知道如何注册多个入口点,以便用户可以选择引入整个库或仅引入他们打算使用的一部分.
I'm making an NPM package and I'm wondering how you can register multiple entry points so that the user can choose to bring in either the entire library or just a portion that they intend on using.
例如引入整个图书馆:
const mainLib = require('main-lib');
或者只带一部分:
const subLib1 = require('sub-lib-1');
const subLib2 = require('sub-lib-2');
让 package.json 的主要属性接受多个值对我来说似乎很直观,但根据文档,情况似乎并非如此.
It seemed intuitive to me to have the main property of package.json to accept multiple values but that doesn't seem to be the case according to the documentation.
推荐答案
"main"
定义调用 require(...)
时加载的模块包的名称.但是,您也可以要求该包中的特定文件.
"main"
defines the module to load when you call require(...)
with just the package's name. However, you can also require a specific file in that package.
例如使用以下包:
- mypackage/
- main.js <- "main" in pkg.json
- moduleA.js
- src/
- index.js
- filaA.js
- fileB.js
- package.json
以下内容有效:
require( 'mypackage' ) // resolve to main.js
require( 'mypackage/moduleA' ) // resolve to moduleA.js
require( 'mypackage/src' ) // resolve to src/index.js
require( 'mypackage/src/fileA' ) // resolve to src/fileA.js
这篇关于npm 多个入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!