Ajaxmin MinifyJavascript adding {} before object

169 Views Asked by At

I am facing an issue while minifying a javascript file using AjaxMin's MinifyJavaScript method.

Unminified Code

if (typeof define === 'function' && define.amd) {
        define(['moment'], function (moment) {
            root.moment = factory(moment)
            return root.moment
        })
    } else if (typeof exports === 'object') {
        module.exports = factory(require('moment'))
    } else {
        root.moment = factory(root.moment)
    }

Minified Code

if(typeof define=="function"&&define.amd)define(["moment"],function(i){return n.moment=t(i),n.moment});else if(typeof exports=="object"){module{}.exports=t(require("moment"))}else n.moment=t(n.moment)}

Here in minified code "{}" is added after module object e.g.: module{}.exports but it should be module.exports

There are few more files before minifying the file: 1. jquery-3.3.1.min.js 2. moment.min.js

All the files are bundled in single file and after that minification is done.

1

There are 1 best solutions below

0
On BEST ANSWER

I think this are the causes to the problem

  1. ajaxmin source code module seems to be a reserved word
  2. If/else scopes doesn't compiles properly to the output.

This seems to causing the issue

var blockType = AjaxMin.BlockTypeModule.FormatInvariant
(moduleScope.ScopeName.IfNullOrWhiteSpace(AjaxMin.ModuleNameImplicit));

Workaround (this is what I did in my project):

  • You can replace that string module{}. with module.
  • use ternary operator instead of if/else (most probably this will skip the replacement of that whole chunk)
  • use minify version of that file (this will skip the minify)