{"id":2096,"date":"2022-08-30T15:22:01","date_gmt":"2022-08-30T15:22:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/26\/r-js-not-compiling-shimmed-scripts-properly-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:01","modified_gmt":"2022-08-30T15:22:01","slug":"r-js-not-compiling-shimmed-scripts-properly-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/r-js-not-compiling-shimmed-scripts-properly-collection-of-common-programming-errors\/","title":{"rendered":"r.js not compiling shimmed scripts properly-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m able to successfully compile my JS modules via Grunt&#8217;s requirejs task, but I&#8217;m getting undefined with any shim scripts. Here is my config<\/p>\n<pre><code>requirejs.config({\n    \"baseUrl\": \"..\/..\/..\/components\/\",\n    \"paths\": {\n        \"less\": \"less.js\/dist\/less-1.3.3\",\n        \"datepicker\": \"jquery-ui\/ui\/jquery.ui.datepicker\",\n        \"jquery\": \"jquery\/jquery\",\n        \"jqueryui\": \"jquery-ui\/ui\/jquery-ui\",\n        \"spectrum\": \"spectrum\/spectrum\",\n        \"class\": \"class\/class\",\n        \"underscore\": \"underscore-amd\/underscore\",\n        \"d3\": \"d3\/d3\",\n        \"nv\": \"nvd3\/nv.d3\",\n        \"dataTables\": \"datatables\/dataTables\"\n\n    },\n    shim: {\n        less: {\n            exports: \"less\"\n        },\n        jqueryui: {\n            exports: \"jqueryui\"\n        },\n        spectrum: {\n            exports: \"spectrum\"\n        },\n        class:{\n            deps: [ 'jquery'],\n            exports: \"class\"\n        }\n    }   \n})\n<\/code><\/pre>\n<p>Here is my requirejs config from my Gruntfile:<\/p>\n<pre><code>requirejs: {\n    dist: {\n        options: {\n            mainConfigFile: 'src\/scripts\/main.js',\n            out: '\/scripts\/main.js',\n            paths:{\n                \"main\" : \"..\/final\/src\/scripts\/main\"\n            },\n            include:['main'],\n            preserveLicenseComments: false,\n            useStrict: true,\n            wrap: true\n        }\n    }\n}\n<\/code><\/pre>\n<p>Now when I run <code>grunt requirejs:dist<\/code> It compiles just fine, and does include my shimmed files. But whenever I try to run the compiled JS, access to any of my shimmed scripts are undefined.<\/p>\n<ol>\n<li>\n<p>Not sure what you mean by &#8220;access to any of my shimmed scripts are undefined&#8221;. You need to ensure that shimmed scripts are included in correct order. Also you may try defining fake module for that script. In you build JS configuration add:<\/p>\n<pre><code>onBuildRead: function (moduleName, path, contents) {\n    if (moduleNam === 'spectrum'){\n        contents += '; define(' + moduleName + ', function(){ return window.spectrum; });';\n    }\n    return contents;\n}\n<\/code><\/pre>\n<p>You need to understand, what exports does. After script is loaded it will look for that variable in a global namespace. I don&#8217;t think there is &#8216;jqueryui&#8217; namespace. So it will always be undefined, because it has to load after jQuery and it just extends jQuery, so module can not return anything useful.<\/p>\n<p>I hope that helps.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-26 03:13:50. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m able to successfully compile my JS modules via Grunt&#8217;s requirejs task, but I&#8217;m getting undefined with any shim scripts. Here is my config requirejs.config({ &#8220;baseUrl&#8221;: &#8220;..\/..\/..\/components\/&#8221;, &#8220;paths&#8221;: { &#8220;less&#8221;: &#8220;less.js\/dist\/less-1.3.3&#8221;, &#8220;datepicker&#8221;: &#8220;jquery-ui\/ui\/jquery.ui.datepicker&#8221;, &#8220;jquery&#8221;: &#8220;jquery\/jquery&#8221;, &#8220;jqueryui&#8221;: &#8220;jquery-ui\/ui\/jquery-ui&#8221;, &#8220;spectrum&#8221;: &#8220;spectrum\/spectrum&#8221;, &#8220;class&#8221;: &#8220;class\/class&#8221;, &#8220;underscore&#8221;: &#8220;underscore-amd\/underscore&#8221;, &#8220;d3&#8221;: &#8220;d3\/d3&#8221;, &#8220;nv&#8221;: &#8220;nvd3\/nv.d3&#8221;, &#8220;dataTables&#8221;: &#8220;datatables\/dataTables&#8221; }, shim: { less: { exports: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2096","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2096"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2096\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}