UNPKG

1.59 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var pluginutils = require('@rollup/pluginutils');
5
6var validName = /^[a-zA-Z_$][a-zA-Z$_0-9]*$/;
7
8function legacy(options) {
9 var exports = {};
10 Object.keys(options).forEach(function (file) {
11 exports[pluginutils.normalizePath(path.resolve(file))] = options[file];
12 });
13
14 return {
15 name: 'legacy',
16
17 transform: function transform(content, id) {
18 var normalizedId = pluginutils.normalizePath(id);
19 if (normalizedId in exports) {
20 var code = content;
21 var value = exports[normalizedId];
22
23 if (typeof value === 'string') {
24 // default export
25 code += "\nexport default " + value + ";";
26 } else {
27 var statements = [];
28 var i = 1;
29
30 Object.entries(value).forEach(function (ref) {
31 var key = ref[0];
32 var name = ref[1];
33
34 if (name === key) {
35 statements.push(("export { " + key + " };"));
36 } else if (validName.test(name)) {
37 statements.push(("export { " + name + " as " + key + " };"));
38 } else {
39 statements.push(("var __export" + i + " = " + name + ";\nexport { __export" + i + " as " + key + " };"));
40 i += 1;
41 }
42 });
43
44 code += "\n" + (statements.join('\n'));
45 }
46
47 // TODO need a way to say 'sourcemap hasn't changed
48 return {
49 code: code,
50 map: { mappings: '' }
51 };
52 }
53 return null;
54 }
55 };
56}
57
58module.exports = legacy;
59//# sourceMappingURL=index.js.map