Skip to content

Commit d138d10

Browse files
committed
Fix SystemJS pathing issues
1 parent a949034 commit d138d10

7 files changed

+71
-20
lines changed

intern.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
{
2+
"defaultTimeout": "5000",
23
"browser": {
34
"loader": {
4-
"script": "systemjs",
5-
"options": {
6-
"baseURL": "/dist"
7-
}
5+
"script": "systemjs"
86
},
97
"plugins": [
10-
"intern-angular-shim.js"
8+
"dist/intern-angular-shim.js"
119
],
1210
"suites": [
1311
"app/app.component.spec",

package-lock.json

+9-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"chokidar-cli": "^1.2.0",
4343
"concurrently": "^3.5.0",
4444
"cpx": "^1.5.0",
45-
"intern": "next",
45+
"intern": "^4.0.0-alpha.9",
4646
"lite-server": "^2.3.0",
4747
"rimraf": "^2.6.1",
4848
"sinon": "^2.3.7",

src/app/components/about.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component } from '@angular/core';
22

33
@Component({
4+
selector: 'app-about',
45
template: `
56
<h2 highlight="skyblue">About</h2>
67
<twain-quote></twain-quote>

src/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<title>Angular Intern Example</title>
66

7-
<base href="/"/>
7+
<base href=".">
88
<link rel="stylesheet" href="styles.css">
99

1010
<script src="../node_modules/core-js/client/shim.min.js"></script>
@@ -15,7 +15,7 @@
1515
<body>
1616
<my-app>Loading...</my-app>
1717
<script>
18-
System.import('./main.js').catch(function (err) {
18+
System.import('main').catch(function (err) {
1919
console.log(err);
2020
});
2121
</script>

src/systemjs-angular-loader.ts

+42-8
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,59 @@ const templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
22
const stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
33
const stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
44

5+
function commonPath(path1: string[], path2: string[]) {
6+
const length = Math.min(path1.length, path2.length);
7+
8+
let position: number;
9+
10+
for (position = 0; position < length; position++) {
11+
if (path1[position] !== path2[position]) {
12+
position--;
13+
break;
14+
}
15+
}
16+
17+
return path1.slice(0, position + 1);
18+
}
19+
20+
function relative(from: string[], to: string[]) {
21+
const common = commonPath(from, to);
22+
23+
to = to.slice(common.length);
24+
if (from.length === common.length) {
25+
return to;
26+
}
27+
28+
return [...from.slice(common.length).map(_ => '..'), ...to];
29+
}
30+
31+
const rootUrlParts = (() => {
32+
const url = document.createElement('a');
33+
url.href = document.baseURI;
34+
const parts = url.pathname.split('/');
35+
parts.pop();
36+
return parts;
37+
})();
38+
539
export function translate(load: { source: string; address: string; }) {
640
if (load.source.indexOf('moduleId') !== -1) {
741
return load;
842
}
943

1044
const url = document.createElement('a');
1145
url.href = load.address;
12-
13-
const basePathParts = url.pathname.split('/');
14-
15-
basePathParts.pop();
16-
let basePath = basePathParts.join('/');
46+
const urlParts = url.pathname.split('/');
47+
urlParts.pop();
1748

1849
const baseHref = document.createElement('a');
1950
baseHref.href = this.baseURL;
51+
const baseHrefParts = baseHref.pathname.split('/');
2052

21-
if (!baseHref.pathname.startsWith('/dist/')) {
22-
basePath = basePath.replace(baseHref.pathname, '');
23-
}
53+
const relPath = relative(rootUrlParts, baseHrefParts).join('/');
54+
55+
let basePath = urlParts.join('/');
56+
basePath = basePath.replace(baseHref.pathname, '');
57+
basePath = `${relPath}${basePath}`;
2458

2559
load.source = load.source
2660
.replace(templateUrlRegex, function(_, __, resolvedUrl){

src/systemjs.config.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
SystemJS.config({
2+
baseURL: '/dist',
23
paths: {
3-
'npm:': `${SystemJS.baseURL}../node_modules/`
4+
'npm:': `/node_modules/`
45
},
56

67
map: {
8+
'dist': '.',
9+
'main': 'main',
710
'app': 'app',
811
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
912
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
@@ -32,6 +35,15 @@ SystemJS.config({
3235
}
3336
},
3437

38+
dist: {
39+
defaultExtension: 'js',
40+
meta: {
41+
'./app/*.js': {
42+
loader: 'systemjs-angular-loader.js'
43+
}
44+
}
45+
},
46+
3547
testing: {
3648
defaultExtension: 'js'
3749
},

0 commit comments

Comments
 (0)