Time for action – optimizing JavaScript with RequireJS
Follow these steps to optimize the source code of your app using Node.js and RequireJS:
Install the
requirejs
module using npm from the command-line tool:$ sudo npm install requirejs -g
Go to the root folder of the app you worked on in the previous chapters, create a file named
build.js
, and add to it the build process configuration info (that is, the JavaScript folder, the paths to the library used in the project, the name of the main file of the app, and the output folder and filename):({ baseUrl: ‘js/', paths: { mustache: ‘libs/mustache', alice: ‘libs/alice.min', text: ‘libs/require/plugins/text' }, name: ‘main', out: ‘js/main-built.js' })
Open the command-line tool again and execute the following command in order to build the app:
$ r.js -o build.js
Open the
index.html
file and change the entry point of your app in thescript
tag in the header:<script data-main=”js/main-built” src=”js...