-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
build-traceviewer-module.js
162 lines (134 loc) · 5.09 KB
/
build-traceviewer-module.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @license
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Usage:
* node scripts/build-traceviewer-module.js
*/
'use strict';
const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const mkdirp = require('mkdirp');
const babel = require('babel-core');
const paths = {};
const INITIAL_IMPORT = 'scripts/traceviewer-module-index';
function convertImport(src) {
console.log('Reading:', src);
const html = fs.readFileSync(src);
let dest = src.replace(/\.html$/, '.js');
dest = dest.replace(INITIAL_IMPORT, 'index');
jsdom.env({
html: html,
done: function(err, window) {
if (err) {
throw err;
}
const imports = window.document.querySelectorAll('link[rel="import"]');
const scripts = window.document.querySelectorAll('script');
let scriptsContent = '';
scriptsContent += addUseStrictDirective();
scriptsContent += convertLicenseComments(html);
// traverse and rewrite the imports
for (var i = 0; i < imports.length; i++) {
const importPath = importToPath(imports[i]);
scriptsContent += importToRequire(importPath, dest);
// Recursively process each import.
if (paths[importPath]) {
continue;
}
paths[importPath] = true;
convertImport(importPath);
}
// adjust the javascript
for (let s = 0; s < scripts.length; s++) {
scriptsContent += rewriteGlobals(scripts[s]);
}
// node4 compat
scriptsContent = polyfillNode4Support(dest, scriptsContent);
// browser compat
scriptsContent = adjustForBrowserCompat(scriptsContent);
writeNewFile(dest, scriptsContent);
function convertLicenseComments(html) {
const license = /<!--(.*\n)+-->/im;
const licenseContent = license.exec(html);
if (licenseContent) {
return licenseContent[0]
.replace(/<!--/g, '/**')
.replace(/-->/g, '**/\n\n');
}
return '';
}
function importToPath(importElem) {
let importPath = importElem.getAttribute('href');
importPath = importPath.replace(/^\//, './third_party/src/catapult/tracing/');
return importPath;
}
function importToRequire(importPath, dest) {
const from = path.dirname(dest);
const to = importPath.replace(/html$/, 'js');
let relativePath = path.relative(from, to);
if (relativePath[0] !== '.') {
relativePath = './' + relativePath;
}
relativePath = relativePath.replace('./third_party/src/catapult/tracing/tracing', '.');
return 'require("' + relativePath + '");\n';
}
function rewriteGlobals(script) {
script = script.textContent;
script = script.replace(/tr\.exportTo/, 'global.tr.exportTo');
script = script.replace(/var global = this;/, '');
script = script.replace(/this.tr =/, 'global.tr =');
script = script.replace(/\(function\(global\)\s?\{/, '(function() {');
return script;
}
// the "use strict" must be found *above* the require() statements
function addUseStrictDirective() {
return '"use strict";\n';
}
// adjust and transpile for usage in node 4+
function polyfillNode4Support(dest, scriptsContent) {
let transformed = scriptsContent;
if (dest.endsWith('/slice.js')) {
// no babel plugin that can transpile `new.target`, so this is done manually
transformed = transformed.replace('if (new.target)', 'if (!(this instanceof Slice))');
}
transformed = babel.transform(transformed, {
plugins: ['transform-es2015-destructuring']
}).code;
return transformed;
}
function adjustForBrowserCompat(scriptsContent) {
return scriptsContent
// early exit in tracing/base.ui and avoid currentScript from breaking us.
.replace('var THIS_DOC = document.currentScript.ownerDocument;', 'return;');
}
function writeNewFile(dest, scriptsContent) {
dest = dest.replace('./third_party/src/catapult/tracing/tracing/', '');
dest = path.resolve('./third_party/traceviewer-js/' + dest);
const destFolder = path.dirname(dest);
mkdirp(destFolder, function(err) {
if (err) {
throw new Error(`Failed to create folder: ${destFolder}`);
}
console.log('Writing:', dest);
fs.writeFile(dest, scriptsContent, 'utf8');
});
}
}
});
}
convertImport('./' + INITIAL_IMPORT + '.html');