Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Corrected behavior with es exports
Browse files Browse the repository at this point in the history
commonjs requires should still be processed in es modules,
but the module should remain with its default exports (+1 squashed commits)
  • Loading branch information
danielgindi committed Feb 27, 2019
1 parent 0c8dea1 commit f40ad73
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,17 @@ export default function commonjs(options = {}) {
normalizePathSlashes(id)
);

let avoidAddingDefaultExport = false;

if (isEsModule && !isDynamicRequireModule) {
(hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport)[
id
] = true;
return null;
}

avoidAddingDefaultExport = true;
}
// it is not an ES module but it does not have CJS-specific elements.
if (!hasCjsKeywords(code, ignoreGlobal)) {
else if (!hasCjsKeywords(code, ignoreGlobal)) {
esModulesWithoutDefaultExport[id] = true;
return null;
}
Expand All @@ -232,10 +234,13 @@ export default function commonjs(options = {}) {
customNamedExports[id],
sourceMap,
dynamicRequireModuleSet,
ast
ast,
avoidAddingDefaultExport
);

if (!transformed) {
esModulesWithoutDefaultExport[id] = true;
if (!isEsModule || isDynamicRequireModule)
esModulesWithoutDefaultExport[id] = true;
return null;
}

Expand Down
42 changes: 24 additions & 18 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export function transformCommonjs(
customNamedExports,
sourceMap,
dynamicRequireModuleSet,
astCache
astCache,
avoidAddingDefaultExport
) {
const ast = astCache || tryParse(parse, code, id);

Expand Down Expand Up @@ -537,36 +538,41 @@ export function transformCommonjs(
}
});

if (!hasDefaultExport) {
if (!hasDefaultExport && !avoidAddingDefaultExport) {
wrapperEnd = `\n\nvar ${moduleName} = {\n${names
.map(({ name, deconflicted }) => `\t${name}: ${deconflicted}`)
.join(',\n')}\n};`;
}
}

Object.keys(namedExports)
.filter(key => !blacklist[key])
.forEach(addExport);

const defaultExport = /__esModule/.test(code)
? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});`
: `export default ${moduleName};`;

const named = namedExportDeclarations
.filter(x => x.name !== 'default' || !hasDefaultExport)
.map(x => x.str);

const exportBlock =
'\n\n' +
[defaultExport]
.concat(named)
.concat(hasDefaultExport ? defaultExportPropertyAssignments : [])
.join('\n');

magicString
.trim()
.prepend(importBlock + wrapperStart)
.trim()
.append(wrapperEnd + exportBlock);
.append(wrapperEnd);

if (!avoidAddingDefaultExport) {
const defaultExport = /__esModule/.test(code)
? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});`
: `export default ${moduleName};`;

const named = namedExportDeclarations
.filter(x => x.name !== 'default' || !hasDefaultExport)
.map(x => x.str);

const exportBlock =
'\n\n' +
[defaultExport]
.concat(named)
.concat(hasDefaultExport ? defaultExportPropertyAssignments : [])
.join('\n');

magicString.append(exportBlock);
}

code = magicString.toString();
const map = sourceMap ? magicString.generateMap() : null;
Expand Down
3 changes: 0 additions & 3 deletions test/form/unambiguous-with-default-export/input.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/form/unambiguous-with-default-export/output.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/form/unambiguous-with-import/input.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/form/unambiguous-with-import/output.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/form/unambiguous-with-named-export/input.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/form/unambiguous-with-named-export/output.js

This file was deleted.

0 comments on commit f40ad73

Please sign in to comment.