Skip to content

Commit

Permalink
feat: add support for 'internLoaderPath' in loader options
Browse files Browse the repository at this point in the history
resolves #872
  • Loading branch information
msssk authored and jason0x43 committed Jun 4, 2020
1 parent d0cad81 commit 9b3fd5d
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 424 deletions.
842 changes: 443 additions & 399 deletions docs/api.json

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ environment (CommonJS in Node and <script> tag injection in the browser).

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

```json5
{
Expand All @@ -430,6 +432,19 @@ using NPM.
}
```

Custom loader path with `internLoaderPath`:

```json5
{
browser: {
loader: 'dojo',
options: {
internLoaderPath: '../path/to/dojo/dojo.js'
}
}
}
```

Additional options may be provided through the options parameter. These options
are passed through to the registered loader script.

Expand Down
11 changes: 10 additions & 1 deletion src/lib/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,16 @@ export interface BenchmarkConfig extends BenchmarkReporterOptions {

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

export interface EnvironmentSpec {
Expand Down
20 changes: 12 additions & 8 deletions src/loaders/dojo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
*/
intern.registerLoader(options => {
const globalObj: any = typeof window !== 'undefined' ? window : global;
const {
internLoaderPath = 'node_modules/dojo/dojo.js',
...loaderConfig
} = options;

options.baseUrl = options.baseUrl || intern.config.basePath;
if (!('async' in options)) {
options.async = true;
loaderConfig.baseUrl = loaderConfig.baseUrl || intern.config.basePath;
if (!('async' in loaderConfig)) {
loaderConfig.async = true;
}

options.has = {
loaderConfig.has = {
'dojo-timeout-api': true,
...options.has
...loaderConfig.has
};

intern.log('Configuring Dojo loader with:', options);
globalObj.dojoConfig = options;
intern.log('Configuring Dojo loader with:', loaderConfig);
globalObj.dojoConfig = loaderConfig;

return intern.loadScript('node_modules/dojo/dojo.js').then(() => {
return intern.loadScript(internLoaderPath).then(() => {
const require = globalObj.require;
intern.log('Using Dojo loader');

Expand Down
13 changes: 9 additions & 4 deletions src/loaders/dojo2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
*/
intern.registerLoader(options => {
const globalObj: any = typeof window !== 'undefined' ? window : global;
return intern.loadScript('node_modules/@dojo/loader/loader.js').then(() => {
const {
internLoaderPath = 'node_modules/@dojo/loader/loader.js',
...loaderConfig
} = options;

return intern.loadScript(internLoaderPath).then(() => {
const require: DojoLoader.RootRequire = globalObj.require;
intern.log('Using Dojo 2 loader');

options.baseUrl = options.baseUrl || intern.config.basePath;
intern.log('Configuring loader with:', options);
require.config(options);
loaderConfig.baseUrl = loaderConfig.baseUrl || intern.config.basePath;
intern.log('Configuring loader with:', loaderConfig);
require.config(loaderConfig);

return (modules: string[]) => {
let handle: { remove(): void };
Expand Down
19 changes: 11 additions & 8 deletions src/loaders/systemjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
*/

intern.registerLoader(options => {
options.baseURL = options.baseURL || intern.config.basePath;
const globalObj: any = typeof window !== 'undefined' ? window : global;
const {
internLoaderPath = 'node_modules/systemjs/dist/system.src.js',
...loaderConfig
} = options;

loaderConfig.baseURL = loaderConfig.baseURL || intern.config.basePath;

if (intern.environment === 'browser') {
return intern
.loadScript('node_modules/systemjs/dist/system.src.js')
.then(() => {
return configAndLoad(SystemJS);
});
return intern.loadScript(internLoaderPath).then(() => {
return configAndLoad(SystemJS);
});
} else {
// Use globalObj to get to require to improve testability
const SystemJS = (globalObj.require || require)('systemjs');
Expand All @@ -25,8 +28,8 @@ intern.registerLoader(options => {
function configAndLoad(loader: typeof SystemJS) {
intern.log('Using SystemJS loader');

intern.log('Configuring SystemJS with:', options);
loader.config(options);
intern.log('Configuring SystemJS with:', loaderConfig);
loader.config(loaderConfig);

return (modules: string[]) => {
intern.log('Loading modules with SystemJS:', modules);
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/loaders/dojo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ registerSuite('loaders/dojo', function() {
const mockIntern = {
config: { basePath: '/' },
emit: spy(() => {}),
loadScript: spy(() => Promise.resolve()),
loadScript: spy((_path: string) => Promise.resolve()),
registerLoader: spy((_init: LoaderInit) => {}),
log: spy(() => {})
};
Expand Down Expand Up @@ -65,8 +65,14 @@ registerSuite('loaders/dojo', function() {
tests: {
init() {
const init = mockIntern.registerLoader.getCall(0).args[0];
const defaultLoaderPath = 'node_modules/dojo/dojo.js';

return (init({}) as Promise<any>).then(() => {
assert.equal(mockIntern.loadScript.callCount, 1);
assert.equal(
mockIntern.loadScript.getCall(0).args[0],
defaultLoaderPath
);
assert.deepEqual(
global.dojoConfig,
{
Expand Down Expand Up @@ -112,6 +118,20 @@ registerSuite('loaders/dojo', function() {
}
);
});
},

internLoaderPath() {
const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
const loaderOptions = {
internLoaderPath: 'foo/bar'
};

return Promise.resolve(init(loaderOptions)).then(() => {
assert.equal(
mockIntern.loadScript.lastCall.args[0],
loaderOptions.internLoaderPath
);
});
}
}
};
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/loaders/dojo2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ registerSuite('loaders/dojo2', function() {
const mockIntern = {
config: { basePath: '/' },
emit: spy(() => {}),
loadScript: spy(() => Promise.resolve()),
loadScript: spy((_path: string) => Promise.resolve()),
registerLoader: spy((_init: LoaderInit) => {}),
log: spy(() => {})
};
Expand Down Expand Up @@ -67,8 +67,14 @@ registerSuite('loaders/dojo2', function() {
tests: {
init() {
const init = mockIntern.registerLoader.getCall(0).args[0];
const defaultLoaderPath = 'node_modules/@dojo/loader/loader.js';

return (init({}) as Promise<any>).then(() => {
assert.equal(mockIntern.loadScript.callCount, 1);
assert.equal(
mockIntern.loadScript.getCall(0).args[0],
defaultLoaderPath
);
});
},

Expand Down Expand Up @@ -103,6 +109,20 @@ registerSuite('loaders/dojo2', function() {
}
);
});
},

internLoaderPath() {
const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
const loaderOptions = {
internLoaderPath: 'foo/bar'
};

return Promise.resolve(init(loaderOptions)).then(() => {
assert.equal(
mockIntern.loadScript.lastCall.args[0],
loaderOptions.internLoaderPath
);
});
}
}
};
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/loaders/systemjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ registerSuite('loaders/systemjs', function() {
environment: intern.environment,
config: { basePath: '/' },
emit: spy(() => {}),
loadScript: spy(() => Promise.resolve()),
loadScript: spy((_path: string) => Promise.resolve()),
registerLoader: spy((_init: LoaderInit) => {}),
log: spy(() => {})
};
Expand Down Expand Up @@ -49,6 +49,7 @@ registerSuite('loaders/systemjs', function() {
global.intern = mockIntern;
global.require = fakeRequire;
global.SystemJS = mockSystemJS;
mockIntern.environment = intern.environment;
mockIntern.emit.resetHistory();
mockIntern.loadScript.resetHistory();
fakeRequire.resetHistory();
Expand All @@ -63,9 +64,15 @@ registerSuite('loaders/systemjs', function() {
tests: {
init() {
const init = mockIntern.registerLoader.getCall(0).args[0];
const defaultLoaderPath = 'node_modules/systemjs/dist/system.src.js';

return Promise.resolve(init({})).then(() => {
if (intern.environment === 'browser') {
assert.equal(mockIntern.loadScript.callCount, 1);
assert.equal(
mockIntern.loadScript.getCall(0).args[0],
defaultLoaderPath
);
}
});
},
Expand Down Expand Up @@ -95,6 +102,24 @@ registerSuite('loaders/systemjs', function() {
}
);
});
},

internLoaderPath() {
// the SystemJS loader only calls `loadScript` in the browser
mockIntern.environment = 'browser';

const init: LoaderInit = mockIntern.registerLoader.getCall(0).args[0];
const loaderOptions = {
internLoaderPath: 'foo/bar'
};

return Promise.resolve(init(loaderOptions)).then(() => {
assert.equal(mockIntern.loadScript.callCount, 1);
assert.equal(
mockIntern.loadScript.lastCall.args[0],
loaderOptions.internLoaderPath
);
});
}
}
};
Expand Down

0 comments on commit 9b3fd5d

Please sign in to comment.