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:

<?php
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

<?php
// 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

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)

Thanks for catching it, updated.

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).

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