Skip to content

Instantly share code, notes, and snippets.

@markshust
Created November 11, 2022 20:23
Show Gist options
  • Select an option

  • Save markshust/b5a66517a8f4f9d40681945ce2f61672 to your computer and use it in GitHub Desktop.

Select an option

Save markshust/b5a66517a8f4f9d40681945ce2f61672 to your computer and use it in GitHub Desktop.
What is RequireJS?
<!DOCTYPE html>
<html>
<head>
<title>Hi there!</title>
<!-- Load the RequireJS library before all other scripts to be called -->
<script src="js/require.js"></script>
</head>
<body>
<h1>Hi there!</h1>
<script>
/*
* The require.config() function accepts an object
*
* baseUrl defines which folder to reference for JavaScript files
*/
require.config({
baseUrl: "js"
});
/*
* RequireJS expects an array of dependencies as its first argument
*
* Dependencies are defined as strings without the ".js" extension
*
* The second argument is a function callback that accepts any number of
* arguments which are mapped back to the defined dependencies. In this
* case, 'jquery' maps to the $ variable.
*/
requirejs(['jquery'], function($) {
// Dependencies are now accessible within the function callback.
$(function() {
console.log('Hi there!')
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment