简体   繁体   English

为client和node.js发布库

[英]Publish a library for client and node.js

Following my previous question, I have followed webpack guidelines of using multiple targets to build my NPM package. 根据我之前的问题,我遵循webpack使用多个目标构建我的NPM包的指导原则。 https://webpack.js.org/concepts/targets/#multiple-targets https://webpack.js.org/concepts/targets/#multiple-targets

I now have two different output files, index.js which is the browser bundle and index.node.js which is obviously supposed to run on the backend. 我现在有两个不同的输出文件,index.js是浏览器包和index.node.js,显然应该在后端运行。

The app should run on both browser and Node, the code is mostly reused but there is some big difference around accessing files etc. which means I do need two separate files depending on what platform should the app run. 该应用程序应该在浏览器和Node上运行,代码主要是重用,但是在访问文件等方面存在一些很大的不同,这意味着我需要两个单独的文件,具体取决于应用程序运行的平台。

My question is how should I publish this library in a way that user can consume it like import {//SOME OBJECT} from 'my-published-library' when they import it from NPM regardless if they are using it on the browser or in their node application? 我的问题是我应该如何以一种方式发布这个库,用户可以在从NPM导入它时import {//SOME OBJECT} from 'my-published-library'无论他们是在浏览器上使用它还是在他们的节点应用? If I try that at the moment it always defaults to index.js which works in the browser but not in Node. 如果我现在尝试它,它总是默认为index.js,它在浏览器中工作但在Node中不工作。

Not sure if this is what you're looking for but if you are installing for the browser, the package.json provides a field to set the entry point. 不确定这是否是您正在寻找的,但如果您要为浏览器进行安装,则package.json提供一个字段来设置入口点。

https://docs.npmjs.com/files/package.json#browser https://docs.npmjs.com/files/package.json#browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM