Skip to content

Commit f802000

Browse files
committed
Merge remote-tracking branch 'origin/master' into ggilmore-master
2 parents 8a7f325 + 6a33f63 commit f802000

19 files changed

+503
-451
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ To run the tests:
7878
* code loading in node: `npm run test`
7979
* amd spec tests, unit tests & code loading in browser:
8080
* `npm run simpleserver`
81-
* open `http://localhost:8888/tests/run-tests.htm`
81+
* open `http://localhost:9999/tests/run-tests.htm`
8282

8383
The project uses as a submodule the [AMD compliance tests](https://github.com/amdjs/amdjs-tests). The goal is to support as many tests without adding `eval()` or an equivalent. It is also not a goal to support loading CommonJS code:
8484

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"homepage": "https://github.com/Microsoft/vscode-loader#readme",
2828
"devDependencies": {
2929
"http-server": "^0.9.0",
30-
"typescript": "^2.0.3"
30+
"typescript": "^2.7.1"
3131
}
3232
}

src/core/configuration.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace AMDLoader {
126126
/**
127127
* Ensure configuration options make sense
128128
*/
129-
private static validateConfigurationOptions(isWebWorker: boolean, options: IConfigurationOptions): IConfigurationOptions {
129+
private static validateConfigurationOptions(options: IConfigurationOptions): IConfigurationOptions {
130130

131131
function defaultOnError(err): void {
132132
if (err.errorCode === 'load') {
@@ -164,8 +164,7 @@ namespace AMDLoader {
164164
options.config = {};
165165
}
166166
if (typeof options.catchError === 'undefined') {
167-
// Catch errors by default in web workers, do not catch errors by default in other contexts
168-
options.catchError = isWebWorker;
167+
options.catchError = false;
169168
}
170169
if (typeof options.urlArgs !== 'string') {
171170
options.urlArgs = '';
@@ -208,7 +207,7 @@ namespace AMDLoader {
208207
return options;
209208
}
210209

211-
public static mergeConfigurationOptions(isWebWorker: boolean, overwrite: IConfigurationOptions = null, base: IConfigurationOptions = null): IConfigurationOptions {
210+
public static mergeConfigurationOptions(overwrite: IConfigurationOptions = null, base: IConfigurationOptions = null): IConfigurationOptions {
212211
let result: IConfigurationOptions = Utilities.recursiveClone(base || {});
213212

214213
// Merge known properties and overwrite the unknown ones
@@ -224,7 +223,7 @@ namespace AMDLoader {
224223
}
225224
});
226225

227-
return ConfigurationOptionsUtil.validateConfigurationOptions(isWebWorker, result);
226+
return ConfigurationOptionsUtil.validateConfigurationOptions(result);
228227
}
229228
}
230229

@@ -251,19 +250,19 @@ namespace AMDLoader {
251250

252251
constructor(env: Environment, options?: IConfigurationOptions) {
253252
this._env = env;
254-
this.options = ConfigurationOptionsUtil.mergeConfigurationOptions(this._env.isWebWorker, options);
253+
this.options = ConfigurationOptionsUtil.mergeConfigurationOptions(options);
255254

256255
this._createIgnoreDuplicateModulesMap();
257256
this._createNodeModulesMap();
258257
this._createSortedPathsRules();
259258

260259
if (this.options.baseUrl === '') {
261-
if (this._env.isNode && this.options.nodeRequire && this.options.nodeRequire.main && this.options.nodeRequire.main.filename) {
260+
if (this.options.nodeRequire && this.options.nodeRequire.main && this.options.nodeRequire.main.filename && this._env.isNode) {
262261
let nodeMain = this.options.nodeRequire.main.filename;
263262
let dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\'));
264263
this.options.baseUrl = nodeMain.substring(0, dirnameIndex + 1);
265264
}
266-
if (this._env.isNode && this.options.nodeMain) {
265+
if (this.options.nodeMain && this._env.isNode) {
267266
let nodeMain = this.options.nodeMain;
268267
let dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\'));
269268
this.options.baseUrl = nodeMain.substring(0, dirnameIndex + 1);
@@ -315,7 +314,7 @@ namespace AMDLoader {
315314
* @result A new configuration
316315
*/
317316
public cloneAndMerge(options?: IConfigurationOptions): Configuration {
318-
return new Configuration(this._env, ConfigurationOptionsUtil.mergeConfigurationOptions(this._env.isWebWorker, options, this.options));
317+
return new Configuration(this._env, ConfigurationOptionsUtil.mergeConfigurationOptions(options, this.options));
319318
}
320319

321320
/**

src/core/env.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,46 @@ namespace AMDLoader {
6262

6363
export class Environment {
6464

65-
public static detect(): Environment {
66-
return new Environment({
67-
isWindows: this._isWindows(),
68-
isNode: (typeof module !== 'undefined' && !!module.exports),
69-
isElectronRenderer: (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer'),
70-
isWebWorker: (typeof global.importScripts === 'function')
71-
});
65+
private _detected: boolean;
66+
private _isWindows: boolean;
67+
private _isNode: boolean;
68+
private _isElectronRenderer: boolean;
69+
private _isWebWorker: boolean;
70+
71+
public get isWindows(): boolean {
72+
this._detect();
73+
return this._isWindows;
74+
}
75+
public get isNode(): boolean {
76+
this._detect();
77+
return this._isNode;
78+
}
79+
public get isElectronRenderer(): boolean {
80+
this._detect();
81+
return this._isElectronRenderer;
82+
}
83+
public get isWebWorker(): boolean {
84+
this._detect();
85+
return this._isWebWorker;
7286
}
7387

74-
public readonly isWindows: boolean;
75-
public readonly isNode: boolean;
76-
public readonly isElectronRenderer: boolean;
77-
public readonly isWebWorker: boolean;
88+
constructor() {
89+
this._detected = false;
90+
this._isWindows = false;
91+
this._isNode = false;
92+
this._isElectronRenderer = false;
93+
this._isWebWorker = false;
94+
}
7895

79-
constructor(opts: {
80-
isWindows: boolean;
81-
isNode: boolean;
82-
isElectronRenderer: boolean;
83-
isWebWorker: boolean;
84-
}) {
85-
this.isWindows = opts.isWindows;
86-
this.isNode = opts.isNode;
87-
this.isElectronRenderer = opts.isElectronRenderer;
88-
this.isWebWorker = opts.isWebWorker;
96+
private _detect(): void {
97+
if (this._detected) {
98+
return;
99+
}
100+
this._detected = true;
101+
this._isWindows = Environment._isWindows();
102+
this._isNode = (typeof module !== 'undefined' && !!module.exports);
103+
this._isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer');
104+
this._isWebWorker = (typeof global.importScripts === 'function');
89105
}
90106

91107
private static _isWindows(): boolean {

src/core/loaderEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace AMDLoader {
77

8-
export enum LoaderEventType {
8+
export const enum LoaderEventType {
99
LoaderAvailable = 1,
1010

1111
BeginLoadingScript = 10,

src/core/main.ts

Lines changed: 91 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -12,130 +12,117 @@ var define;
1212

1313
namespace AMDLoader {
1414

15+
const env = new Environment();
16+
1517
let moduleManager: ModuleManager = null;
16-
let DefineFunc: IDefineFunc = null;
17-
let RequireFunc: IRequireFunc = null;
1818

19-
function createGlobalAMDFuncs(): void {
19+
const DefineFunc: IDefineFunc = <any>function (id: any, dependencies: any, callback: any): void {
20+
if (typeof id !== 'string') {
21+
callback = dependencies;
22+
dependencies = id;
23+
id = null;
24+
}
25+
if (typeof dependencies !== 'object' || !Array.isArray(dependencies)) {
26+
callback = dependencies;
27+
dependencies = null;
28+
}
29+
if (!dependencies) {
30+
dependencies = ['require', 'exports', 'module'];
31+
}
2032

21-
const _defineFunc = function (id: any, dependencies: any, callback: any): void {
22-
if (typeof id !== 'string') {
23-
callback = dependencies;
24-
dependencies = id;
25-
id = null;
26-
}
27-
if (typeof dependencies !== 'object' || !Array.isArray(dependencies)) {
28-
callback = dependencies;
29-
dependencies = null;
33+
if (id) {
34+
moduleManager.defineModule(id, dependencies, callback, null, null);
35+
} else {
36+
moduleManager.enqueueDefineAnonymousModule(dependencies, callback);
37+
}
38+
};
39+
DefineFunc.amd = {
40+
jQuery: true
41+
};
42+
43+
const _requireFunc_config = function (params: IConfigurationOptions, shouldOverwrite: boolean = false): void {
44+
moduleManager.configure(params, shouldOverwrite);
45+
};
46+
const RequireFunc: IRequireFunc = <any>function () {
47+
if (arguments.length === 1) {
48+
if ((arguments[0] instanceof Object) && !Array.isArray(arguments[0])) {
49+
_requireFunc_config(arguments[0]);
50+
return;
3051
}
31-
if (!dependencies) {
32-
dependencies = ['require', 'exports', 'module'];
52+
if (typeof arguments[0] === 'string') {
53+
return moduleManager.synchronousRequire(arguments[0]);
3354
}
34-
35-
if (id) {
36-
moduleManager.defineModule(id, dependencies, callback, null, null);
37-
} else {
38-
moduleManager.enqueueDefineAnonymousModule(dependencies, callback);
55+
}
56+
if (arguments.length === 2 || arguments.length === 3) {
57+
if (Array.isArray(arguments[0])) {
58+
moduleManager.defineModule(Utilities.generateAnonymousModule(), arguments[0], arguments[1], arguments[2], null);
59+
return;
3960
}
40-
};
41-
42-
DefineFunc = <any>_defineFunc;
43-
DefineFunc.amd = {
44-
jQuery: true
45-
};
61+
}
62+
throw new Error('Unrecognized require call');
63+
};
64+
RequireFunc.config = _requireFunc_config;
65+
RequireFunc.getConfig = function (): IConfigurationOptions {
66+
return moduleManager.getConfig().getOptionsLiteral();
67+
};
68+
RequireFunc.reset = function (): void {
69+
moduleManager = moduleManager.reset();
70+
};
71+
RequireFunc.getBuildInfo = function (): IBuildModuleInfo[] {
72+
return moduleManager.getBuildInfo();
73+
};
74+
RequireFunc.getStats = function (): LoaderEvent[] {
75+
return moduleManager.getLoaderEvents();
76+
};
4677

47-
const _requireFunc_config = function (params: IConfigurationOptions, shouldOverwrite: boolean = false): void {
48-
moduleManager.configure(params, shouldOverwrite);
49-
};
78+
export function init(): void {
79+
if (typeof global.require !== 'undefined' || typeof require !== 'undefined') {
80+
const _nodeRequire = (global.require || require);
81+
if (typeof _nodeRequire === 'function' && typeof _nodeRequire.resolve === 'function') {
82+
// re-expose node's require function
83+
const nodeRequire = function (what) {
84+
moduleManager.getRecorder().record(LoaderEventType.NodeBeginNativeRequire, what);
85+
try {
86+
return _nodeRequire(what);
87+
} finally {
88+
moduleManager.getRecorder().record(LoaderEventType.NodeEndNativeRequire, what);
89+
}
90+
};
5091

51-
const _requireFunc = function () {
52-
if (arguments.length === 1) {
53-
if ((arguments[0] instanceof Object) && !Array.isArray(arguments[0])) {
54-
_requireFunc_config(arguments[0]);
55-
return;
56-
}
57-
if (typeof arguments[0] === 'string') {
58-
return moduleManager.synchronousRequire(arguments[0]);
59-
}
60-
}
61-
if (arguments.length === 2 || arguments.length === 3) {
62-
if (Array.isArray(arguments[0])) {
63-
moduleManager.defineModule(Utilities.generateAnonymousModule(), arguments[0], arguments[1], arguments[2], null);
64-
return;
65-
}
92+
global.nodeRequire = nodeRequire;
93+
(<any>RequireFunc).nodeRequire = nodeRequire;
94+
(<any>RequireFunc).__$__nodeRequire = nodeRequire;
6695
}
67-
throw new Error('Unrecognized require call');
68-
};
69-
70-
RequireFunc = <any>_requireFunc;
71-
RequireFunc.config = _requireFunc_config;
72-
RequireFunc.getConfig = function (): IConfigurationOptions {
73-
return moduleManager.getConfig().getOptionsLiteral();
74-
};
75-
76-
RequireFunc.reset = function (): void {
77-
moduleManager = moduleManager.reset();
78-
};
79-
80-
RequireFunc.getBuildInfo = function (): IBuildModuleInfo[] {
81-
return moduleManager.getBuildInfo();
82-
};
83-
84-
RequireFunc.getStats = function (): LoaderEvent[] {
85-
return moduleManager.getLoaderEvents();
86-
};
87-
}
88-
89-
export function init(): void {
90-
createGlobalAMDFuncs();
91-
92-
const env = Environment.detect();
93-
const scriptLoader: IScriptLoader = createScriptLoader(env);
94-
moduleManager = new ModuleManager(env, scriptLoader, DefineFunc, RequireFunc, Utilities.getHighPerformanceTimestamp());
95-
96-
if (env.isNode) {
97-
var _nodeRequire = (global.require || require);
98-
var nodeRequire = function (what) {
99-
moduleManager.getRecorder().record(LoaderEventType.NodeBeginNativeRequire, what);
100-
try {
101-
return _nodeRequire(what);
102-
} finally {
103-
moduleManager.getRecorder().record(LoaderEventType.NodeEndNativeRequire, what);
104-
}
105-
};
106-
107-
global.nodeRequire = nodeRequire;
108-
(<any>RequireFunc).nodeRequire = nodeRequire;
10996
}
11097

11198
if (env.isNode && !env.isElectronRenderer) {
11299
module.exports = RequireFunc;
113-
// These two defs are fore the local closure defined in node in the case that the loader is concatenated
114-
define = function () {
115-
DefineFunc.apply(null, arguments);
116-
};
117100
require = <any>RequireFunc;
118101
} else {
119-
// The global variable require can configure the loader
120-
if (typeof global.require !== 'undefined' && typeof global.require !== 'function') {
121-
RequireFunc.config(global.require);
122-
}
123102
if (!env.isElectronRenderer) {
124-
global.define = define = DefineFunc;
125-
} else {
126-
define = function () {
127-
DefineFunc.apply(null, arguments);
128-
};
103+
global.define = DefineFunc;
129104
}
130105
global.require = RequireFunc;
131-
global.require.__$__nodeRequire = nodeRequire;
132106
}
133107
}
134108

135-
if (
136-
typeof doNotInitLoader === 'undefined' &&
137-
(typeof global.define !== 'function' || !global.define.amd)
138-
) {
139-
init();
109+
if (typeof global.define !== 'function' || !global.define.amd) {
110+
moduleManager = new ModuleManager(env, createScriptLoader(env), DefineFunc, RequireFunc, Utilities.getHighPerformanceTimestamp());
111+
112+
// The global variable require can configure the loader
113+
if (typeof global.require !== 'undefined' && typeof global.require !== 'function') {
114+
RequireFunc.config(global.require);
115+
}
116+
117+
// This define is for the local closure defined in node in the case that the loader is concatenated
118+
define = function () {
119+
return DefineFunc.apply(null, arguments);
120+
};
121+
define.amd = DefineFunc.amd;
122+
123+
if (typeof doNotInitLoader === 'undefined') {
124+
init();
125+
}
140126
}
127+
141128
}

0 commit comments

Comments
 (0)