Skip to content

Commit 48a4695

Browse files
authored
Adding support for npm namespaces (#143)
* Adding support for npm namespaces, issue #134 * Adding functional test for @ package, issue #134 * Fixing support for @ root packages, issue #134 * Improving package check, issue #134
1 parent 13adb56 commit 48a4695

File tree

5 files changed

+116
-8
lines changed

5 files changed

+116
-8
lines changed

src/loader.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
import ModuleShim = DojoLoader.ModuleShim;
33
import Module = DojoLoader.Module;
4+
import Package = DojoLoader.Package;
45

56
declare const load: (module: string) => any;
67
declare const Packages: {} | undefined;
@@ -544,16 +545,16 @@ declare const Packages: {} | undefined;
544545
}
545546

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

552-
if (pack) {
553-
moduleId = packageId + '/' + (moduleIdInPackage = ((match && match[3]) || pack.main || 'main'));
554-
}
555-
else {
556-
packageId = '';
552+
const matches = Object.keys((config && config.pkgs || {})).filter(pkg => (moduleId + '/').indexOf(pkg + '/') === 0).sort((a, b) => a.length > b.length ? -1 : 1);
553+
554+
if (matches.length) {
555+
packageId = matches.shift() as string;
556+
pack = config.pkgs![packageId];
557+
moduleId = packageId + '/' + (moduleIdInPackage = (moduleId.substr(packageId.length + 1) || pack.main || 'main'));
557558
}
558559

559560
let module = modules[moduleId];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>require.config Test</title>
6+
</head>
7+
8+
<body>
9+
<script src="../../../../src/loader.js"></script>
10+
<script>
11+
require.config({
12+
packages: [
13+
{
14+
name: '@test/common',
15+
location: '../../../common'
16+
}
17+
]
18+
});
19+
20+
require([
21+
'@test/common/app'
22+
], function (app) {
23+
window.loaderTestResults = app;
24+
});
25+
</script>
26+
</body>
27+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>require.config Test</title>
6+
</head>
7+
8+
<body>
9+
<script src="../../../../src/loader.js"></script>
10+
<script>
11+
require.config({
12+
packages: [
13+
{ name: '@common', location: '../../../common' },
14+
{ name: '@common/a', location: '../../../common/a', main: 'app' }
15+
]
16+
});
17+
18+
require([
19+
'@common/app',
20+
'@common/a'
21+
], function (app) {
22+
window.loaderTestResults = app;
23+
});
24+
</script>
25+
</body>
26+
</html>

tests/functional/require/require.ts

+12
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ registerSuite({
9797
return executeTest(this, './config/packages2.html', function (results: any) {
9898
assert.strictEqual(results, appMessage, '"app" module should load');
9999
});
100+
},
101+
102+
'package name with slashes'(this: any) {
103+
return executeTest(this, './config/packages3.html', function (results: any) {
104+
assert.strictEqual(results, appMessage, '"app" module should load');
105+
});
106+
},
107+
108+
'nested packages'(this: any) {
109+
return executeTest(this, './config/packages4.html', function (results: any) {
110+
assert.strictEqual(results, appMessage, '"app" module should load');
111+
});
100112
}
101113
},
102114

tests/unit/require.ts

+42
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,48 @@ registerSuite({
505505
], dfd.callback(function (app: any) {
506506
assert.strictEqual(app, 'app', '"app" module should load');
507507
}));
508+
},
509+
'slashes in package name'(this: any) {
510+
let dfd = this.async(DEFAULT_TIMEOUT);
511+
512+
setErrorHandler(dfd);
513+
514+
global.require.config({
515+
packages: [
516+
{
517+
name: '@test/common',
518+
location: './_build/tests/common',
519+
main: 'app'
520+
}
521+
]
522+
});
523+
524+
global.require([
525+
'@test/common'
526+
], dfd.callback(function (app: any) {
527+
assert.strictEqual(app, 'app', '"app" module should load');
528+
}));
529+
},
530+
'single @ package'(this: any) {
531+
let dfd = this.async(DEFAULT_TIMEOUT);
532+
533+
setErrorHandler(dfd);
534+
535+
global.require.config({
536+
packages: [
537+
{
538+
name: '@test',
539+
location: './_build/tests',
540+
main: 'app'
541+
}
542+
]
543+
});
544+
545+
global.require([
546+
'@test/common/app'
547+
], dfd.callback(function (app: any) {
548+
assert.strictEqual(app, 'app', '"app" module should load');
549+
}));
508550
}
509551
},
510552

0 commit comments

Comments
 (0)