Skip to content

Commit

Permalink
Fix benign helper typo, introduce helper for named export (#418)
Browse files Browse the repository at this point in the history
I forgot the named export from case, which also will need to change with
upcoming strategy changes to how imports work.
  • Loading branch information
alangpierce authored Feb 18, 2019
1 parent cecfdee commit 8ea989a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/CJSImportProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class CJSImportProcessor {

private interopRequireWildcardName: string;
private interopRequireDefaultName: string;
private createNamedExportFromName: string;
private createStarExportName: string;

constructor(
Expand All @@ -56,10 +57,11 @@ export default class CJSImportProcessor {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key))
if (Object.prototype.hasOwnProperty.call(obj, key)) {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
return newObj;
}
Expand All @@ -71,6 +73,12 @@ export default class CJSImportProcessor {
return obj && obj.__esModule ? obj : { default: obj };
}`.replace(/\s+/g, " ");
}
if (this.createNamedExportFromName) {
prefix += `
function ${this.createNamedExportFromName}(obj, localName, importedName) {
Object.defineProperty(exports, localName, {enumerable: true, get: () => obj[importedName]});
}`.replace(/\s+/g, " ");
}
if (this.createStarExportName) {
// Note that TypeScript and Babel do this differently; TypeScript does a simple existence
// check in the exports object and does a plain assignment, whereas Babel uses
Expand Down Expand Up @@ -105,6 +113,13 @@ export default class CJSImportProcessor {
return this.interopRequireDefaultName;
}

getCreateNamedExportFromName(): string {
if (!this.createNamedExportFromName) {
this.createNamedExportFromName = this.nameManager.claimFreeName("_createNamedExportFrom");
}
return this.createNamedExportFromName;
}

getCreateStarExportName(): string {
if (!this.createStarExportName) {
this.createStarExportName = this.nameManager.claimFreeName("_createStarExport");
Expand Down Expand Up @@ -203,8 +218,7 @@ export default class CJSImportProcessor {
}

for (const {importedName, localName} of namedExports) {
requireCode += ` Object.defineProperty(exports, '${localName}', \
{enumerable: true, get: () => ${primaryImportName}.${importedName}});`;
requireCode += ` ${this.getCreateNamedExportFromName()}(${primaryImportName}, '${localName}', '${importedName}');`;
}
for (const exportStarName of exportStarNames) {
requireCode += ` exports.${exportStarName} = ${secondaryImportName};`;
Expand Down
9 changes: 5 additions & 4 deletions test/imports-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CREATE_NAMED_EXPORT_FROM_PREFIX,
CREATE_STAR_EXPORT_PREFIX,
ESMODULE_PREFIX,
IMPORT_DEFAULT_PREFIX,
Expand Down Expand Up @@ -253,7 +254,7 @@ exports.a = a; exports.b = b;
`"use strict"; function _interopRequireWildcard2(obj) { \
if (obj && obj.__esModule) { return obj; } else { var newObj = {}; \
if (obj != null) { for (var key in obj) { \
if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } \
if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } \
newObj.default = obj; return newObj; } } function _interopRequireDefault3(obj) { \
return obj && obj.__esModule ? obj : { default: obj }; }
var _foo = require('foo'); var foo = _interopRequireWildcard2(_foo);
Expand Down Expand Up @@ -793,9 +794,9 @@ module.exports = exports.default;
export {x} from './MyVars';
export {a as b, c as d} from './MyOtherVars';
`,
`"use strict";${ESMODULE_PREFIX}
var _MyVars = require('./MyVars'); Object.defineProperty(exports, 'x', {enumerable: true, get: () => _MyVars.x});
var _MyOtherVars = require('./MyOtherVars'); Object.defineProperty(exports, 'b', {enumerable: true, get: () => _MyOtherVars.a}); Object.defineProperty(exports, 'd', {enumerable: true, get: () => _MyOtherVars.c});
`"use strict";${CREATE_NAMED_EXPORT_FROM_PREFIX}${ESMODULE_PREFIX}
var _MyVars = require('./MyVars'); _createNamedExportFrom(_MyVars, 'x', 'x');
var _MyOtherVars = require('./MyOtherVars'); _createNamedExportFrom(_MyOtherVars, 'b', 'a'); _createNamedExportFrom(_MyOtherVars, 'd', 'c');
`,
);
});
Expand Down
5 changes: 4 additions & 1 deletion test/prefixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ return obj && obj.__esModule ? obj : { default: obj }; }`;
export const IMPORT_WILDCARD_PREFIX = ` function _interopRequireWildcard(obj) { \
if (obj && obj.__esModule) { return obj; } else { var newObj = {}; \
if (obj != null) { for (var key in obj) { \
if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } \
if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } \
newObj.default = obj; return newObj; } }`;
export const CREATE_NAMED_EXPORT_FROM_PREFIX = ` function _createNamedExportFrom(obj, \
localName, importedName) { Object.defineProperty(exports, localName, \
{enumerable: true, get: () => obj[importedName]}); }`;
export const CREATE_STAR_EXPORT_PREFIX = ` function _createStarExport(obj) { \
Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") \
.forEach((key) => { if (exports.hasOwnProperty(key)) { return; } \
Expand Down

0 comments on commit 8ea989a

Please sign in to comment.