-
Notifications
You must be signed in to change notification settings - Fork 11
/
plugin.es5.js
342 lines (279 loc) · 10.7 KB
/
plugin.es5.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = plugin;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Node.JS modules ignored in the resolveId method by default
* TODO use https://www.npmjs.com/package/builtin-modules
*/
var externalModules = ['assert', 'buffer', 'console', 'constants', 'crypto', 'domain', 'events', 'http', 'https', 'os', 'path', 'punycode', 'querystring', 'stream', 'string_decoder', 'timers', 'tty', 'url', 'util', 'vm', 'zlib'];
var defaultExtensions = ['.js', '.json'];
var RollupIncludePaths = function () {
/**
* Options:
*
* - paths
* An array of source paths in your project where the plugin should look for files
* Example: ['src/lib', 'src/foo']
*
* - include
* A map of module=>path/to/file.js with custom module paths. Used to override
* the search with a static path (like Browserify does with the "browser" config)
*
* - external
* An array of module names that should be excluded from the bundle
*
* - extensions
* An array of file extensions to look for in the project.
* Default: ['.js', '.json']
*
* @param {Object} options
*/
function RollupIncludePaths(options) {
_classCallCheck(this, RollupIncludePaths);
options = options || {};
// include paths
this.projectPaths = options.paths || [''];
this.cache = {};
if (options.include) {
this.copyStaticPathsToCache(options.include);
}
// external modules to ignore
this.externalModules = options.external || externalModules;
// file extensions
this.extensions = options.extensions || defaultExtensions;
var extensionMatchers = this.extensions.map(function (e) {
return e.replace('.', '\\.');
}).join('|');
this.HAS_EXTENSION = RegExp('(' + extensionMatchers + ')$');
}
/**
* Rollup plugin method. Implements the Module resolution for project
* files
*
* @param {string} file File path to search
* @param {string} [origin] Origin of the module request
*/
_createClass(RollupIncludePaths, [{
key: 'resolveId',
value: function resolveId(id, origin) {
origin = origin || false;
return this.resolveCachedPath(id, origin) || this.searchModule(id, origin);
}
/**
* Rollup plugin method. Modifies the "external" option
*
* If options.external is a function, follows the Rollup API by keeping the
* option as a function (https://github.com/rollup/rollup/wiki/JavaScript-API#external)
*
* If options.external is an array, append the Node.JS builtin modules to it.
*
* @param {Object} options
*/
}, {
key: 'options',
value: function options(_options) {
var _this = this;
if ('function' === typeof this.externalModules) {
_options.external = this.externalModules;
} else if (this.externalModules instanceof Array && this.externalModules.length) {
var external = _options.external;
if ('function' === typeof external) {
_options.external = function (id) {
return external(id) || _this.externalModules.indexOf(id) !== -1;
};
} else {
_options.external = (external && external instanceof Array ? external : []).concat(this.externalModules);
}
}
return _options;
}
/**
* Receives an object with { moduleName => fullPath }
*
* Used to override resolution process with static values, like the
* `browser` config in Browserify
*
* If the path has no file extension, ".js" is implied
*
* @param {Object} paths
*/
}, {
key: 'copyStaticPathsToCache',
value: function copyStaticPathsToCache(staticPaths) {
var cache = this.cache;
Object.keys(staticPaths).forEach(function (id) {
var modulePath = staticPaths[id];
cache[id] = resolveJsExtension(modulePath);
});
/**
* Add '.js' to the end of file path
* @param {string} file
* @return {string}
*/
function resolveJsExtension(file) {
if (/\.js$/.test(file) === false) {
file += '.js';
}
return file;
}
}
/**
* Return a path from cache
* @param {string} id
* @return {string|nulld}
*/
}, {
key: 'resolveCachedPath',
value: function resolveCachedPath(id, origin) {
var key = this.getCacheKey(id, origin);
if (key in this.cache) {
return this.cache[key];
}
return false;
}
}, {
key: 'getCacheKey',
value: function getCacheKey(id, origin) {
var isRelativePath = id.indexOf('.') === 0;
return isRelativePath ? origin + ':' + id : id;
}
/**
* @param {string} file File path to search
* @param {string} [origin] Origin of the module request
*/
}, {
key: 'searchModule',
value: function searchModule(file, origin) {
var newPath = this.searchRelativePath(file, origin) || this.searchProjectModule(file, origin);
if (newPath) {
// add result to cache
var cacheKey = this.getCacheKey(file, origin);
this.cache[cacheKey] = newPath;
return newPath;
}
// if no path was found, null must be returned to keep the
// plugin chain!
return null;
}
/**
* Sarch for a file in the defined include paths
*
* @param {string} file File path to search
* @param {string} [origin] Origin of the module request
* @return {string|null}
*/
}, {
key: 'searchProjectModule',
value: function searchProjectModule(file) {
var newPath = void 0;
var includePath = this.projectPaths;
var workingDir = process.cwd();
for (var i = 0, ii = includePath.length; i < ii; i++) {
newPath = this.resolvePath(_path2.default.resolve(workingDir, includePath[i], file));
if (newPath) return newPath;
// #1 - also check for 'path/to/file/index.js'
// #4 - also check for 'path/to/file/index.[extensions]'
newPath = this.resolvePath(_path2.default.resolve(workingDir, includePath[i], file, 'index'));
if (newPath) return newPath;
}
return null;
}
/**
* Sarch for a file relative to who required it
*
* @param {string} file File path to search
* @param {string} [origin] Origin of the module request
*/
}, {
key: 'searchRelativePath',
value: function searchRelativePath(file, origin) {
if (!origin) {
return null;
}
var basePath = _path2.default.dirname(origin);
return (
// common case
// require('./file.js') in 'path/origin.js'
// > path/file.js
this.resolvePath(_path2.default.join(basePath, file)) ||
// nodejs path case
// require('./subfolder') in 'lib/origin.js'
// > lib/subfolder/index.js
this.resolvePath(_path2.default.join(basePath, file, 'index'))
);
}
/**
* Resolve a given file path by checking if it exists. If it does not,
* also checks if the file exists by appending the extensions to it,
* i.e. checks for 'file', then 'file.js', 'file.json'
* and so on, until one is found.
*
* Returns false if "file" was not found
*
* @param {string} file
* @return {boolean}
*/
}, {
key: 'resolvePath',
value: function resolvePath(file) {
if (this.fileExists(file)) {
return file;
}
// check different file extensions
for (var i = 0, ii = this.extensions.length; i < ii; i++) {
var ext = this.extensions[i];
var newPath = file + ext;
if (this.fileExists(newPath)) {
return newPath;
}
}
return false;
}
/**
* Check if "file" has one of the extensions defined in the plugin options
* @param {string} file
* @return {boolean}
*/
}, {
key: 'hasExtension',
value: function hasExtension(file) {
return this.HAS_EXTENSION.test(file);
}
/**
* @param {string} file
* @return {boolean}
*/
}, {
key: 'fileExists',
value: function fileExists(file) {
try {
var stat = _fs2.default.statSync(file);
return stat.isFile();
} catch (e) {
return false;
}
}
}]);
return RollupIncludePaths;
}();
function plugin(options) {
var resolver = new RollupIncludePaths(options);
return {
resolveId: function resolveId(file, origin) {
return resolver.resolveId(file, origin);
},
options: function options(_options2) {
return resolver.options(_options2);
}
};
}
module.exports = exports.default;