简体   繁体   English

仅使用非捆绑依赖项发布 NPM package

[英]Publish NPM package with only non-bundled dependencies

Lets say we're developing a small javascript library L .假设我们正在开发一个小型 javascript 库L

The code is in ES6.代码在 ES6 中。 To use some utility function, like debounce , we install lodash as a dependency.要使用一些实用程序 function,比如debounce ,我们安装 lodash 作为依赖项。

At build the webpack transpiles the code, bundling the tree shaked lodash code, and we end up with a nice little javascript file we want to publish and share as a npm package. At build the webpack transpiles the code, bundling the tree shaked lodash code, and we end up with a nice little javascript file we want to publish and share as a npm package.

Now, the package.json file lists lodash as a dependency.现在, package.json文件将 lodash 列为依赖项。 But that is only true at build time, it is not really needed in production.但这仅在构建时是正确的,在生产中并不是真正需要的。

What's the proper way to handle this kind of situation?处理这种情况的正确方法是什么? Does it make sense to consider lodash a devDependency?将 lodash 视为 devDependency 是否有意义? As such, only webpack's externals would be "real" dependencies?因此,只有 webpack 的externals组件才是“真正的”依赖项?

Or should we somehow tamper the package.json file before publishing it?还是我们应该在发布之前以某种方式篡改package.json文件?

Do you know any real examples of projects handling this question?你知道处理这个问题的项目的任何真实例子吗?

Webpack "merges" the code of the project with that of the used code of the non-external dependencies into some bundle.js file. Webpack 将项目的代码与非外部依赖项的使用代码“合并”到一些bundle.js文件中。 This file is then published to NPM, along with a package.json file, which lists all of the dependencies, independently of these being external or embedded.然后,此文件与 package.json 文件一起发布到package.json文件,其中列出了所有依赖项,独立于外部或嵌入式。

All of the code of packages referenced in dependencies (or optionalDependencies , or peerDependencies ) is expected to be "production" code. dependencies项(或optionalDependenciespeerDependencies )中引用的所有包代码都应该是“生产”代码。 While code in devDependencies is expected to be used only at "development" time, and is thus "development" code.devDependencies中的代码预计仅在“开发”时使用,因此是“开发”代码。 Under this principle, I believe that it would be incorrect to declare non-external dependencies as development dependencies.在这个原则下,我认为将非外部依赖声明为开发依赖是不正确的。

However, if all dependencies, embedded or external, are declared equally in the published package.json file, there is no way for a runtime environment to know which are the real dependencies that need to be made available to the package — the ones that the package will import at runtime and which better be available.但是,如果在已发布的package.json文件中同样声明了所有依赖项,无论是嵌入的还是外部的,那么运行时环境就无法知道哪些是需要提供给 ZEFE90A8E604A7C840E88D03 的真正依赖项。 package 将在运行时导入并且更好地可用。

For the Node.js environment, bundles and Webpack are not normally used, so this never came to be an issue — all dependencies are always installed (never merged/bundled).对于 Node.js 环境,通常不使用捆绑包和 Webpack,因此这永远不会成为问题 — 始终安装所有依赖项(从不合并/捆绑)。

However, if you use the package.json file to drive some web-packages runtime environment, the way that dependencies are currently included in published package.json is not suitable. However, if you use the package.json file to drive some web-packages runtime environment, the way that dependencies are currently included in published package.json is not suitable.

You might want to take a look at Pika Web's devDependencies package.json property, which aims to solve a comparable problem (even though their mojo is "a future without Webpack").您可能想看看Pika Web 的devDependencies package.json属性,该属性旨在解决类似的问题(即使他们的 mojo 是“没有 Webpack 的未来”)。 They also introduce the concept of publishing a different package.json file than that which is checked-in (tampering the package.json before publishing, as you say). They also introduce the concept of publishing a different package.json file than that which is checked-in (tampering the package.json before publishing, as you say).

Interestingly, it looks like a related tool, Pika Pack, caught the attention of NPM folks and is being considered to become part of NPM .有趣的是,它看起来像一个相关的工具 Pika Pack,引起了 NPM 人的注意,并被考虑成为 NPM 的一部分 So, maybe , NPM has become aware of the specific package format needs of web projects' workflows.所以,也许,NPM 已经意识到 web 项目工作流程的特定 package 格式需求。

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

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