问题描述
在我加入的项目中,这是 node_packages
的架构:
On the project I am joining this is the architecture for the node_packages
:
|- Django project
|-- app1
|-- app2
|-- node_modules
|--- foundation-sites
|--- grunt
|-- static
|--- css
|--- images
|--- js
|--urls.py
|--settings.py
|--package.json
我个人认为 node_packages
应该在 js
文件夹下的 static 以及 package.json
像这样:
I personally think node_packages
should be in the static under the js
folder as well as the package.json
like so:
|- Django project
|-- app1
|-- app2
|-- static
|--- css
|--- images
|--- js
|---- node_modules
|----- foundation-sites
|----- grunt
|---- packages.json
|--urls.py
|--settings.py
有区别吗?这是最佳实践?为什么?
is there a difference? which is best practice? why?
推荐答案
我理解您想将所有与 javascript 相关的文件保存在一个地方的想法,但以下是您可能希望保留 的几个原因node_modules
文件夹和 Django 应用程序的 static
目录中的 package.json
文件.
I understand your thinking of wanting to keep all the javascript related files in one place, but here are a couple of reasons you might want to keep the node_modules
folder and the package.json
file out of a Django app's static
directory.
- 您最终可能会以静态方式提供不应该提供的文件.如果您的生产环境中存在
node_modules
文件夹,请运行collectstatic
每次都必须检查它是否同步,由于节点嵌套的依赖结构,这可能会很慢.假设您有一个构建步骤来捆绑和转译您的 JS,如果这些源文件在static
中,它们也将毫无理由地作为静态文件提供. - 您可能希望将 node 用于 JavaScript 构建过程之外的更多用途.我看到您正在使用 Grunt,并且您可能希望将它用于超出 JavaScript 需求的范围,例如缩小你的
css
,或者在你的 Django 开发服务器周围运行一个代理服务器,当文件更改或 Django 服务器重新启动时,它会自动重新加载你的浏览器.考虑到这一点,将 Node.js 视为构建过程中的一种工具可能会更有意义,它可以涉及项目的任何部分,JavaScript 的捆绑/转换只是其中的一部分.
- You'll likely end up statically serving files that aren't meant to be. If the
node_modules
folder exists in your production environment, runningcollectstatic
will have to check that it's in sync every time, which can be slow due to nodes nested dependency structure. And assuming you have a build step to bundle and transpile your JS, if those source files are withinstatic
, they too will be served as static files, for no reason. - You might want to use node for more than just your JavaScript build process. I see you're using Grunt, and you may want to use it to for more than your JavaScript needs, like minifying your
css
, or running a proxy server around your Django dev server that auto-reloads your browser when files change or the Django server restarts. With this in mind, it might make more sense to think of Node.js as a tool in your build process that could touch any part of your project, the bundling/transpiling of JavaScript being only one part of that.
这篇关于django npm 和 node 包架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!