Skip to content

Commit 9b3fd5d

Browse files
mssskjason0x43
authored andcommitted
feat: add support for 'internLoaderPath' in loader options
resolves #872
1 parent d0cad81 commit 9b3fd5d

File tree

9 files changed

+569
-424
lines changed

9 files changed

+569
-424
lines changed

docs/api.json

Lines changed: 443 additions & 399 deletions
Large diffs are not rendered by default.

docs/configuration.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ environment (CommonJS in Node and <script> tag injection in the browser).
420420

421421
To use one of Intern's pre-defined loader scripts, simply specify it's name. The
422422
loader script will expect the loader package to be installed in `node_modules`
423-
using NPM.
423+
using NPM. The loader file location can be customized with the `internLoaderPath`
424+
option, which although it is specified on the `options` object passed to the
425+
loader, it will be consumed by Intern and not passed to the loader config.
424426

425427
```json5
426428
{
@@ -430,6 +432,19 @@ using NPM.
430432
}
431433
```
432434

435+
Custom loader path with `internLoaderPath`:
436+
437+
```json5
438+
{
439+
browser: {
440+
loader: 'dojo',
441+
options: {
442+
internLoaderPath: '../path/to/dojo/dojo.js'
443+
}
444+
}
445+
}
446+
```
447+
433448
Additional options may be provided through the options parameter. These options
434449
are passed through to the registered loader script.
435450

src/lib/common/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,16 @@ export interface BenchmarkConfig extends BenchmarkReporterOptions {
499499

500500
export interface LoaderDescriptor {
501501
script: string;
502-
options?: { [key: string]: any };
502+
options?: {
503+
/**
504+
* An optional file path for the loader. If unspecified Intern will assume the loader's package is
505+
* installed in `node_modules`. This property is prefixed with "intern" to distinguish it from
506+
* other properties which will be passed to the loader. If present, Intern will read this property
507+
* and then filter it out of the config passed to the loader.
508+
*/
509+
internLoaderPath?: string;
510+
[key: string]: any;
511+
};
503512
}
504513

505514
export interface EnvironmentSpec {

src/loaders/dojo.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
*/
66
intern.registerLoader(options => {
77
const globalObj: any = typeof window !== 'undefined' ? window : global;
8+
const {
9+
internLoaderPath = 'node_modules/dojo/dojo.js',
10+
...loaderConfig
11+
} = options;
812

9-
options.baseUrl = options.baseUrl || intern.config.basePath;
10-
if (!('async' in options)) {
11-
options.async = true;
13+
loaderConfig.baseUrl = loaderConfig.baseUrl || intern.config.basePath;
14+
if (!('async' in loaderConfig)) {
15+
loaderConfig.async = true;
1216
}
1317

14-
options.has = {
18+
loaderConfig.has = {
1519
'dojo-timeout-api': true,
16-
...options.has
20+
...loaderConfig.has
1721
};
1822

19-
intern.log('Configuring Dojo loader with:', options);
20-
globalObj.dojoConfig = options;
23+
intern.log('Configuring Dojo loader with:', loaderConfig);
24+
globalObj.dojoConfig = loaderConfig;
2125

22-
return intern.loadScript('node_modules/dojo/dojo.js').then(() => {
26+
return intern.loadScript(internLoaderPath).then(() => {
2327
const require = globalObj.require;
2428
intern.log('Using Dojo loader');
2529

src/loaders/dojo2.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
*/
88
intern.registerLoader(options => {
99
const globalObj: any = typeof window !== 'undefined' ? window : global;
10-
return intern.loadScript('node_modules/@dojo/loader/loader.js').then(() => {
10+
const {
11+
internLoaderPath = 'node_modules/@dojo/loader/loader.js',
12+
...loaderConfig
13+
} = options;
14+
15+
return intern.loadScript(internLoaderPath).then(() => {
1116
const require: DojoLoader.RootRequire = globalObj.require;
1217
intern.log('Using Dojo 2 loader');
1318

14-
options.baseUrl = options.baseUrl || intern.config.basePath;
15-
intern.log('Configuring loader with:', options);
16-
require.config(options);
19+
loaderConfig.baseUrl = loaderConfig.baseUrl || intern.config.basePath;
20+
intern.log('Configuring loader with:', loaderConfig);
21+
require.config(loaderConfig);
1722

1823
return (modules: string[]) => {
1924
let handle: { remove(): void };

src/loaders/systemjs.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
*/
88

99
intern.registerLoader(options => {
10-
options.baseURL = options.baseURL || intern.config.basePath;
1110
const globalObj: any = typeof window !== 'undefined' ? window : global;
11+
const {
12+
internLoaderPath = 'node_modules/systemjs/dist/system.src.js',
13+
...loaderConfig
14+
} = options;
15+
16+
loaderConfig.baseURL = loaderConfig.baseURL || intern.config.basePath;
1217

1318
if (intern.environment === 'browser') {
14-
return intern
15-
.loadScript('node_modules/systemjs/dist/system.src.js')
16-
.then(() => {
17-
return configAndLoad(SystemJS);
18-
});
19+
return intern.loadScript(internLoaderPath).then(() => {
20+
return configAndLoad(SystemJS);
21+
});
1922
} else {
2023
// Use globalObj to get to require to improve testability
2124
const SystemJS = (globalObj.require || require)('systemjs');
@@ -25,8 +28,8 @@ intern.registerLoader(options => {
2528
function configAndLoad(loader: typeof SystemJS) {
2629
intern.log('Using SystemJS loader');
2730

28-
intern.log('Configuring SystemJS with:', options);
29-
loader.config(options);
31+
intern.log('Configuring SystemJS with:', loaderConfig);
32+
loader.config(loaderConfig);
3033

3134
return (modules: string[]) => {
3235
intern.log('Loading modules with SystemJS:', modules);

tests/unit/loaders/dojo.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ registerSuite('loaders/dojo', function() {
1515
const mockIntern = {
1616
config: { basePath: '/' },
1717
emit: spy(() => {}),
18-
loadScript: spy(() => Promise.resolve()),
18+
loadScript: spy((_path: string) => Promise.resolve()),
1919
registerLoader: spy((_init: LoaderInit) => {}),
2020
log: spy(() => {})
2121
};
@@ -65,8 +65,14 @@ registerSuite('loaders/dojo', function() {
6565
tests: {
6666
init() {
6767
const init = mockIntern.registerLoader.getCall(0).args[0];
68+
const defaultLoaderPath = 'node_modules/dojo/dojo.js';
69+
6870
return (init({}) as Promise<any>).then(() => {
6971
assert.equal(mockIntern.loadScript.callCount, 1);
72+
assert.equal(
73+
mockIntern.loadScript.getCall(0).args[0],
74+
defaultLoaderPath
75+
);
7076
assert.deepEqual(
7177
global.dojoConfig,
7278
{
@@ -112,6 +118,20 @@ registerSuite('loaders/dojo', function() {
112118
}
113119
);
114120
});
121+
},
122+
123+
internLoaderPath() {
124+
const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
125+
const loaderOptions = {
126+
internLoaderPath: 'foo/bar'
127+
};
128+
129+
return Promise.resolve(init(loaderOptions)).then(() => {
130+
assert.equal(
131+
mockIntern.loadScript.lastCall.args[0],
132+
loaderOptions.internLoaderPath
133+
);
134+
});
115135
}
116136
}
117137
};

tests/unit/loaders/dojo2.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ registerSuite('loaders/dojo2', function() {
1515
const mockIntern = {
1616
config: { basePath: '/' },
1717
emit: spy(() => {}),
18-
loadScript: spy(() => Promise.resolve()),
18+
loadScript: spy((_path: string) => Promise.resolve()),
1919
registerLoader: spy((_init: LoaderInit) => {}),
2020
log: spy(() => {})
2121
};
@@ -67,8 +67,14 @@ registerSuite('loaders/dojo2', function() {
6767
tests: {
6868
init() {
6969
const init = mockIntern.registerLoader.getCall(0).args[0];
70+
const defaultLoaderPath = 'node_modules/@dojo/loader/loader.js';
71+
7072
return (init({}) as Promise<any>).then(() => {
7173
assert.equal(mockIntern.loadScript.callCount, 1);
74+
assert.equal(
75+
mockIntern.loadScript.getCall(0).args[0],
76+
defaultLoaderPath
77+
);
7278
});
7379
},
7480

@@ -103,6 +109,20 @@ registerSuite('loaders/dojo2', function() {
103109
}
104110
);
105111
});
112+
},
113+
114+
internLoaderPath() {
115+
const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
116+
const loaderOptions = {
117+
internLoaderPath: 'foo/bar'
118+
};
119+
120+
return Promise.resolve(init(loaderOptions)).then(() => {
121+
assert.equal(
122+
mockIntern.loadScript.lastCall.args[0],
123+
loaderOptions.internLoaderPath
124+
);
125+
});
106126
}
107127
}
108128
};

tests/unit/loaders/systemjs.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ registerSuite('loaders/systemjs', function() {
1717
environment: intern.environment,
1818
config: { basePath: '/' },
1919
emit: spy(() => {}),
20-
loadScript: spy(() => Promise.resolve()),
20+
loadScript: spy((_path: string) => Promise.resolve()),
2121
registerLoader: spy((_init: LoaderInit) => {}),
2222
log: spy(() => {})
2323
};
@@ -49,6 +49,7 @@ registerSuite('loaders/systemjs', function() {
4949
global.intern = mockIntern;
5050
global.require = fakeRequire;
5151
global.SystemJS = mockSystemJS;
52+
mockIntern.environment = intern.environment;
5253
mockIntern.emit.resetHistory();
5354
mockIntern.loadScript.resetHistory();
5455
fakeRequire.resetHistory();
@@ -63,9 +64,15 @@ registerSuite('loaders/systemjs', function() {
6364
tests: {
6465
init() {
6566
const init = mockIntern.registerLoader.getCall(0).args[0];
67+
const defaultLoaderPath = 'node_modules/systemjs/dist/system.src.js';
68+
6669
return Promise.resolve(init({})).then(() => {
6770
if (intern.environment === 'browser') {
6871
assert.equal(mockIntern.loadScript.callCount, 1);
72+
assert.equal(
73+
mockIntern.loadScript.getCall(0).args[0],
74+
defaultLoaderPath
75+
);
6976
}
7077
});
7178
},
@@ -95,6 +102,24 @@ registerSuite('loaders/systemjs', function() {
95102
}
96103
);
97104
});
105+
},
106+
107+
internLoaderPath() {
108+
// the SystemJS loader only calls `loadScript` in the browser
109+
mockIntern.environment = 'browser';
110+
111+
const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
112+
const loaderOptions = {
113+
internLoaderPath: 'foo/bar'
114+
};
115+
116+
return Promise.resolve(init(loaderOptions)).then(() => {
117+
assert.equal(mockIntern.loadScript.callCount, 1);
118+
assert.equal(
119+
mockIntern.loadScript.lastCall.args[0],
120+
loaderOptions.internLoaderPath
121+
);
122+
});
98123
}
99124
}
100125
};

0 commit comments

Comments
 (0)