Requirejs optimiser - Create minified groupings of

2019-05-28 15:23发布

问题:

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.