Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active March 25, 2021 03:56
Show Gist options
  • Save cowboy/7897268 to your computer and use it in GitHub Desktop.
Save cowboy/7897268 to your computer and use it in GitHub Desktop.
require.js ideas

index.html

<script>
document.write('http://' + location.hostname + ':35729/livereload.js?snipver=1');
</script>
<script src="config.js"></script>
<script data-main="app" src="require.js"></script>

config.js

var require = {
  paths: {
    'jquery': 'bower_components/jquery/jquery',
  },
};

app.js

define(function(require) {
  var $ = require('jquery');
  // app code here
});

index.html

<script data-main="app-bootstrap" src="require.js"></script>

config.js

require.config({
  paths: {
    'jquery': 'bower_components/jquery/jquery',
    // This must work in the browser AND not explode in the r.js build step.
    'livereload': 'http://' + (typeof location !== 'undefined' ?
      location.hostname : 'localhost') + ':35729/livereload.js?snipver=1',
  },
});

app-bootstrap.js

require(['config'], function() {
  require(['livereload']);
  require(['app']);
});

app.js

define(function(require) {
  var $ = require('jquery');
  // app code here
});
@jugglinmike
Copy link

Probably lipstick on a pig, as far as you're concerned, but:

- require(['livereload']);
- require(['app']);
+ require(['app', 'livereload']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment