forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.js
199 lines (169 loc) · 4.31 KB
/
container.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
/* jshint node:true, undef:true, unused:true */
var assert = require('assert');
var Rewriter = require('./rewriter');
var Writer = require('./writer');
/** @typedef {{resolveModule: function(string, Module, Container): Module}} */
var Resolver;
/**
* Represents a container of modules for the given options.
*
* @constructor
* @param {{resolvers: Resolver[], formatter: Formatter}} options
*/
function Container(options) {
var formatter = options && options.formatter;
if (typeof formatter === 'function') {
formatter = new formatter();
}
assert.ok(
formatter,
'missing required option `formatter`'
);
var resolvers = options && options.resolvers;
assert.ok(
resolvers && resolvers.length > 0,
'at least one resolver is required'
);
resolvers.forEach(function(resolver) {
assert.equal(
typeof resolver.resolveModule, 'function',
'`resolver` must have `resolveModule` function: ' + resolver
);
});
Object.defineProperties(this, {
modules: {
value: Object.create(null)
},
formatter: {
value: formatter
},
resolvers: {
value: resolvers
},
options: {
value: options
}
});
}
/**
* Gets a module by resolving `path`. If `path` is resolved to the same path
* as a previous call, the same object will be returned.
*
* @param {string} importedPath
* @param {Module=} fromModule
* @return {Module}
*/
Container.prototype.getModule = function(importedPath, fromModule) {
for (var i = 0, length = this.resolvers.length; i < length; i++) {
var resolvedModule = this.resolvers[i].resolveModule(
importedPath,
fromModule,
this
);
if (resolvedModule) {
this.addModule(resolvedModule);
return resolvedModule;
}
}
throw new Error(
'missing module import' +
(fromModule ? ' from ' + fromModule.relativePath : '') +
' for path: ' + importedPath
);
};
/**
* Adds a module to the internal cache and gives it a unique name.
*
* @private
* @param {Module} mod
*/
Container.prototype.addModule = function(mod) {
if (mod.path in this.modules) {
return;
}
// We have not seen this module before, so let's give it a unique name.
var modules = this.getModules();
var name = mod.name;
var baseName = name;
var counter = 0;
var nameExists;
while (true) {
nameExists = modules.some(function(existingModule) {
return existingModule.name === name;
});
if (!nameExists) {
break;
} else {
counter++;
name = baseName + counter;
}
}
mod.name = name;
delete mod.id;
this.modules[mod.path] = mod;
};
/**
* Get a cached module by a resolved path.
*
* @param {string} resolvedPath
* @return {?Module}
*/
Container.prototype.getCachedModule = function(resolvedPath) {
return this.modules[resolvedPath];
};
/**
* Writes the contents of this container to the given path.
*
* @param {string} target
*/
Container.prototype.write = function(target) {
var files = this.convert();
var writer = new Writer(target);
writer.write(files);
};
/**
* Converts the contents of this container using the current formatter.
*
* @return {File[]}
*/
Container.prototype.convert = function() {
if (this.formatter.beforeConvert) {
this.formatter.beforeConvert(this);
}
var modules = this.getModules();
var rewriter = new Rewriter(this.formatter);
rewriter.rewrite(modules);
var formatter = this.formatter;
return formatter.build(modules);
};
/**
* Follows all imports/exports looking for new modules to add to this container.
*/
Container.prototype.findImportedModules = function() {
var knownModules;
var lastModuleCount = 0;
while ((knownModules = this.getModules()).length !== lastModuleCount) {
lastModuleCount = knownModules.length;
for (var i = 0; i < lastModuleCount; i++) {
// Force loading of imported modules.
noop(knownModules[i].imports.modules);
}
}
};
/**
* Gets the modules in this container in no particular order.
*
* @return {Module[]}
*/
Container.prototype.getModules = function() {
var modules = this.modules;
return Object.keys(modules).map(function(key) {
return modules[key];
});
};
/**
* Does nothing. This is only here to make JSHint/other static analysis
* tools happy about using getters for side effects.
*/
function noop() {}
module.exports = Container;