Requirejs optimiser - Create minified groupings of scripts for sections of the site

只谈情不闲聊 提交于 2019-12-08 02:01:07

问题


I'm using the requirejs optimiser and I have a lot of JavaScript files for this webapp. I would like to create separate minified scripts which group scripts for certain sections of the site.

My question is: how do I create minified groups of scripts and how I prevent script libraries for example JQuery from being added into these module groups which already exist in the scripts.min.js core script?

({
    baseUrl: '../static/js',
    mainConfigFile: '../static/js/main.js',
    name: 'main',
    out: '../static/js/scripts.min.js',
    paths: {
        requireLib: 'vendor/require/require.min'
    },
    include: ['requireLib', '_core']
})

The current setup above adds all non nested requires plus all the scripts inside _core module and combines it to create one script that is essential for every page.

Now I need to create separate minified scripts for search, user management etc I would like each section (which contains many files) to have its own minified and concatenated script.

For example the user management pages would have these script includes:

    <script type="text/javascript" src="/static/js/scripts.min.js"></script>
    <script type="text/javascript" src="/static/js/user_management.min.js"></script>

I have been following this tutorial to get the optimiser running: http://www.youtube.com/watch?v=m6VNhqKDM4E


回答1:


Check out modules in the requirejs example build

You can set a common build file that includes all shared scripts

And then you can have separate modules for each other page

Use include[] and exclude[] to include modules in the built files




回答2:


Since you're separating your compiled scripts into separate files, I'm guessing you need scripts.min.js to act as a shared set of components across multiple pages.

If so, the problem you're describing is solved in the example-multipage and example-multipage-shim repositories. They are by the RequireJS creator and are pretty straightforward.

This question and its accepted answer are also great references to the options available for a multipage config with RequireJS.



来源:https://stackoverflow.com/questions/19345796/requirejs-optimiser-create-minified-groupings-of-scripts-for-sections-of-the-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!