Using grunt requirejs with almond results in "define not defined"

In my grunt.js file I have

 requirejs: { dist: { options: { almond: true, wrap: true, modules: [{name: 'main'}], mainConfigFile: "src/js/main.js", baseUrl: "src/js", dir: "tmp/js", inlineText: true, preserveLicenseComments: false } } } 

Running grunt requirejs:dist populates the tmp/js directory with some minified files, including the large main.js file (everything seems to be bundled with this file, as expected) - however, when I want to include this file like this, / p >

 <script type="text/javascript" src="tmp/main.js"></script> 

The result is "Uncaught ReferenceError: define is not defined"

The purpose of using almonds was that I did not need to download the require.js file to load my optimized file - any idea how to make this work?

footnote: I already managed to do this, except that the main-built.js file was previously compiled, however, it seems this is not the case (updates ... -.-)

+8
requirejs amd js-amd almond
source share
1 answer

So, I tried to set an empty directory with the following files and folders

 /grunt.js /src/js/main.js /tmp/js 

Here is my grunt.js:

 module.exports = function(grunt) { grunt.loadNpmTasks("grunt-requirejs"); grunt.initConfig({ requirejs: { dist: { options: { almond: true, wrap: true, modules: [{name: 'main'}], mainConfigFile: "src/js/main.js", baseUrl: "src/js", dir: "tmp/js", inlineText: true, preserveLicenseComments: false } } } }); grunt.registerTask("default", "requirejs"); }; 

Here is my main.js:

 define(function() { console.log('hi'); }); 

I ran these commands in the console:

 npm install grunt npm install requirejs npm install grunt-requirejs grunt --gruntfile grunt.js 

Make a conclusion:

 Running "requirejs:dist" (requirejs) task >> RequireJS optimizer finished Uncompressed size: 14234 bytes. Compressed size: 1265 bytes gzipped. (2633 bytes minified) 

The resulting file in /tmp/js/main.js had links to almonds. He did not mention the definition function, although due to the minimization of js. My call to define( been rewritten to n( .

I suspect that you might have code outside the collapsed file that calls define.

Also, in my other project, mainConfigFile is referenced only as a configuration file and not loaded as a module.

+2
source share

All Articles