Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

All core JavaScript files are now defined as libraries. They explicitly define all of their dependencies on other JS and CSS files like jquery.js, drupal.js and Drupal.settings. This solves the Drupal 7 issue #1279226: jQuery and Drupal JavaScript libraries and settings are output even when no JS is added to the page. This change will make possible the use of core JavaScript in the context of modular JavaScript such as AMD or CommonJS.

Core now only loads the JavaScript that is needed to display the page, no more, no less.

To add a script.js file from a module that uses: jQuery, jQuery.once, Drupal, and drupalSettings

In D7:

drupal_add_js(drupal_get_path('module', 'MYMODULE') . '/script.js');

In D8:
MYMODULE.libraries.yml

foo:
  version: 1.0
  js:
    script.js: {}
  dependencies:
    - core/jquery
    - core/drupal
    - core/drupalSettings
    - core/jquery.once
// Later in the module code.
$my_render_array['#attached']['library'][] = 'MYMODULE/foo';
Impacts: 
Module developers
Themers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Not done

Comments

mondrake’s picture

Note that the Drupal settings library name for hook_library_info was changed from drupal.settings to drupalSettings to reflect the change on the javascript side. (see JavaScript settings moved from Drupal.settings to global drupalSettings variable)

nod_’s picture

Thanks for catching it, updated.

elachlan’s picture

Calls to drupal_add_library() will fail without a version being defined.

Otherwise they will get:
Notice: Undefined index: version in drupal_get_library() (line 3742 of core\includes\common.inc).

awasson’s picture

If I am building a module that doesn't use hook_library_info(), how do I tell Drupal that I need to load jQuery?

For instance, I'm building a Drupal 8 module that uses the Libraries module to map and load a library found at /sites/all/libraries/myscripts.

It does exactly what it is supposed to do except the library won't run because it requires jQuery.

As a temporary workaround, I'm using hook_library_info() to load a blank script with dependencies on system jquery but I'm sure there is a clean way to do this. I just can't find it in the docs. Any direction would be great!

Thanks,
Andrew

DigitalFrontiersMedia’s picture

It seems like this method could be self-defeating at times, e.g. a module that injects values into drupalSettings may only need to OPTIONALLY make the values available for anonymous users. However, to do that, the dependency needs to be hardcoded in the D8 module to always make it available even if your use-case doesn't need anonymous users to see it. There must be a way to optionally make drupalSettings available to anonymous users, yes?

--
Web: http://digitalfrontiersmedia.com
Cell: 941.773.2036
Phone: 941.677.DFM1 ( 941.677.3361 )
Skype: DigitalFrontiersMedia
Twitter: DigitalFrontier
--