1

We have a suite of similar apps, all being built from the same codebase. There are currently 5 different main.js files, and they all repeat the same

requirejs.config({
  paths: { foo foo foo },
  shim: { bar bar bar }
});

in each main. Ideally, I could keep this DRY by only defining these values once. Is it possible to define them somewhere external to the main.js file?

Thanks in advance!

2 Answers 2

0

I've created my own personal distribution of Require.JS for this purpose.

It is basically the original Require.JS code with the configuration (paths and shims) concatenated to the bottom. It means my projects never have to declare where to find jQuery, etc. Pretty handy.

Note: my individual projects may still call require.config in order to set project-specific locations, etc. This is technically calling it a second time (as the first time was called when loading Require.JS) but it works fine according to my tests.

0

Delete 4 of these files and change your tags from:

<script data-main="js/main1.js" src="[requirejs path]"></script>
<script data-main="js/main2.js" src="[requirejs path]"></script>
<script data-main="js/main3.js" src="[requirejs path]"></script>
<script data-main="js/main4.js" src="[requirejs path]"></script>
<script data-main="js/main5.js" src="[requirejs path]"></script>

to

<script data-main="js/main1.js" src="[requirejs path]"></script>
<script data-main="js/main1.js" src="[requirejs path]"></script>
<script data-main="js/main1.js" src="[requirejs path]"></script>
<script data-main="js/main1.js" src="[requirejs path]"></script>
<script data-main="js/main1.js" src="[requirejs path]"></script>

Then put a small inline snippet in each file that launches a specific app, e.g.

<script type="text/javascript">
require(['app1'], function(App) { App.init() }) 
</script>

and

<script type="text/javascript">
require(['app2'], function(App) { App.init() }) 
</script>

etc

1
  • How could this result in running different apps? May 22, 2013 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.