forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexHtmlTranslator.js
143 lines (135 loc) · 4 KB
/
indexHtmlTranslator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module.exports = {
translate: translate
};
var _rxRules = {
basehref: {
from: /<base href=".*"[/]?>/,
to: '<script>document.write(\'<base href="\' + document.location + \'" />\');</script>'
},
script: {
from: /<script.*".*%tag%".*>.*<\/script>/,
to: '<script src="%tag%"></script>'
},
link: {
from: '/<link rel="stylesheet" href=".*%tag%".*>/',
to: '<link rel="stylesheet" href="%tag%">'
},
config: {
from: /\s*System.config\(\{[\s\S]*\}\);/m,
to: "\n" +
" System.config({\n" +
" transpiler: 'typescript', \n" +
" typescriptOptions: { emitDecoratorMetadata: true }, \n" +
" packages: {'app': {defaultExtension: 'ts'}} \n" +
" });"
},
};
var _rxData = [
{
pattern: 'basehref',
},
{
pattern: 'script',
from: 'node_modules/systemjs/dist/system.src.js',
//to: ['https://code.angularjs.org/tools/system.js', 'https://code.angularjs.org/tools/typescript.js']
to: ['https://code.angularjs.org/tools/system.js', 'https://npmcdn.com/[email protected]/lib/typescript.js']
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/angular2.dev.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/angular2.dev.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/angular2-all.umd.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/angular2-all.umd.dev.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/angular2-polyfills.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/angular2-polyfills.js'
},
{
pattern: 'script',
from: 'node_modules/rxjs/bundles/Rx.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/Rx.js'
},
{
pattern: 'script',
from: 'node_modules/rxjs/bundles/Rx.umd.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/Rx.umd.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/router.dev.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/router.dev.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/http.dev.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/http.dev.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/bundles/testing.dev.js',
to: 'https://code.angularjs.org/2.0.0-beta.8/testing.dev.js'
},
{
pattern: 'script',
from: 'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js',
to: 'https://npmcdn.com/[email protected]/es6/dev/src/testing/shims_for_IE.js'
},
{
pattern: 'script',
from: 'node_modules/es6-shim/es6-shim.min.js',
to: 'https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js'
},
{
pattern: 'script',
from: 'node_modules/systemjs/dist/system-polyfills.js',
to: 'https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js'
},
{
pattern: 'script',
from: 'node_modules/a2-in-memory-web-api/web-api.js',
to: 'https://npmcdn.com/a2-in-memory-web-api/web-api.js'
},
{
pattern: 'link',
from: 'node_modules/bootstrap/dist/css/bootstrap.min.css',
to: 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css'
},
{
pattern: 'config',
}
];
function translate(html) {
_rxData.forEach(function(rxDatum) {
var rxRule = _rxRules[rxDatum.pattern];
// rxFrom is a rexexp
var rxFrom = rxRule.from;
if (rxDatum.from) {
var from = rxDatum.from.replace('/', '\/');
var rxTemp = rxFrom.toString();
rxTemp = rxTemp.replace('%tag%', from);
rxFrom = rxFromString(rxTemp);
}
// rxTo is a string
var rxTo = rxRule.to;
if (rxDatum.to) {
var to = rxDatum.to;
to = Array.isArray(to) ? to : [to];
to = to.map(function (toItem) {
return rxTo.replace("%tag%", toItem);
});
rxTo = to.join("\n ");
}
html = html.replace(rxFrom, rxTo );
});
return html;
}
function rxFromString(rxString) {
var rx = /^\/(.*)\/(.*)/;
var pieces = rx.exec(rxString);
return RegExp(pieces[1], pieces[2]);
}