UNPKG

2.34 kBJavaScriptView Raw
1
2
3const HELPERS = {
4 interopRequireWildcard: `
5 function interopRequireWildcard(obj) {
6 if (obj && obj.__esModule) {
7 return obj;
8 } else {
9 var newObj = {};
10 if (obj != null) {
11 for (var key in obj) {
12 if (Object.prototype.hasOwnProperty.call(obj, key)) {
13 newObj[key] = obj[key];
14 }
15 }
16 }
17 newObj.default = obj;
18 return newObj;
19 }
20 }
21 `,
22 interopRequireDefault: `
23 function interopRequireDefault(obj) {
24 return obj && obj.__esModule ? obj : { default: obj };
25 }
26 `,
27 createNamedExportFrom: `
28 function createNamedExportFrom(obj, localName, importedName) {
29 Object.defineProperty(exports, localName, {enumerable: true, get: () => obj[importedName]});
30 }
31 `,
32 // Note that TypeScript and Babel do this differently; TypeScript does a simple existence
33 // check in the exports object and does a plain assignment, whereas Babel uses
34 // defineProperty and builds an object of explicitly-exported names so that star exports can
35 // always take lower precedence. For now, we do the easier TypeScript thing.
36 createStarExport: `
37 function createStarExport(obj) {
38 Object.keys(obj)
39 .filter((key) => key !== "default" && key !== "__esModule")
40 .forEach((key) => {
41 if (exports.hasOwnProperty(key)) {
42 return;
43 }
44 Object.defineProperty(exports, key, {enumerable: true, get: () => obj[key]});
45 });
46 }
47 `,
48};
49
50export class HelperManager {
51 __init() {this.helperNames = {}}
52 constructor( nameManager) {;this.nameManager = nameManager;HelperManager.prototype.__init.call(this);}
53
54 getHelperName(baseName) {
55 let helperName = this.helperNames[baseName];
56 if (helperName) {
57 return helperName;
58 }
59 helperName = this.nameManager.claimFreeName(`_${baseName}`);
60 this.helperNames[baseName] = helperName;
61 return helperName;
62 }
63
64 emitHelpers() {
65 let resultCode = "";
66 for (const [baseName, helperCode] of Object.entries(HELPERS)) {
67 const helperName = this.helperNames[baseName];
68 if (helperName) {
69 resultCode += " ";
70 resultCode += helperCode
71 .replace(baseName, helperName)
72 .replace(/\s+/g, " ")
73 .trim();
74 }
75 }
76 return resultCode;
77 }
78}