RequireJS: Multiple main.js?

后端 未结 2 769
忘掉有多难
忘掉有多难 2021-01-31 08:51

I have been building a single page app using requireJS and so far loving it. I have come to the point of developing other parts of the site outside of the main app and am not re

相关标签:
2条回答
  • 2021-01-31 09:30

    The requirejs team has some samples for multipage applications on github. Have a look at: https://github.com/requirejs/example-multipage

    Basically you are going to have the following structure:

    • page1.html: page 1 of the app.

    • page2.html: page 2 of the app.

    • js app: the directory to store app-specific modules.

    • lib: the directory to hold third party modules, like jQuery.

    • common.js: contains the requirejs config, and it will be the build target for the set of common modules.

    • page1.js: used for the data-main for page1.html. Loads the common module, then loads app/main1, the main module for page 1.

    • page2.js: used for the data-main for page2.html. Loads the common module, then loads app/main2, the main module for page 2.

    0 讨论(0)
  • 2021-01-31 09:39

    So require.js should always be used on any page to allow for modularity and a clean namespace. I believe each 'app' needs it's own main.js script. When optimizing your site r.js allows for you to exclude modules from the compilation which you should do for jQuery always.

    That way require.js will always load jquery.js on the fly and most of the time from the cache. Finding other modules that might be cached between your app and homepage will have to be done at your own discretion and depends on the flow of your users and other factors.

    It sounds like you have two projects, an app and a marketing site. I believe those should be separated to quite an extend and should have their own respective 'js' folders containing their own main.js.

    0 讨论(0)
提交回复
热议问题