最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

minify - Minified Javascript doesn't work when combined in one file - Stack Overflow

programmeradmin6浏览0评论

I have 3 JS libraries that I use that are in their own separate files. They are mented with the code minified in each individual file

file 1: /

file 2: /

file 3: /

Now individually they work fine in my app, don't throw any errors. But I wanted to load less files so I bined them into one file like this: /

However, in Chrome it throws an error on the last line of the file:

Uncaught TypeError: undefined is not a function 

and it then it doesn't work anymore. I didn't change anything in the code before I bined them, I just copy and pasted the content of the first 3 files into the new one, and it doesn't work anymore. Don't understand why bining them into one file seems to break functionality

Was hoping someone would have an idea what's going here?

I have 3 JS libraries that I use that are in their own separate files. They are mented with the code minified in each individual file

file 1: http://jsfiddle/NGMVa/

file 2: http://jsfiddle/AzEME/

file 3: http://jsfiddle/qVkhn/

Now individually they work fine in my app, don't throw any errors. But I wanted to load less files so I bined them into one file like this: http://jsfiddle/Gxswy/

However, in Chrome it throws an error on the last line of the file:

Uncaught TypeError: undefined is not a function 

and it then it doesn't work anymore. I didn't change anything in the code before I bined them, I just copy and pasted the content of the first 3 files into the new one, and it doesn't work anymore. Don't understand why bining them into one file seems to break functionality

Was hoping someone would have an idea what's going here?

Share Improve this question edited Feb 7, 2013 at 5:00 sth 230k56 gold badges287 silver badges370 bronze badges asked Feb 7, 2013 at 4:53 jeffchong07jeffchong07 5526 silver badges13 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Make sure to add the semicolons at the end of each file or concatenate and then minify and minifier should take care of that.

Try this code: https://gist.github./elclanrs/4728677

Reason

In my case it was because I specified dependencies in requirejs's shim, for a library (perfect-scrollbar) that already supports AMD loading. The result is that the whole of its code in define is bypassed after grunt-contrib-requirejs did its job.

Basically, to concat requirejs files together, grunt-contrib-requirejs would

// change this:
define(['d3'],function(d3){...});

// into this:
define('d3', ['d3'],function(d3){...});

Whereas inside perfect-scrollbar, there's already

(function (factory) {
  'use strict';

  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.

    define(['jquery'], factory); // <-------------------------- this part
    // after minification would bee:
    // define('PerfectScrollbar', ['jquery'], factory);

  } else if (typeof exports === 'object') {
    factory(require('jquery')); // Node/CommonJS
  } else {
    factory(jQuery); // Browser globals
  }
}(function ($) {
  // perfect-scrollbar code here...
});

Which conflicted with what I specified in shim:

shim: {
    "PerfectScrollbar": ['jquery', 'jquery.mousewheel']
}

Therefore when requirejs got to the part where PerfectScrollbar was really defined, it hopped over it, assuming that it has already done the job.

Solution

Don't specify dependencies in shim for libraries that already have AMD support.

Question

But what if I need to specify dependencies? I need jquery.mousewheel on top of the jquery it already specified in its code.

Answer

Require that file with proper require, and get its own dependencies right:

define(['perfect-scrollbar', 'jquery.mousewheel'], function(){...});

Either when the library you need has AMD support,

// inside jquery.mousewheel:
require(['jquery'], factory);

Or it doesn't

shim: {
    "IDontSupportAMD": ['jquery']
}
发布评论

评论列表(0)

  1. 暂无评论