### AMD Module Loading and Configuration Source: https://github.com/dojo/loader/blob/master/tests/functional/basicAmdLoading.html Configures the AMD loader with a package location and then asynchronously loads a module ('amdApp/app'). The loaded module's message is captured for testing purposes. ```javascript require.config({ packages: [ { name: 'amdApp', location: './amdApp' } ] }); require([ 'amdApp/app' ], function (app) { window.loaderTestResults = { message: app.getMessage() }; }); ``` -------------------------------- ### Dojo Loader: Module Loading and Undefining Example (JavaScript) Source: https://github.com/dojo/loader/blob/master/tests/functional/require/undef.html This example demonstrates configuring the Dojo loader, defining a module, loading multiple modules including a custom one and an application module, and then undefining them to verify the loader's state management. It uses assertions to check module loading and undefining success. ```javascript require.undef('Test'); require.config({ baseUrl: '../..', packages: [ { name: 'chai', location: '../../node_modules/chai', main: 'chai' } ] }); define('some/module', [], function () { return 'bananas'; }); require([ 'chai/chai', 'some/module', 'common/app' ], function (chai, someModule, app) { var assert = chai.assert; try { assert.strictEqual(someModule, 'bananas', '"some/module" should load'); assert.strictEqual(app, 'app', '"common/app" should load'); require.undef('some/module'); someModule = require('some/module'); assert.isUndefined(someModule, '"some/module" should be undefined'); require.undef('common/app'); app = require('common/app'); assert.isUndefined(someModule, '"common/app" should be undefined'); } catch (error) { window.loaderTestResults = error.message; } finally { if (window.loaderTestResults === undefined) { window.loaderTestResults = 'success'; } } }); ``` -------------------------------- ### Configure and Load AMD Modules with Shim Source: https://github.com/dojo/loader/blob/master/tests/functional/shimAmdLoading.html Demonstrates configuring an AMD loader using `require.config` with `shim` properties to manage dependencies and initialization for modules. It then loads these modules using `require` and asserts their values. ```javascript var globalValues = { string: 'string' }; define('first-number', [], function() { return 3; }); define('second-number', [], function() { return 5; }); define('plugin-dep', [], function() { window.pluginDep = 'plugin-dep'; }); require.config({ shim: { 'global/string': { exports: 'globalValues.string' }, 'global/number': { init: function() { return 5; } }, 'init-once': (function() { var initCount = 0; return { init: function() { return ++initCount; } }; })(), 'init-no-return': { init: function() { } }, 'adder': { deps: ['first-number', 'second-number'], init: function(firstNumber, secondNumber) { return firstNumber + secondNumber; } }, 'plugin': ['plugin-dep'] } }); require([ 'global/string', 'global/number', 'adder', 'init-no-return', 'init-once', 'init-once', 'plugin' ], function(stringValue, numberValue, addedValues, initNoReturn, initOnce, initTwice) { window.loaderTestResults = { stringValue: stringValue, numberValue: numberValue, addedValues: addedValues, initNoReturn: initNoReturn, initOnce: initOnce, initTwice: initTwice, pluginDep: window.pluginDep, requireError: window.requireError }; }); ``` -------------------------------- ### Configure and Load Dojo Module Source: https://github.com/dojo/loader/blob/master/tests/functional/require/config/baseUrl.html Configures the Dojo AMD loader by setting the base URL and then asynchronously loads the 'pkg/main' module. The loaded module is made available globally via `window.loaderTestResults` for testing. ```javascript require.config({ baseUrl: '.' }); require([ 'pkg/main' ], function (app) { window.loaderTestResults = app; }); ``` -------------------------------- ### Define and Load AMD Module with Dojo Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleWithId1.html Shows how to define an AMD module with its dependencies and then asynchronously load it. The example defines 'test/module1' and uses `require` to load it, passing the loaded module to a callback function. ```javascript define('test/module1', [], function () { return 'testModule1'; }); require(['test/module1'], function (testModule1) { window.loaderTestResults = { testModule1Value: testModule1 }; }); ``` -------------------------------- ### AMD Module Loading Example Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleWithId4a.html Configures AMD package locations and loads modules sequentially using `require.config` and `require` calls. It demonstrates how to load 'amdApp/module1' and then 'amdApp/app1', retrieving a message from the latter. ```javascript require.config({ packages: [ { name: 'amdApp', location: './amdApp' } ] }); require(['amdApp/module1'], function () { require(['amdApp/app1'], function (app1) { window.loaderTestResults = app1.getMessage(); }); }); ``` -------------------------------- ### Install @dojo/loader Source: https://github.com/dojo/loader/blob/master/README.md Installs the `@dojo/loader` package using npm. This command is essential for adding the loader to your project's dependencies. ```bash npm install @dojo/loader ``` -------------------------------- ### JavaScript Web Worker Initialization Source: https://github.com/dojo/loader/blob/master/tests/functional/webworkerBasic.html This snippet demonstrates how to create a Web Worker in JavaScript. It checks for browser support, initializes a worker, sets up a message handler to receive data, and sends a 'start' message. If Web Workers are not supported, it logs an error. ```javascript if (window.Worker) { const worker = new Worker('worker.js'); worker.onmessage = function (e) { window.loaderTestResults = e.data; } worker.postMessage('start'); } else { console.error('does not support web workers'); } ``` -------------------------------- ### AMD Module Loading with Dojo Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleDeepDeps.html Configures the AMD loader to recognize a package and then loads a module from that package. It demonstrates asynchronous module loading and accessing module exports. ```javascript require.config({ packages: [ { name: 'amdApp', location: './amdApp' } ] }); require(['amdApp/deep1'], function (deep1) { var obj = deep1.default(); window.loaderTestResults = obj.deep3(); }); ``` -------------------------------- ### Configure Dojo Paths and Load Module Source: https://github.com/dojo/loader/blob/master/tests/functional/require/config/paths1.html Configures the AMD loader to map module names to file paths and then asynchronously loads a module. It sets up a path alias for 'common' and executes a callback function with the loaded module. ```javascript require.config({ paths: { common: '../../../common' } }); require(['common/app'], function (app) { window.loaderTestResults = app; }); ``` -------------------------------- ### AMD Module Definition and Loading Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleWithId6.html This snippet shows how to define an AMD module with a specific name and properties. It then demonstrates how to asynchronously load this module using the require function, making its exports available in a callback function. ```javascript define('test/module1', { testModuleProperty: 'property value' }); require(['test/module1'], function (testModule1) { window.loaderTestResults = testModule1; }); ``` -------------------------------- ### Dojo Loader Error Handling with require.js Source: https://github.com/dojo/loader/blob/master/tests/functional/require/on/error.html This snippet demonstrates setting up an error handler for the Dojo loader. It configures the loader, defines a module that will fail to load, and attaches an error listener to verify the error details provided by the loader. The success or failure of the test is reported via `window.loaderTestResults`. ```javascript require.undef("Test"); require.config({ baseUrl: "../..", packages: [ { name: "chai", location: "../../../node_modules/chai", main: "chai" } ] }); require(['chai/chai'], function (chai) { var assert = chai.assert; var badMid = 'bad/module'; require.on('error', function (loaderError) { try { assert.strictEqual(loaderError.src, 'dojo/loader', 'Error should be marked as from the loader'); assert.isObject(loaderError.info, 'Error should be supplemented with info'); assert.strictEqual(loaderError.info.module.mid, badMid, 'Error should be related to the bad module'); assert.include(loaderError.info.url, badMid + '.js', 'Error should contain the URL of the bad module'); } catch (error) { window.loaderTestResults = error.message; } finally { if (window.loaderTestResults === undefined) { window.loaderTestResults = 'success'; } } }); require([badMid]); }); ``` -------------------------------- ### Dojo Loader Configuration and Test Source: https://github.com/dojo/loader/blob/master/tests/functional/require/plugin-config.html Demonstrates setting up Dojo's AMD loader configuration, including package definitions, and then executing a test to verify the loaded configuration against expected values using Chai assertions. It checks the baseUrl and packages properties of the loaded configuration. ```javascript var requireConfig = { baseUrl: '../..', packages: [ { name: 'chai', location: '../../node_modules/chai', main: 'chai' } ] }; require.config(requireConfig); require(['common/pluginConfig!one', 'chai'], function (pluginConfig, chai) { var assert = chai.assert; try { assert.equal(pluginConfig.baseUrl, requireConfig.baseUrl, 'Plugin config should contain the BaseUrl'); assert.deepEqual(pluginConfig.packages, requireConfig.packages, 'Plugin should have received cpackages equal to require config packages'); } catch (error) { window.loaderTestResults = error.message; } finally { if (window.loaderTestResults === undefined) { window.loaderTestResults = 'success'; } } }); ``` -------------------------------- ### Run Prettier for Code Styling Source: https://github.com/dojo/loader/blob/master/README.md Executes the Prettier tool to format and style all source and test files within the project. This ensures consistent code style across the repository. ```bash npm run prettier ``` -------------------------------- ### Build Project with Grunt Source: https://github.com/dojo/loader/blob/master/README.md Builds the project using Grunt. `grunt dev` is typically for development builds, while `grunt dist` is for creating production-ready distribution files. ```bash grunt dev ``` ```bash grunt dist ``` -------------------------------- ### Dojo Loader Error Handling with require.on('error') and window.onerror Source: https://github.com/dojo/loader/blob/master/tests/functional/require/on/remove.html This snippet demonstrates how to configure the Dojo loader to handle module loading errors. It sets up a global window.onerror handler to catch loading failures and uses require.on('error') to listen for loader-specific error events. The code asserts that the error messages contain expected details about the failed module and checks for loader-specific error object properties. ```javascript require.undef('Test'); require.config({ baseUrl: '../..', packages: [ { name: 'chai', location: '../../../node_modules/chai', main: 'chai' } ] }); require(['chai/chai'], function (chai) { var assert = chai.assert; var badMid = 'bad/module'; // Register a listener for the loader's 'error' event var onErrorHandler = require.on('error', function (error) { try { assert.fail(null, null, 'on-error callback should not have fired'); } catch (error) { window.loaderTestResults = error.message; } finally { if (window.loaderTestResults === undefined) { window.loaderTestResults = 'success'; } } }); // Set a global error handler to catch loading failures window.onerror = function (errorMsg, url, lineNumber, column, errorObj) { try { assert.include(errorMsg, 'Failed to load module', 'Message should indicate module failed to load'); assert.include(errorMsg, badMid, 'Message should contain bad mid'); // errorObj not present for IE9/10 if (errorObj) { assert.strictEqual(errorObj.src, 'dojo/loader', 'Error should be marked as from the loader'); assert.isObject(errorObj.info, 'Error should be supplemented with info'); assert.strictEqual(errorObj.info.module.mid, badMid, 'Error should be related to the bad module'); assert.include(errorObj.info.url, badMid + '.js', 'Error should contain the URL of the bad module'); } } catch (assertionError) { window.loaderTestResults = assertionError.message; } finally { if (window.loaderTestResults === undefined) { window.loaderTestResults = 'success'; } } return true; // Prevent default browser error handling }; // Remove the loader's error handler to rely solely on window.onerror onErrorHandler.remove(); // Attempt to load a non-existent module to trigger errors require([badMid]); }); ``` -------------------------------- ### AMD Module Loading Configuration and Execution Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleWithId3a.html Configures AMD packages and loads a specific module, demonstrating asynchronous module definition. It sets up the 'amdApp' package and then requires 'amdApp/module3', assigning the loaded module to a global variable for testing. ```javascript require.config({ packages: [ { name: 'amdApp', location: './amdApp' } ] }); require(['amdApp/module3'], function() { var testModule3 = require('test/module3'); window.loaderTestResults = testModule3; }); ``` -------------------------------- ### Configure and Load Dojo Plugin Source: https://github.com/dojo/loader/blob/master/tests/functional/require/plugin-load.html Demonstrates how to configure the Dojo AMD loader by setting the base URL and then asynchronously loading a plugin named 'one' from the 'common/plugin' module. The loaded plugin is made available globally via `window.loaderTestResults`. ```javascript require.config({ baseUrl: '../..' }); require(['common/plugin!one'], function (plugin1) { window.loaderTestResults = plugin1; }); ``` -------------------------------- ### AMD Loading with require() and Callback Source: https://github.com/dojo/loader/blob/master/tests/functional/amdModuleWithId2a.html Shows how to load multiple AMD modules asynchronously using `require` with a callback function. It loads 'amdApp/module1' and 'amdApp/module2', then within the callback, it loads 'test/module2' and assigns it to a global variable. ```javascript require([ 'amdApp/module1', 'amdApp/module2' ], function () { var testModule2 = require('test/module2'); window.loaderTestResults = testModule2; }); ``` -------------------------------- ### Dojo AMD Loading Configuration and Execution Source: https://github.com/dojo/loader/blob/master/tests/functional/crossOriginCreds.html Configures the AMD loader with package locations and cross-origin settings. It then asynchronously loads an AMD module ('amdApp/crossOrigin') and stores a result in `window.loaderTestResults`. ```javascript require.config({ crossOrigin: 'use-credentials', packages: [ { name: 'amdApp', location: './amdApp' } ] }); require(['amdApp/crossOrigin'], function (app) { window.loaderTestResults = { message: 'The cross origin value is ' + window.crossOriginResult.value }; }); ``` -------------------------------- ### Configure and Load Dojo Modules Source: https://github.com/dojo/loader/blob/master/tests/functional/map.html This snippet illustrates setting up module mappings and package locations using `require.config` in Dojo. It then demonstrates loading a specific module ('common/map1') and assigning its exports to a global variable. ```javascript require.config({ map: { common: { mapped: '../common' } }, packages: [ { name: 'common', location: '../common' } ] }); require(['common/map1'], function (map1) { window.loaderTestResults = map1; }); ``` -------------------------------- ### Configure and Load Dojo Package for Testing Source: https://github.com/dojo/loader/blob/master/tests/functional/require/config/defaultConfig.html This snippet configures the Dojo loader and then requires a specific package ('pkg/main') for testing. It assigns the loaded application object to `window.loaderTestResults` for global access during testing. ```javascript require.config Test require([ 'pkg/main' ], function (app) { window.loaderTestResults = app; }); ``` -------------------------------- ### Dojo Loader Configuration and Module Loading Source: https://github.com/dojo/loader/blob/master/tests/functional/commonJsModuleWithId3.html This snippet shows how to configure the Dojo loader with package locations and then load a CommonJS module using `require`. It demonstrates setting up a package mapping for 'commonJs' and initiating module loading with a callback function that accesses the loaded module. ```javascript require.config({ packages: [ { name: 'commonJs', location: '../common/commonJs' } ] }); require([ 'commonJs/testModule3' ], function() { var testModule3 = require('test/module3'); window.loaderTestResults = testModule3; }); ``` -------------------------------- ### Basic AMD Loading Test Source: https://github.com/dojo/loader/blob/master/tests/functional/amdFactoryOnly.html This snippet demonstrates how to configure AMD packages and load a module using require.js. It sets up a package named 'amd' located at '../common/amd' and then requires the 'amd/onlyFactory' module, exposing its 'property' to a global test result object. ```javascript require.config({ packages: [ { name: 'amd', location: '../common/amd' } ] }); require([ 'amd/onlyFactory' ], function (app) { window.loaderTestResults = { property: app.property }; }); ``` -------------------------------- ### AMD Loading with RequireJS Configuration Source: https://github.com/dojo/loader/blob/master/tests/functional/crossOriginFalse.html Configures the AMD loader with package locations and then loads a specific module. It demonstrates setting a global variable based on the loaded module's functionality, useful for testing module dependencies and execution flow. ```javascript require.config({ crossOrigin: false, packages: [ { name: 'amdApp', location: './amdApp' } ] }); require([ 'amdApp/crossOrigin' ], function (app) { window.loaderTestResults = { message: 'The cross origin value is ' + window.crossOriginResult.value }; }); ``` -------------------------------- ### Configure and Load Dojo Module Source: https://github.com/dojo/loader/blob/master/tests/functional/require/config/map-simple.html Configures the module loader with package mappings and then asynchronously loads the 'common/map1' module. The loaded module's results are assigned to a global variable for testing. This snippet assumes a RequireJS-like environment. ```javascript require.config({ map: { common: { mapped: '../../../common' } }, packages: [ { name: 'common', location: '../../../common' } ] }); require(['common/map1'], function (map1) { window.loaderTestResults = map1; }); ``` -------------------------------- ### AMD Module Loading and Execution Source: https://github.com/dojo/loader/blob/master/tests/functional/scriptConfigReading.html This snippet demonstrates loading an AMD module named 'amdApp/app' and executing a function upon successful loading. It retrieves a message from the loaded module and stores it in `window.loaderTestResults`. ```javascript require([ "amdApp/app" ], function (app) { window.loaderTestResults = { message: app.getMessage() }; }); ```