[英]How to export multiple library variants from a single NPM package
I'm creating a node library that could be partially used in web browsers.我正在创建一个可以在 Web 浏览器中部分使用的节点库。
My current structure is something like:我目前的结构是这样的:
package.json
lib/
index.js
node.js
web.js
modules/
some-function.js
some-node-function.js
some-web-function.js
...
"lib/index.js" is specified as "main" in package.json "lib/index.js "在package.json 中被指定为"main"
I'm using each file inside lib/ to re-export functions from modules/ filtered by target (all/node/web)我正在使用lib/ 中的每个文件重新导出模块中的函数/按目标(所有/节点/网络)过滤
And I would like to use it like this:我想像这样使用它:
import fullLibrary from "my-library";
import {someFunction} from "my-library";
import webOnlyLibrary from "my-library/web";
import {someWebFunction} from "my-library/web";
import nodeOnlyLibrary from "my-library/node";
import {someNodeFunction} from "my-library/node";
BUT keeping index.js , node.js and web.js inside /lib !但是将index.js 、 node.js和web.js 保存在/lib 中!
Currently only the first two import statements work (because index.js is pointed from package.json).目前只有前两个导入语句有效(因为 index.js 是从 package.json 中指向的)。
I know that I could place those files in the root of the package and it would work as expected when importing, but I was wondering if there was a way to do the same while keeping the files in /lib for cleanness.我知道我可以将这些文件放在包的根目录中,并且在导入时它会按预期工作,但我想知道是否有办法在将文件保存在/lib 中以保持清洁的同时这样做。
In any case, is this the recommended way to expose multiple variants of a same library from a single NPM package?无论如何,这是从单个 NPM 包中公开同一库的多个变体的推荐方法吗?
Just for anyone that is having similar questions regarding libraries structure and organization.仅适用于对图书馆结构和组织有类似问题的任何人。
Back then I started working with monorepos using Lerna and Yarn Workspaces and I haven't regret it.那时我开始使用Lerna和Yarn Workspaces与 monorepos 合作,我并不后悔。 I think this is the most efficient way to go in terms of modularity, readability, scalability and maintainability.
我认为这是在模块化、可读性、可扩展性和可维护性方面最有效的方法。
Specifically, the question code would be traduced into this:具体来说,问题代码将被转化为:
import webOnlyLibrary from "@my-library/web";
import {someWebFunction} from "@my-library/web";
import nodeOnlyLibrary from "@my-library/node";
import {someNodeFunction} from "@my-library/node";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.