Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intern config: add support for 'internLoaderPath' in loader options #1161

Merged
merged 5 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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