ChatGPT解决这个技术问题 Extra ChatGPT

How to install jQuery with Composer?

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

    {
        "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": "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 solution to help @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"
                }
            }
        }
    ]
Currently I'm using a CDN, but I'd like to get jQuery by Composer or git submodules as well. I don't like the idea of putting 3rd party libraries into my repos. BTW, JS popularity on GitHub is overrated because of all this JS copypasta taking place.
2013 is nearly over now and there's still not jquery/jquery package. This is really weird.
Choco! Hello! Now you have jQuery js file in the vendor directory and what now? Do you have to add src to this file manually or composer have to load it by himself? I am trying to load jQuery automaticaly and inlude it to my site. Is is possible or I don't understand what Composer using for?
@SharikovVladislav Composer is for managing dependencies (ie. finding the right versions, checking that there aren't conflicts, downloading everything, etc). How you use those dependencies is a separate issue. If they are PHP dependencies, they will normally be autoloaded and all you have to do is create objects or call functions directly. If they are JS files or other assets, you have to add the path to the file in your list of assets to be compiled/minified, or if you don't use minification just create a symlink to the file in your webroot and add the src manually.
"... or if you don't use minification just create a symlink...". That would mean you'd have to have your vendors dir under your web root. Isn't that insecure?

C
César

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

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

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


This doesn't have anything related to the javascript components package management does it?
components/jquery is a Shim repository for jQuery and the same repo works with Bower, Component and Composer
Done composer require components/jquery without version and worked like a charm. It added me the line "components/jquery": "^2.1" to the .json and proceeded to install it and glue it into the .lock - thanks for the components/* tip!
j
j0k

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/

EDIT :

The feature actually exists but wasn't documented.

"type": "file"

Thanks, I filed this issue just now: github.com/composer/composer/issues/946
Stof commented on the issue, the feature exists. I edited your answer.
I'll try that. I think what I'm missing is a way to put the files on the web directory. Creating a bundle just for assets is an overkill. It should be possible to install js files directly to the web directory.
How does this work? I just want to get Composer to download a single text file over HTTP? Where do I specify everything?
@CMCDragonkai I edited my post to add the full solution
s
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.

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

{
  "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": "*"
      }
    }
  }
}

Caution: it is worth noticing that the package names, as processed by the Asset Manager plugin, are all lowercase. If you used upper-case letters packages in the "require", they will never be matched by the plugin even if you write them the same way in its "packages" section.
Also note event hooks - getcomposer.org/doc/articles/scripts.md. I am using these now. KISS.
C
CecilMerrell aka bringrainfire

you can install jquery by using npm like so,

npm install jquery

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


s
snoob dogg

Use npm, yarn or WebPack instead of Composer they are way better for that kind of needs.


Bower is already deprecated and no longer maintained - not a good choice in 2018.
@rob006 what to use instead ? Webpack ?
There are recommendations on bower's website - probably yarn and webpack.