简体   繁体   English

如何为后端和前端使用一个包管理器? (纱线/NPM)

[英]How to use one package manager for backend and frontend? (Yarn/NPM)

Previously I would use NPM for server side stuff and Bower for frontend.以前,我将 NPM 用于服务器端,而 Bower 用于前端。 NPM would install to the node_modules/ directory and I had a .bowerrc to install those packages to public/lib . NPM 将安装到node_modules/目录,我有一个.bowerrc将这些包安装到public/lib

Now I've begun to use Yarn instead of NPM;现在我已经开始使用 Yarn 而不是 NPM; and since they are pretty much 1:1 compatible it has been a smooth change.. However, now I'm wondering if I can ditch Bower and just use Yarn for everything.并且由于它们几乎是 1:1 兼容的,因此这是一个平滑的变化.. 但是,现在我想知道我是否可以放弃 Bower 而只使用 Yarn 来处理所有事情。 But I do not see a way (in either NPM or Yarn) to install normal packages to node_modules, but then have a sort of separate option to put some packages in public/ ... Something similar to the --dev option but instead it would be yarn add jquery --public .但是我没有看到一种方法(在 NPM 或 Yarn 中)将普通包安装到 node_modules,但是有一种单独的选项可以将一些包放在public/ ... 类似于--dev选项的东西,而是它将yarn add jquery --public Does anything like this exist?这样的东西存在吗?

One option, of course, would be to install all packages to the public/ directory but I believe this is a bad practice (correct me if I'm wrong).当然,一种选择是将所有软件包安装到public/目录,但我认为这是一种不好的做法(如果我错了,请纠正我)。

Another option might be to have a separate package.json in the public/ folder and run separate Yarn commands in there for frontend packages, but it's just a bit less convenient, I think;另一种选择可能是在public/文件夹中有一个单独的package.json并在其中为前端包运行单独的 Yarn 命令,但我认为它不太方便; more error prone.更容易出错。

The norm pretty much is something like:规范几乎是这样的:

  1. Install packages via yarn通过yarn安装包
  2. Use a task runner such as gulp , or module loader & bundler such as webpack to create the bundles you need.使用任务运行如gulp ,或模块加载器和诸如捆绑webpack创建你所需要的包。
    • This all depends on your stack, there are obviously others, for example in the past I've used .NET's native bundling, which you can achieve the same as gulp/webpack... again this is for you to decide.这一切都取决于您的堆栈,显然还有其他的,例如过去我使用过 .NET 的本机捆绑,您可以实现与 gulp/webpack 相同的效果……这又是由您决定的。

The gist of it is, install all the modules in a central place ( using a package manager) , then have a loader or other process grab and move the required assets to the location that you need to use them.它的要点是,将所有模块安装在一个中心位置(使用包管理器) ,然后让加载程序或其他进程抓取并将所需资产移动到您需要使用它们的位置。

I'd suggest having a look at a kickstart project (pertaining to the stack that you're using) eg: I've used : Fountainjs in the past with success, or look into Yeoman if fountain doesnt strike your fancy (fyi. Fountain is built on top of Yeoman)我建议查看一个 kickstart 项目(与您正在使用的堆栈有关)例如:我曾经使用过: Fountainjs过去取得了成功,或者如果喷泉没有引起您的兴趣,请查看Yeoman (仅供参考。Fountain建立在 Yeoman 之上)

In pseudo commands:在伪命令中:

1. yarn install     /// installs all assets
2. gulp build       /// cleans out current public folder, bundles & copies new
3. gulp serve       /// runs your app

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

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