php 如何使用 Composer 安装 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11663559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:49:34  来源:igfitidea点击:

How to install jQuery with Composer?

phpsymfonycomposer-php

提问by ChocoDeveloper

I have been able to install repositories that do not have a composer.json file like this:

我已经能够安装没有这样的 composer.json 文件的存储库:

    {
        "type": "package",
        "package": {
            "name": "yahoo/yui-compressor",
            "version": "2.0.4",
            "dist": {
                "url": "http://yui.zenfs.com/releases/yuicompressor/yuicompressor-2.4.7.zip",
                "type": "zip"
            }
        }
    },

I took the "type": "zip" part from the docs, but I couldn't find many other types. For example, I need to install jQuery, but I don't know what to put in type ("js" did not work).

我从文档中获取了 "type": "zip" 部分,但我找不到许多其他类型。例如,我需要安装 jQuery,但我不知道该输入什么类型(“js”不起作用)。

    {
        "type": "package",
        "package": {
            "name": "jquery/jquery",
            "version": "1.7.2",
            "dist": {
                "url": "http://code.jquery.com/jquery-1.7.2.js",
                "type": "js"
            }
        }
    }

Any ideas?

有任何想法吗?

EDIT: I'm adding the full solutionto help @CMCDragonkai:

编辑:我正在添加完整的解决方案来帮助@CMCDragonkai:

    "require": {
        "vendorname/somefile": "1.2.3",
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "vendorname/somefile",
                "version": "1.2.3",
                "dist": {
                    "url": "http://example.com/somefile.txt",
                    "type": "file"
                }
            }
        }
    ]

采纳答案by naderman

This is simply a missing feature. There should probably be a new type of dist which is just a single plaintext file to be downloaded and left as-is. Please file a feature request on the github issue tracker: https://github.com/composer/composer/issues/

这只是一个缺失的功能。可能应该有一种新的 dist 类型,它只是一个要下载并保持原样的纯文本文件。请在 github 问题跟踪器上提交功能请求:https: //github.com/composer/composer/issues/

EDIT :

编辑 :

The feature actually existsbut wasn't documented.

该功能实际上存在,但没有记录。

"type": "file"

回答by César

Actually there is an easier way to install jQuery, just type:

实际上有一种更简单的方法来安装 jQuery,只需输入:

{
    "require": {
        "components/jquery": "1.9.*"
    }
}

It uses Component Installer for Composerand by default all assets from Component are installed under components, but it can be customize. (see docs).

为 Composer使用Component Installer,默认情况下components,Component 中的所有资产都安装在 下,但可以自定义。(见文档)。

回答by spinkus

As outlined already, part one of the solution is defining you own repositories and the "type: ": "file"repository definition option. But a subsequent issue is getting composer to put the JQuery where you want it. As it stands, composer seems to be limited to downloading dependency source under vendor-dir(which is annoying but probably related to autoloading requirements). The general fix to this limitation is to write a composer plugin that overcomes it. Seems to be a few plugins that can manage this. The simplest most lightweight solution I've found is PHP Composer Asset Manager, which is dedicated to managing non PHP/Composer "assets". Though, it has at least one limitation in that changes that the plugin makes are not managed/detected by composer. Still usable.

如前所述,解决方案的第一部分是定义您自己的存储库和"type: ": "file"存储库定义选项。但随后的问题是让作曲家将 JQuery 放在您想要的位置。就目前而言,composer 似乎仅限于下载依赖源vendor-dir(这很烦人,但可能与自动加载要求有关)。解决此限制的一般方法是编写一个 Composer 插件来克服它。似乎是一些可以管理这个的插件。我发现的最简单最轻量级的解决方案是PHP Composer Asset Manager,它专门用于管理非 PHP/Composer “资产”。尽管如此,它至少有一个限制,即插件所做的更改不是由 Composer 管理/检测的。还是可以用的。

Here's a full composer.jsonto install JQuery using that plugin:

这是composer.json使用该插件安装 JQuery的完整内容:

{
  "name": "foo/bar",
  "require":
  {
    "phpclasses/assets": "*",
     "jquery/jquery": "*"
  },
  "repositories": [
    {
     "type": "composer",
     "url": "http://www.phpclasses.org/"
    },
    {
      "type": "package",
      "package": {
        "name": "jquery/jquery",
        "version": "1.7.2",
        "type": "jquery",
        "dist": {
          "url": "http://code.jquery.com/jquery-1.7.2.js",
          "type": "file"
        }
      }
    }
  ],
  "extra": {
    "assets": {
      "actions": [
        {
          "type": "copy",
          "target": "webroot/js",
          "pattern": "\.js$"
        }
      ],
      "packages": {
        "jquery/jquery": "*"
      }
    }
  }
}

回答by snoob dogg

Use npm, yarnor WebPackinstead of Composer they are way better for that kind of needs.

使用npmyarn或者WebPack代替 Composer,它们更适合这种需求。

回答by cecil merrel aka bringrainfire

you can install jquery by using npm like so,

你可以像这样使用 npm 安装 jquery,

npm install jquery

https://www.npmjs.com/package/jquery

https://www.npmjs.com/package/jquery