简体   繁体   English

如何使用 Composer 安装的 Javascript 或 CSS 文件?

[英]How to use Javascript or CSS files installed by Composer?

How do I actually use the frontend files that Composer installs?我如何实际使用 Composer 安装的前端文件? Specifically JavaScript.特别是 JavaScript。 I've been able to use a PHP file previously, but JavaScript files are breaking my brain...我以前可以使用 PHP 文件,但是 JavaScript 文件让我大吃一惊……

I've installed Composer and used it to install a package (Handlebars, specifically).我已经安装了 Composer 并用它来安装 package(特别是把手)。 Everything looks like it should.一切看起来都应该如此。 I've got a handlebars folder in my vendor folder and it created a "components" folder with some JavaScript files in it, so I know things are there.我的供应商文件夹中有一个车把文件夹,它创建了一个“组件”文件夹,其中包含一些 JavaScript 文件,所以我知道那里有东西。 I've tried just doing this:我试过这样做:

<script type="text/text/javascript"
    src="/components/handlebars/handlebars.js"></script>`

But that doesn't seem like what I should be doing and doesn't seem to work anyway.但这似乎不是我应该做的,而且似乎也不起作用。 I see there's some require files in there as well, but I've tried referencing them with no luck.我看到那里也有一些需要文件,但我尝试引用它们但没有运气。

I would hope that just including require __DIR__. '/vendor/autoload.php';我希望只包括require __DIR__. '/vendor/autoload.php'; require __DIR__. '/vendor/autoload.php'; would do it, but that seems to just bring in the backend stuff.会这样做,但这似乎只是带来了后端的东西。

It seems that Bower may be better suited for frontend stuff like this, but I'm already using Composer for another part of the project, so it would be nice to not have to use two different package managers in one project.似乎 Bower 可能更适合这样的前端东西,但我已经在项目的另一部分使用 Composer,所以不必在一个项目中使用两个不同的 package 管理器会很好。

If you are using any kind of front-end buildchain, you can simply point the build-chain to the location of your JS package in vendor , and only put the "built" assets in your web accessible directory (called public in many projects).如果您使用任何类型的前端构建链,您可以简单地将构建链指向您的 JS package 在vendor中的位置,并且只将“构建”资产放在您的 web 可访问目录中(在许多项目中称为public ) .

If you are not, you'll have to simply either copy or symlink the JS (and/or CSS) dependencies from its vendor path to one that's web accessible.如果不是,您只需将 JS(和/或 CSS)依赖项从其vendor路径复制或符号链接到 web 可访问的路径。

Composer is mainly a PHP dependency manager, and while you can use it to install some frontend packages, usually you'll be better served by a manager build specifically for frontend. Composer 主要是一个PHP依赖管理器,虽然您可以使用它来安装一些前端包,但通常会更好地为您提供专门为前端构建的管理器。 As soon as your project reaches a minimum level of complexity, separating the needs of front and back is usually good, since front will have more specific and appropriate tools both to manage dependencies and to build assets.一旦您的项目达到最低程度的复杂性,分离前端和后端的需求通常是好的,因为前端将拥有更具体和更合适的工具来管理依赖关系和构建资产。

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

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