Laravel bundle for managing Requirejs in your application
- Clone the repo into your Laravel bundles directory
- Copy the bundle's config/requirejs.php to your application's config directory
- Publish the bundle's assets with
php artisan bundle:publish
The requirejs::optimize
task lets you run the r.js optimizer within the artisan command. You need
to install node on your machine. Specify a build profile
for your modules and set its location in requirejs.php. Then, from the command line, run php artisan requirejs::optimize
.
As an option, you can store your project build configuration with your require configuration. This lets you share shim and path settings between the two. If you use the build_profile configuration, then the profile file will be created at the location specified in build_profile_path automatically when you run the optimize task.
<html>
<head>
...
</head>
<body>
...
{{ Requirejs::load_require() }}
</body>
</html>
<html>
<head>
...
</head>
<body>
...
{{ Requirejs::load_require("js/app/main") }}
</body>
</html>
Require gives you lots of configuration options. The documentation tells you that it can be loaded like this:
<script>
var require = {
"baseUrl" : "js/app/"
"paths" : {
...
}
}
</script>
This bundle lets you centralize your configuration in your application/config/requirejs.php file and then load it into your views. Just be sure to call Requirejs::config()
before Requirejs::load_require()
.
<?php
return array(
...
"config" => array(
"baseUrl" => "js/app/",
"paths" => array(
...
)
)
)
<html>
<head>
...
</head>
<body>
...
{{ Requirejs::config() }}
{{ Requirejs::load_require("js/app/main") }}
</body>
</html>
Since you'll probably do this quite often, there's a method that can combine both calls.
<html>
<head>
...
</head>
<body>
...
{{ Requirejs::require_script_config("js/app/main") }}
</body>
</html>