Skip to content

Instantly share code, notes, and snippets.

@davidjguru
Last active January 12, 2020 19:40
Show Gist options
  • Select an option

  • Save davidjguru/23b85a0dfea3ebf311245110a42316aa to your computer and use it in GitHub Desktop.

Select an option

Save davidjguru/23b85a0dfea3ebf311245110a42316aa to your computer and use it in GitHub Desktop.
Drupal 8: Including JavaScript files and libraries in your Drupal project.
// Case 1: Basic library file with only JavaScript dependencies.
module_name.library_name:
js:
js/hello_world.js: {}
// Case 2: Typical file with CSS and JavaScript dependencies.
foo:
version: 1.x
css:
theme:
css/foo.css: {}
js:
js/foo.js: {}
// Case 3: JavaScript library with jQuery dependency from Drupal Core.
module_name.library_name:
js:
js/foo.js: {}
dependencies:
- core/jquery
// Case 4: Loading JavaScript libraries in head.
js_library_for_header:
header: true
js:
header.js: {}
js_library_for_footer:
js:
footer.js: {}
// Case 5: Adding external resources from your libraries.yml file.
backbone:
remote: https://github.com/jashkenas/backbone
version: "1.2.3"
license:
name: MIT
url: https://github.com/jashkenas/backbone/blob/1.2.3/LICENSE
gpl-compatible: true
js:
assets/vendor/backbone/backbone-min.js: { weight: -19, minified: true }
dependencies:
- core/underscore
// Case 6: Declaring dependencies
js_library_hide_show:
js:
js/my_custom_javascript_library.js: {}
dependencies:
- core/jquery
// Case 7: Fully external library
angular.angularjs:
remote: https://github.com/angular/angular.js
version: 1.4.4
license:
name: MIT
url: https://github.com/angular/angular.js/blob/master/LICENSE
gpl-compatible: true
js:
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js: { type: external, minified: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment