0

How can I add a jQuery plugin without a module for use in the whole site.

I found things about how to add it in modules: Adding a jQuery Library to Magento 2

But can I add it without a module and if yes in which should requirejs-config.js be placed?

0

Best thing to do is use a Magento 2 Module or a Theme to include such plugins/libraries. It is the recommended way and the best practice. Is there anything blocking you from doing that? If so, pls share.


Method 1 > THEME: If the javascript/jquery library is theme related (In order to change the look and feel of the system).

  • Place the custom component source file in one of the following location
    [theme_dir]/web/js/
  • Place your requirejs-config.js file in
    [theme_dir]

Method 2 > MODULE: If the javascript/jquery library is related to a particular business function or handles a Magento feature. It should go inside a module.

  • Place the custom component source file in one of the following location
    [module_dir]/view/frontend/web/js/

  • Place your requirejs-config.js file in
    [module_dir]/view/frontend/

Magento 2 strongly recommends not changing the source code of default Magento components and widgets. All customizations must be implemented in custom modules or themes.



[EDITED: To provide a example as per in the first comment]

Using method 1 for including a js file.

File: [Root_Dir]/app/code/[NameSpace]/[Module]/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            faq:'Namespace_Module/js/faq'
        }
    }
};

Then the file related to this mapping will be there on the same module directory.

File: [Root_Dir]/app/code/[NameSpace]/[Module]/view/frontend/web/js/faq.js

//faq.js
define([
    "jquery"
], function ($) {
    alert('TEST');
});

So the script path in the require-config.js file should be specified from modules view/frontend/web directory, without the .js extension.

This way, a script can be added to the frontend. Follow the default module ./vendor/magento/module-checkout/* or similar...

EDIT 2: If you want to debug and see from where Magento is trying to pull the static file, follow the article on http://www.magleaks.com/magento-2-static-file-resolving/ and add a debug pointer to the file [DOC_ROOT]/vendor/magento/framework/View/Design/FileResolution/Fallback/Resolver/Simple.php

  • Thanks, I am trying Method 1. The change in requirejs-config.js works and apears in the source code. But my js file in web/js/main.js does not apear. I added this to requiresjs-config.js: var config = { map: { '*': { "js/main": {} } } }; – Oliver Schmid Mar 28 '16 at 12:26
  • I'll add a sample code shortly, check that and provide more information... – Dilhan Maduranga Mar 28 '16 at 17:04
  • I added my sample code. My problem is I don't have a module. And I try to do it in the theme. But my js does not load. – Oliver Schmid Mar 29 '16 at 6:40
  • It tries to load my file only if I request it in a phtml file. Now I get it to look for my file in the pub/static folder. But that file doesn't move into the static folder and I get a 404. – Oliver Schmid Mar 29 '16 at 13:41
  • I made a new entry, as it seems to be a different problem. magento.stackexchange.com/questions/108484/… – Oliver Schmid Mar 30 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.