Skip to content

Commit 06c14e4

Browse files
committed
Get rid of eager isNode
1 parent b82178c commit 06c14e4

File tree

10 files changed

+112
-109
lines changed

10 files changed

+112
-109
lines changed

src/core/configuration.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ namespace AMDLoader {
187187

188188
export class Configuration {
189189

190+
private readonly _isNode: boolean;
191+
190192
private options: IConfigurationOptions;
191193

192194
/**
@@ -204,20 +206,21 @@ namespace AMDLoader {
204206
*/
205207
private sortedPathsRules: { from: string; to: string[]; }[];
206208

207-
constructor(options?: IConfigurationOptions) {
209+
constructor(isNode: boolean, options?: IConfigurationOptions) {
210+
this._isNode = isNode;
208211
this.options = ConfigurationOptionsUtil.mergeConfigurationOptions(options);
209212

210213
this._createIgnoreDuplicateModulesMap();
211214
this._createNodeModulesMap();
212215
this._createSortedPathsRules();
213216

214217
if (this.options.baseUrl === '') {
215-
if (isNode && this.options.nodeRequire && this.options.nodeRequire.main && this.options.nodeRequire.main.filename) {
218+
if (this._isNode && this.options.nodeRequire && this.options.nodeRequire.main && this.options.nodeRequire.main.filename) {
216219
let nodeMain = this.options.nodeRequire.main.filename;
217220
let dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\'));
218221
this.options.baseUrl = nodeMain.substring(0, dirnameIndex + 1);
219222
}
220-
if (isNode && this.options.nodeMain) {
223+
if (this._isNode && this.options.nodeMain) {
221224
let nodeMain = this.options.nodeMain;
222225
let dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\'));
223226
this.options.baseUrl = nodeMain.substring(0, dirnameIndex + 1);
@@ -269,7 +272,7 @@ namespace AMDLoader {
269272
* @result A new configuration
270273
*/
271274
public cloneAndMerge(options?: IConfigurationOptions): Configuration {
272-
return new Configuration(ConfigurationOptionsUtil.mergeConfigurationOptions(options, this.options));
275+
return new Configuration(this._isNode, ConfigurationOptionsUtil.mergeConfigurationOptions(options, this.options));
273276
}
274277

275278
/**

src/core/env.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ declare var Map: MapConstructor;
5959

6060
namespace AMDLoader {
6161
export const global: any = _amdLoaderGlobal
62-
export const isNode = (typeof module !== 'undefined' && !!module.exports);
6362
export const isWebWorker = (typeof global.importScripts === 'function');
6463
export const isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer');
6564
export const hasPerformanceNow = (global.performance && typeof global.performance.now === 'function');
@@ -68,16 +67,20 @@ namespace AMDLoader {
6867

6968
public static detect(): Environment {
7069
return new Environment({
71-
isWindows: this._isWindows()
70+
isWindows: this._isWindows(),
71+
isNode: (typeof module !== 'undefined' && !!module.exports)
7272
});
7373
}
7474

7575
public readonly isWindows: boolean;
76+
public readonly isNode: boolean;
7677

7778
constructor(opts: {
78-
isWindows: boolean
79+
isWindows: boolean;
80+
isNode: boolean;
7981
}) {
8082
this.isWindows = opts.isWindows;
83+
this.isNode = opts.isNode;
8184
}
8285

8386
private static _isWindows(): boolean {
@@ -92,4 +95,6 @@ namespace AMDLoader {
9295
return false;
9396
}
9497
}
98+
99+
export const _env = Environment.detect();
95100
}

src/core/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace AMDLoader {
7575
* Non standard extension to reset completely the loader state. This is used for running amdjs tests
7676
*/
7777
public static reset(): void {
78-
moduleManager = new ModuleManager(scriptLoader, loaderAvailableTimestamp);
78+
moduleManager = new ModuleManager(_env, scriptLoader, loaderAvailableTimestamp);
7979
}
8080

8181
/**
@@ -93,10 +93,10 @@ namespace AMDLoader {
9393
}
9494
}
9595

96-
function init(): void {
97-
moduleManager = new ModuleManager(scriptLoader, loaderAvailableTimestamp);
96+
function init(env: Environment): void {
97+
moduleManager = new ModuleManager(env, scriptLoader, loaderAvailableTimestamp);
9898

99-
if (isNode) {
99+
if (env.isNode) {
100100
var _nodeRequire = (global.require || require);
101101
var nodeRequire = function (what) {
102102
moduleManager.getRecorder().record(LoaderEventType.NodeBeginNativeRequire, what);
@@ -111,7 +111,7 @@ namespace AMDLoader {
111111
(<any>RequireFunc).nodeRequire = nodeRequire;
112112
}
113113

114-
if (isNode && !isElectronRenderer) {
114+
if (env.isNode && !isElectronRenderer) {
115115
module.exports = RequireFunc;
116116
// These two defs are fore the local closure defined in node in the case that the loader is concatenated
117117
define = function () {
@@ -136,7 +136,7 @@ namespace AMDLoader {
136136
}
137137

138138
if (typeof global.define !== 'function' || !global.define.amd) {
139-
init();
139+
init(_env);
140140
loaderAvailableTimestamp = getHighPerformanceTimestamp();
141141
}
142142

src/core/moduleManager.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ namespace AMDLoader {
315315

316316
export class ModuleManager {
317317

318+
private readonly _env: Environment;
319+
318320
private readonly _moduleIdProvider: ModuleIdProvider;
319321

320322
private _config: Configuration;
@@ -357,10 +359,11 @@ namespace AMDLoader {
357359
private _buildInfoDefineStack: string[];
358360
private _buildInfoDependencies: string[][];
359361

360-
constructor(scriptLoader: IScriptLoader, loaderAvailableTimestamp: number = 0) {
362+
constructor(env: Environment, scriptLoader: IScriptLoader, loaderAvailableTimestamp: number = 0) {
363+
this._env = env;
361364
this._loaderAvailableTimestamp = loaderAvailableTimestamp;
362365
this._moduleIdProvider = new ModuleIdProvider();
363-
this._config = new Configuration();
366+
this._config = new Configuration(this._env.isNode);
364367
this._scriptLoader = scriptLoader;
365368
this._modules2 = [];
366369
this._knownModules2 = [];
@@ -563,7 +566,7 @@ namespace AMDLoader {
563566
public configure(params: IConfigurationOptions, shouldOverwrite: boolean): void {
564567
let oldShouldRecordStats = this._config.shouldRecordStats();
565568
if (shouldOverwrite) {
566-
this._config = new Configuration(params);
569+
this._config = new Configuration(this._env.isNode, params);
567570
} else {
568571
this._config = this._config.cloneAndMerge(params);
569572
}
@@ -749,7 +752,7 @@ namespace AMDLoader {
749752
let strModuleId = this._moduleIdProvider.getStrModuleId(moduleId);
750753
let paths = this._config.moduleIdToPaths(strModuleId);
751754

752-
if (isNode && strModuleId.indexOf('/') === -1) {
755+
if (this._env.isNode && strModuleId.indexOf('/') === -1) {
753756
paths.push('node|' + strModuleId);
754757
}
755758

src/core/scriptLoader.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ namespace AMDLoader {
290290

291291
} else {
292292

293-
scriptSrc = Utilities.fileUriToFilePath(this._env, scriptSrc);
293+
scriptSrc = Utilities.fileUriToFilePath(this._env.isWindows, scriptSrc);
294294

295295
this._fs.readFile(scriptSrc, { encoding: 'utf8' }, (err, data: string) => {
296296
if (err) {
@@ -416,13 +416,11 @@ namespace AMDLoader {
416416
}
417417
}
418418

419-
export const env = Environment.detect();
420-
421419
export const scriptLoader: IScriptLoader = new OnlyOnceScriptLoader(
422420
isWebWorker ?
423421
new WorkerScriptLoader()
424-
: isNode ?
425-
new NodeScriptLoader(env)
422+
: _env.isNode ?
423+
new NodeScriptLoader(_env)
426424
: new BrowserScriptLoader()
427425
);
428426
}

src/core/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace AMDLoader {
99
/**
1010
* This method does not take care of / vs \
1111
*/
12-
public static fileUriToFilePath(env: Environment, uri: string): string {
12+
public static fileUriToFilePath(isWindows: boolean, uri: string): string {
1313
uri = decodeURI(uri);
14-
if (env.isWindows) {
14+
if (isWindows) {
1515
if (/^file:\/\/\//.test(uri)) {
1616
// This is a URI without a hostname => return only the path segment
1717
return uri.substr(8);

src/loader.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ interface MapConstructor {
3131
declare var Map: MapConstructor;
3232
declare namespace AMDLoader {
3333
const global: any;
34-
const isNode: boolean;
3534
const isWebWorker: boolean;
3635
const isElectronRenderer: boolean;
3736
const hasPerformanceNow: boolean;
3837
class Environment {
3938
static detect(): Environment;
4039
readonly isWindows: boolean;
40+
readonly isNode: boolean;
4141
constructor(opts: {
4242
isWindows: boolean;
43+
isNode: boolean;
4344
});
4445
private static _isWindows();
4546
}
47+
const _env: Environment;
4648
}
4749
declare namespace AMDLoader {
4850
enum LoaderEventType {
@@ -85,7 +87,7 @@ declare namespace AMDLoader {
8587
/**
8688
* This method does not take care of / vs \
8789
*/
88-
static fileUriToFilePath(env: Environment, uri: string): string;
90+
static fileUriToFilePath(isWindows: boolean, uri: string): string;
8991
static startsWith(haystack: string, needle: string): boolean;
9092
static endsWith(haystack: string, needle: string): boolean;
9193
static containsQueryString(url: string): boolean;
@@ -184,6 +186,7 @@ declare namespace AMDLoader {
184186
static mergeConfigurationOptions(overwrite?: IConfigurationOptions, base?: IConfigurationOptions): IConfigurationOptions;
185187
}
186188
class Configuration {
189+
private readonly _isNode;
187190
private options;
188191
/**
189192
* Generated from the `ignoreDuplicateModules` configuration option.
@@ -197,7 +200,7 @@ declare namespace AMDLoader {
197200
* Generated from the `paths` configuration option. These are sorted with the longest `from` first.
198201
*/
199202
private sortedPathsRules;
200-
constructor(options?: IConfigurationOptions);
203+
constructor(isNode: boolean, options?: IConfigurationOptions);
201204
private _createIgnoreDuplicateModulesMap();
202205
private _createNodeModulesMap();
203206
private _createSortedPathsRules();
@@ -258,7 +261,6 @@ declare namespace AMDLoader {
258261
interface IScriptLoader {
259262
load(moduleManager: IModuleManager, scriptPath: string, loadCallback: () => void, errorCallback: (err: any) => void): void;
260263
}
261-
const env: Environment;
262264
const scriptLoader: IScriptLoader;
263265
}
264266
declare namespace AMDLoader {
@@ -362,6 +364,7 @@ declare namespace AMDLoader {
362364
}
363365
type Dependency = RegularDependency | PluginDependency;
364366
class ModuleManager {
367+
private readonly _env;
365368
private readonly _moduleIdProvider;
366369
private _config;
367370
private readonly _loaderAvailableTimestamp;
@@ -394,7 +397,7 @@ declare namespace AMDLoader {
394397
private _buildInfoPath;
395398
private _buildInfoDefineStack;
396399
private _buildInfoDependencies;
397-
constructor(scriptLoader: IScriptLoader, loaderAvailableTimestamp?: number);
400+
constructor(env: Environment, scriptLoader: IScriptLoader, loaderAvailableTimestamp?: number);
398401
private static _findRelevantLocationInStack(needle, stack);
399402
getBuildInfo(): IBuildModuleInfo[];
400403
private _recorder;

0 commit comments

Comments
 (0)