Loading min.js files Generated by TypeScript with Require

前端 未结 3 1419
余生分开走
余生分开走 2020-12-19 10:40

I have a full aplication working with Typescript and RequireJs, it is working perfectly. I have now download WebEssentials and it is generating the minified script files. We

相关标签:
3条回答
  • 2020-12-19 11:38

    There isn't any magic to this. You have a few options though

    1. Use the RequireJS optimizer to generate min files for you. Very configurable.
    2. Use the --out compiler option to concatenate all your files into one and minify that
    3. Create a script/batch file to copy your app somewhere else, rename all .min.js files to .js and overwrite the non-min'd ones. Or some other mix but you get the gist
    4. Use the paths config like Steve mentioned. OK for small apps, less than ideal for large.

    For the best performance you should look into the optimizer and determine if the single-script approach is the right decision or multiple smaller libraries is a better approach. I doubt that loading each .min.js file is the ideal solution for you.

    0 讨论(0)
  • 2020-12-19 11:42

    You can use path config to override module paths. You can even fall back to non minified files:

    requirejs.config({
        enforceDefine: true,
        paths: {
            jquery: [
                'lib/jquery.min',
                'lib/jquery'
            ]
        }
    });
    

    There may be a more general way to use min files that I don't know about!

    0 讨论(0)
  • 2020-12-19 11:42

    You can create a simple customized version of require which will conditionally append '.min.js' to all module file requests that would normally just receive the '.js' appendage. Consider the following:

        //Establish a new 'useMinified' boolean in your initial config.
        require.config({ useMinified: true });
    

    Then, edit the require.js library file and modify the 'nameToUrl' function as below:

        //Join the path parts together, then figure out if baseUrl is needed.
    url = syms.join('/');
    
        //new statement
    var extension = config.useMinified ? '.min.js' : '.js';
    
        //customized statement
    url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : extension));
    
    url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
    
    0 讨论(0)
提交回复
热议问题