-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-transpilation-registry.js
212 lines (186 loc) · 6.09 KB
/
package-transpilation-registry.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
// This file is required by compile-cache, which is required directly from
// apm, so it can only use the subset of newer JavaScript features that apm's
// version of Node supports. Strict mode is required for block scoped declarations.
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const minimatch = require('minimatch');
let Resolve = null;
class PackageTranspilationRegistry {
constructor() {
this.configByPackagePath = {};
this.specByFilePath = {};
this.transpilerPaths = {};
}
addTranspilerConfigForPath(packagePath, packageName, packageMeta, config) {
this.configByPackagePath[packagePath] = {
name: packageName,
meta: packageMeta,
path: packagePath,
specs: config.map(spec => Object.assign({}, spec))
};
}
removeTranspilerConfigForPath(packagePath) {
delete this.configByPackagePath[packagePath];
const packagePathWithSep = packagePath.endsWith(path.sep)
? path.join(packagePath)
: path.join(packagePath) + path.sep;
Object.keys(this.specByFilePath).forEach(filePath => {
if (path.join(filePath).startsWith(packagePathWithSep)) {
delete this.specByFilePath[filePath];
}
});
}
// Wraps the transpiler in an object with the same interface
// that falls back to the original transpiler implementation if and
// only if a package hasn't registered its desire to transpile its own source.
wrapTranspiler(transpiler) {
return {
getCachePath: (sourceCode, filePath) => {
const spec = this.getPackageTranspilerSpecForFilePath(filePath);
if (spec) {
return this.getCachePath(sourceCode, filePath, spec);
}
return transpiler.getCachePath(sourceCode, filePath);
},
compile: (sourceCode, filePath) => {
const spec = this.getPackageTranspilerSpecForFilePath(filePath);
if (spec) {
return this.transpileWithPackageTranspiler(
sourceCode,
filePath,
spec
);
}
return transpiler.compile(sourceCode, filePath);
},
shouldCompile: (sourceCode, filePath) => {
if (this.transpilerPaths[filePath]) {
return false;
}
const spec = this.getPackageTranspilerSpecForFilePath(filePath);
if (spec) {
return true;
}
return transpiler.shouldCompile(sourceCode, filePath);
}
};
}
getPackageTranspilerSpecForFilePath(filePath) {
if (this.specByFilePath[filePath] !== undefined)
return this.specByFilePath[filePath];
let thisPath = filePath;
let lastPath = null;
// Iterate parents from the file path to the root, checking at each level
// to see if a package manages transpilation for that directory.
// This means searching for a config for `/path/to/file/here.js` only
// only iterates four times, even if there are hundreds of configs registered.
while (thisPath !== lastPath) {
// until we reach the root
let config = this.configByPackagePath[thisPath];
if (config) {
const relativePath = path.relative(thisPath, filePath);
if (
relativePath.startsWith(`node_modules${path.sep}`) ||
relativePath.indexOf(`${path.sep}node_modules${path.sep}`) > -1
) {
return false;
}
for (let i = 0; i < config.specs.length; i++) {
const spec = config.specs[i];
if (minimatch(filePath, path.join(config.path, spec.glob))) {
spec._config = config;
this.specByFilePath[filePath] = spec;
return spec;
}
}
}
lastPath = thisPath;
thisPath = path.join(thisPath, '..');
}
this.specByFilePath[filePath] = null;
return null;
}
getCachePath(sourceCode, filePath, spec) {
const transpilerPath = this.getTranspilerPath(spec);
const transpilerSource =
spec._transpilerSource || fs.readFileSync(transpilerPath, 'utf8');
spec._transpilerSource = transpilerSource;
const transpiler = this.getTranspiler(spec);
let hash = crypto
.createHash('sha1')
.update(JSON.stringify(spec.options || {}))
.update(transpilerSource, 'utf8')
.update(sourceCode, 'utf8');
if (transpiler && transpiler.getCacheKeyData) {
const meta = this.getMetadata(spec);
const additionalCacheData = transpiler.getCacheKeyData(
sourceCode,
filePath,
spec.options || {},
meta
);
hash.update(additionalCacheData, 'utf8');
}
return path.join(
'package-transpile',
spec._config.name,
hash.digest('hex')
);
}
transpileWithPackageTranspiler(sourceCode, filePath, spec) {
const transpiler = this.getTranspiler(spec);
if (transpiler) {
const meta = this.getMetadata(spec);
const result = transpiler.transpile(
sourceCode,
filePath,
spec.options || {},
meta
);
if (result === undefined || (result && result.code === undefined)) {
return sourceCode;
} else if (result.code) {
return result.code.toString();
} else {
throw new Error(
'Could not find a property `.code` on the transpilation results of ' +
filePath
);
}
} else {
const err = new Error(
"Could not resolve transpiler '" +
spec.transpiler +
"' from '" +
spec._config.path +
"'"
);
throw err;
}
}
getMetadata(spec) {
return {
name: spec._config.name,
path: spec._config.path,
meta: spec._config.meta
};
}
getTranspilerPath(spec) {
Resolve = Resolve || require('resolve');
return Resolve.sync(spec.transpiler, {
basedir: spec._config.path,
extensions: Object.keys(require.extensions)
});
}
getTranspiler(spec) {
const transpilerPath = this.getTranspilerPath(spec);
if (transpilerPath) {
const transpiler = require(transpilerPath);
this.transpilerPaths[transpilerPath] = true;
return transpiler;
}
}
}
module.exports = PackageTranspilationRegistry;