Skip to content

Commit 5eed6be

Browse files
Update comments for Exports Transform (#9)
* Additional comments for export postCompilation lifecycle * console.log left over
1 parent e13362d commit 5eed6be

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/transformers/exports.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default class ExportTransform extends Transform implements TransformInter
8989
if (defaultDeclarationValue !== null) {
9090
this.exported = { ...this.exported, ...defaultDeclarationValue };
9191
}
92-
console.log(this.exported);
9392
break;
9493
case EXPORT_ALL_DECLARATION:
9594
// TODO(KB): This case `export * from "./import"` is not currently supported.
@@ -160,8 +159,9 @@ export default class ExportTransform extends Transform implements TransformInter
160159
const namedClassMatch = new RegExp(`window.${key}=(\\w+);`).exec(code);
161160
if (namedClassMatch && namedClassMatch.length > 0) {
162161
// Remove the declaration on window scope, i.e. `window.Exported=a;`
163-
// Replace it with an export statement `export {a as Exported};`
164-
code = code.replace(namedClassMatch[0], `export {${namedClassMatch[1]} as ${key}};`);
162+
code = code.replace(namedClassMatch[0], '');
163+
// Store a new export constant to output at the end. `a as Exported`
164+
exportedConstants.push(`${namedClassMatch[1]} as ${key}`);
165165
}
166166
break;
167167
case ExportClosureMapping.NAMED_DEFAULT_FUNCTION:
@@ -170,7 +170,7 @@ export default class ExportTransform extends Transform implements TransformInter
170170
case ExportClosureMapping.NAMED_DEFAULT_CLASS:
171171
const namedDefaultClassMatch = new RegExp(`window.${key}=(\\w+);`).exec(code);
172172
if (namedDefaultClassMatch && namedDefaultClassMatch.length > 0) {
173-
// Remove the declaration on window scope, i.e. `window.Exported=a;`
173+
// Remove the declaration on window scope, i.e. `window.ExportedTwo=a;`
174174
// Replace it with an export statement `export default a;`
175175
code = code.replace(
176176
namedDefaultClassMatch[0],
@@ -179,8 +179,11 @@ export default class ExportTransform extends Transform implements TransformInter
179179
}
180180
break;
181181
case ExportClosureMapping.NAMED_CONSTANT:
182-
exportedConstants.push(key);
182+
// Remove the declaration on the window scope, i.e. `window.ExportedThree=value`
183+
// Replace it with a const declaration, i.e `const ExportedThree=value`
183184
code = code.replace(`window.${key}=`, `const ${key}=`);
185+
// Store a new export constant to output at the end, i.e `ExportedThree`
186+
exportedConstants.push(key);
184187
break;
185188
default:
186189
this.context.warn(
@@ -191,9 +194,11 @@ export default class ExportTransform extends Transform implements TransformInter
191194
});
192195

193196
if (exportedConstants.length > 0) {
197+
// Remove the newline at the end since we are going to append exports.
194198
if (code.endsWith('\n')) {
195199
code = code.substr(0, code.lastIndexOf('\n'));
196200
}
201+
// Append the exports that were gathered, i.e `export {a as Exported, ExportedThree};`
197202
code += `export {${exportedConstants.join(',')}};`;
198203
}
199204
}

0 commit comments

Comments
 (0)