Skip to content

Commit 009efd6

Browse files
authoredJan 31, 2017
Stdavis recursive undef (#107)
* Add an optional `recursive` parameter to `require.undef` Related to dojo/dojo2/issues/16 * Fixing up typing issues, issue-16 * Adding test for undefined modules, issue-16 * ci build * Updating grunt ts * More gurnt file changes
1 parent bc5dd1c commit 009efd6

File tree

9 files changed

+80
-5
lines changed

9 files changed

+80
-5
lines changed
 

‎Gruntfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ module.exports = function (grunt) {
6161
'typings:dev',
6262
'tslint',
6363
'clean:dev',
64-
'ts:dev',
65-
'ts:tests',
64+
'dojo-ts:dev',
65+
'dojo-ts:tests',
6666
'copy:staticTestFiles'
6767
]);
6868

‎src/interfaces.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ declare namespace DojoLoader {
214214
config(config: Config): void;
215215
inspect?(name: string): any;
216216
nodeRequire?(id: string): any;
217-
undef(moduleId: string): void;
217+
undef(moduleId: string, recursive?: boolean): void;
218218
}
219219

220220
type SignalType = 'error';

‎src/loader.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
import ModuleShim = DojoLoader.ModuleShim;
3+
import Module = DojoLoader.Module;
34

45
declare const load: (module: string) => any;
56
declare const Packages: {} | undefined;
@@ -1076,8 +1077,18 @@ declare const Packages: {} | undefined;
10761077

10771078
has.add('loader-undef', true);
10781079
if (has('loader-undef')) {
1079-
requireModule.undef = function (id: string): void {
1080-
if (modules[id]) {
1080+
requireModule.undef = function (id: string, recursive?: boolean): void {
1081+
const module: Module | undefined = modules[id];
1082+
const undefDeps = function (mod: Module): void {
1083+
if (mod.deps) {
1084+
forEach(mod.deps, undefDeps);
1085+
}
1086+
modules[mod.mid] = undefined;
1087+
};
1088+
if (module) {
1089+
if (recursive && module.deps) {
1090+
forEach(module.deps, undefDeps);
1091+
}
10811092
modules[id] = undefined;
10821093
}
10831094
};

‎tests/common/recursive/a.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define([
2+
'./b'
3+
], function (b) {
4+
return 'a';
5+
});

‎tests/common/recursive/b.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define([
2+
'./c',
3+
'./d'
4+
], function (c) {
5+
return 'b';
6+
});

‎tests/common/recursive/c.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], 'c');

‎tests/common/recursive/d.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define([
2+
'./e'
3+
], function (e) {
4+
return 'd';
5+
});

‎tests/common/recursive/e.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], 'e');

‎tests/unit/require.ts

+46
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,52 @@ registerSuite({
631631
dfd.reject('Loading undefined module should throw an error');
632632
});
633633
},
634+
'recurisve undef': {
635+
'dependencies are unloaded'(this: any) {
636+
let dfd = this.async(DEFAULT_TIMEOUT);
637+
638+
global.require.config({
639+
packages: [
640+
{
641+
name: 'recursive',
642+
location: './_build/tests/common/recursive'
643+
}
644+
]
645+
});
646+
647+
function checkForUndef(mod: string): boolean {
648+
try {
649+
global.require(mod);
650+
}
651+
catch (error) {
652+
if (error.message.indexOf(mod) !== -1) {
653+
return true;
654+
}
655+
}
656+
return false;
657+
}
658+
659+
global.require([
660+
'recursive/a'
661+
], function () {
662+
global.require.undef('recursive/a', true);
663+
const deps: string[] = [ 'recursive/a', 'recursive/b', 'recursive/c', 'recursive/d', 'recursive/e' ];
664+
const passed: boolean = deps.every(function (mod: string) {
665+
return checkForUndef(mod);
666+
});
667+
if (passed) {
668+
dfd.resolve();
669+
}
670+
else {
671+
dfd.reject('not all dependencies were undefined');
672+
}
673+
});
674+
},
675+
676+
'modules without dependencies work as expected'() {
677+
global.require.undef('invalid-module', true);
678+
}
679+
},
634680

635681
plugin: {
636682
load(this: any) {

0 commit comments

Comments
 (0)