如何告诉 npm 在安装时使用父项目的依赖项构建模块?
How to tell npm to build module on install with the parent project's dependencies?
我想了解一下 npm 模块在安装时构建的方式,我举个例子:
当我查看 material-ui
npm module sources on GitHub 时,有源代码但没有构建文件,当我查看我的项目 node_modules/material-ui
目录时,我可以看到该目录仅包含构建的文件(es5,uglify)。
我正在尝试了解魔法是如何发生的?我看到 package.json
中有构建脚本,但没有任何内容告诉 npm 在安装时 运行 它,我错过了什么?
谢谢
通常模块不会在客户端的机器上构建,因为这会花费额外的时间并且可能会失败,因为它们使用的是不支持构建工具的旧版本 Node.js,并且当然,还需要安装构建工具,这会使过程变得更长。相反,您在发布之前构建它。 GitHub 上的内容与实际发布到 npm 注册表的内容不同。大多数模块不会将内置源签入 GitHub(尽管有些人更喜欢这样做)。
大概 material-ui
手动执行此过程并仅发布构建的源,如 Unpkg - material-ui 中所示。
一些其他包,例如 redux use a prepublish
hook, which builds the necessary sources just before it gets published when running npm publish
(Redux prepublish hook), which reflects the published package as you can see in Unpkg - Redux. It's pretty close to the original source on GitHub but only contains the relevant files, including built files that are in its .gitignore file. Because a lot of files are unnecessary to be published (e.g. the test
directory, rollup.config.js
etc.) and would only take up space on the client, you can specify files
in package.json
to only publish the listed files (Redux files)。
你只是碰巧在发布时选择了一个相当混乱的包 material-ui
,而 redux
更容易理解。
我想了解一下 npm 模块在安装时构建的方式,我举个例子:
当我查看 material-ui
npm module sources on GitHub 时,有源代码但没有构建文件,当我查看我的项目 node_modules/material-ui
目录时,我可以看到该目录仅包含构建的文件(es5,uglify)。
我正在尝试了解魔法是如何发生的?我看到 package.json
中有构建脚本,但没有任何内容告诉 npm 在安装时 运行 它,我错过了什么?
谢谢
通常模块不会在客户端的机器上构建,因为这会花费额外的时间并且可能会失败,因为它们使用的是不支持构建工具的旧版本 Node.js,并且当然,还需要安装构建工具,这会使过程变得更长。相反,您在发布之前构建它。 GitHub 上的内容与实际发布到 npm 注册表的内容不同。大多数模块不会将内置源签入 GitHub(尽管有些人更喜欢这样做)。
大概 material-ui
手动执行此过程并仅发布构建的源,如 Unpkg - material-ui 中所示。
一些其他包,例如 redux use a prepublish
hook, which builds the necessary sources just before it gets published when running npm publish
(Redux prepublish hook), which reflects the published package as you can see in Unpkg - Redux. It's pretty close to the original source on GitHub but only contains the relevant files, including built files that are in its .gitignore file. Because a lot of files are unnecessary to be published (e.g. the test
directory, rollup.config.js
etc.) and would only take up space on the client, you can specify files
in package.json
to only publish the listed files (Redux files)。
你只是碰巧在发布时选择了一个相当混乱的包 material-ui
,而 redux
更容易理解。