Skip to content

Adding support for npm namespaces #143

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Aug 1, 2017
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
17 changes: 9 additions & 8 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
import ModuleShim = DojoLoader.ModuleShim;
import Module = DojoLoader.Module;
import Package = DojoLoader.Package;

declare const load: (module: string) => any;
declare const Packages: {} | undefined;
Expand Down Expand Up @@ -544,16 +545,16 @@ declare const Packages: {} | undefined;
}

function getModuleInformation(moduleId: string, referenceModule?: DojoLoader.Module): DojoLoader.Module {
let match = moduleId.match(/^([^\/]+)(\/(.+))?$/);
let packageId = match ? match[1] : '';
let pack = config && config.pkgs ? config.pkgs[packageId] : {};
let packageId = '';
let pack: Package = {};
let moduleIdInPackage = '';

if (pack) {
moduleId = packageId + '/' + (moduleIdInPackage = ((match && match[3]) || pack.main || 'main'));
}
else {
packageId = '';
const matches = Object.keys((config && config.pkgs || {})).filter(pkg => (moduleId + '/').indexOf(pkg + '/') === 0).sort((a, b) => a.length > b.length ? -1 : 1);

if (matches.length) {
packageId = matches.shift() as string;
pack = config.pkgs![packageId];
moduleId = packageId + '/' + (moduleIdInPackage = (moduleId.substr(packageId.length + 1) || pack.main || 'main'));
}

let module = modules[moduleId];
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/require/config/packages3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>require.config Test</title>
</head>

<body>
<script src="../../../../src/loader.js"></script>
<script>
require.config({
packages: [
{
name: '@test/common',
location: '../../../common'
}
]
});

require([
'@test/common/app'
], function (app) {
window.loaderTestResults = app;
});
</script>
</body>
</html>
26 changes: 26 additions & 0 deletions tests/functional/require/config/packages4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>require.config Test</title>
</head>

<body>
<script src="../../../../src/loader.js"></script>
<script>
require.config({
packages: [
{ name: '@common', location: '../../../common' },
{ name: '@common/a', location: '../../../common/a', main: 'app' }
]
});

require([
'@common/app',
'@common/a'
], function (app) {
window.loaderTestResults = app;
});
</script>
</body>
</html>
12 changes: 12 additions & 0 deletions tests/functional/require/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ registerSuite({
return executeTest(this, './config/packages2.html', function (results: any) {
assert.strictEqual(results, appMessage, '"app" module should load');
});
},

'package name with slashes'(this: any) {
return executeTest(this, './config/packages3.html', function (results: any) {
assert.strictEqual(results, appMessage, '"app" module should load');
});
},

'nested packages'(this: any) {
return executeTest(this, './config/packages4.html', function (results: any) {
assert.strictEqual(results, appMessage, '"app" module should load');
});
}
},

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,48 @@ registerSuite({
], dfd.callback(function (app: any) {
assert.strictEqual(app, 'app', '"app" module should load');
}));
},
'slashes in package name'(this: any) {
let dfd = this.async(DEFAULT_TIMEOUT);

setErrorHandler(dfd);

global.require.config({
packages: [
{
name: '@test/common',
location: './_build/tests/common',
main: 'app'
}
]
});

global.require([
'@test/common'
], dfd.callback(function (app: any) {
assert.strictEqual(app, 'app', '"app" module should load');
}));
},
'single @ package'(this: any) {
let dfd = this.async(DEFAULT_TIMEOUT);

setErrorHandler(dfd);

global.require.config({
packages: [
{
name: '@test',
location: './_build/tests',
main: 'app'
}
]
});

global.require([
'@test/common/app'
], dfd.callback(function (app: any) {
assert.strictEqual(app, 'app', '"app" module should load');
}));
}
},

Expand Down