-
-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commonjs)!: reconstruct real es module from __esModule marker (#537
) BREAKING CHANGES: #537 (comment) * feat(commonjs): reconstruct real es module from __esModule marker * fix(commonjs): handle module.exports reassignment * fix(commonjs): preserve namespace default export fallback * fix(commonjs): mark redefined module.exports as CJS * chore(commonjs): fix tests * Make tests actually throw when there is an error in the code and skip broken test * chore(commonjs): Improve how AST branche are skipped * fix(commonjs): Fix Rollup peer dependency version check * refactor(commonjs): Use a switch statement for top level analysis * refactor(commonjs): Restructure transform slightly * refactor(commonjs): Extract helpers * refactor(commonjs): Extract helpers * refactor(commonjs): Move __esModule detection logic entirely within CJS transformer * refactor(commonjs): Add __moduleExports back to compiled modules * refactor(commonjs): Move more parts into switch statement * refactor(commonjs): Completely refactor require handling Finally remove second AST walk * fix(commonjs): Handle nested and multiple __esModule definitions * fix(commonjs): Handle shadowed imports for multiple requires * fix(commonjs): Handle double assignments to exports * chore(commonjs): Further cleanup * refactor(commonjs): extract logic to rewrite imports * feat(commonjs): only add interop if necessary * refactor(commonjs): Do not add require helper unless used * refactor(commonjs): Inline dynamic require handling into loop * refactor(commonjs): Extract import logic * refactor(commonjs): Extract export generation * refactor(commonjs): Avoid empty lines before wrapped code * Do not remove leading comments in files * refactor(commonjs): Remove unused code * fix(commonjs): Improve error message for unsupported dynamic requires Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
- Loading branch information
1 parent
40eee93
commit 21c51e0
Showing
92 changed files
with
1,936 additions
and
1,499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
|
||
import { tryParse } from './parse'; | ||
|
||
export default function analyzeTopLevelStatements(parse, code, id) { | ||
const ast = tryParse(parse, code, id); | ||
|
||
let isEsModule = false; | ||
let hasDefaultExport = false; | ||
let hasNamedExports = false; | ||
|
||
for (const node of ast.body) { | ||
switch (node.type) { | ||
case 'ExportDefaultDeclaration': | ||
isEsModule = true; | ||
hasDefaultExport = true; | ||
break; | ||
case 'ExportNamedDeclaration': | ||
isEsModule = true; | ||
if (node.declaration) { | ||
hasNamedExports = true; | ||
} else { | ||
for (const specifier of node.specifiers) { | ||
if (specifier.exported.name === 'default') { | ||
hasDefaultExport = true; | ||
} else { | ||
hasNamedExports = true; | ||
} | ||
} | ||
} | ||
break; | ||
case 'ExportAllDeclaration': | ||
isEsModule = true; | ||
if (node.exported && node.exported.name === 'default') { | ||
hasDefaultExport = true; | ||
} else { | ||
hasNamedExports = true; | ||
} | ||
break; | ||
case 'ImportDeclaration': | ||
isEsModule = true; | ||
break; | ||
default: | ||
} | ||
} | ||
|
||
return { isEsModule, hasDefaultExport, hasNamedExports, ast }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
export function wrapCode(magicString, uses, moduleName, HELPERS_NAME, virtualDynamicRequirePath) { | ||
const args = `module${uses.exports ? ', exports' : ''}`; | ||
|
||
magicString | ||
.trim() | ||
.prepend(`var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`) | ||
.append( | ||
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});` | ||
); | ||
} | ||
|
||
export function rewriteExportsAndGetExportsBlock( | ||
magicString, | ||
moduleName, | ||
wrapped, | ||
topLevelModuleExportsAssignments, | ||
topLevelExportsAssignmentsByName, | ||
defineCompiledEsmExpressions, | ||
deconflict, | ||
isRestorableCompiledEsm, | ||
code, | ||
uses, | ||
HELPERS_NAME | ||
) { | ||
const namedExportDeclarations = [`export { ${moduleName} as __moduleExports };`]; | ||
const moduleExportsPropertyAssignments = []; | ||
let deconflictedDefaultExportName; | ||
|
||
if (!wrapped) { | ||
let hasModuleExportsAssignment = false; | ||
const namedExportProperties = []; | ||
|
||
// Collect and rewrite module.exports assignments | ||
for (const { left } of topLevelModuleExportsAssignments) { | ||
hasModuleExportsAssignment = true; | ||
magicString.overwrite(left.start, left.end, `var ${moduleName}`); | ||
} | ||
|
||
// Collect and rewrite named exports | ||
for (const [exportName, node] of topLevelExportsAssignmentsByName) { | ||
const deconflicted = deconflict(exportName); | ||
magicString.overwrite(node.start, node.left.end, `var ${deconflicted}`); | ||
|
||
if (exportName === 'default') { | ||
deconflictedDefaultExportName = deconflicted; | ||
} else { | ||
namedExportDeclarations.push( | ||
exportName === deconflicted | ||
? `export { ${exportName} };` | ||
: `export { ${deconflicted} as ${exportName} };` | ||
); | ||
} | ||
|
||
if (hasModuleExportsAssignment) { | ||
moduleExportsPropertyAssignments.push(`${moduleName}.${exportName} = ${deconflicted};`); | ||
} else { | ||
namedExportProperties.push(`\t${exportName}: ${deconflicted}`); | ||
} | ||
} | ||
|
||
// Regenerate CommonJS namespace | ||
if (!hasModuleExportsAssignment) { | ||
const moduleExports = `{\n${namedExportProperties.join(',\n')}\n}`; | ||
magicString | ||
.trim() | ||
.append( | ||
`\n\nvar ${moduleName} = ${ | ||
isRestorableCompiledEsm | ||
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})` | ||
: moduleExports | ||
};` | ||
); | ||
} | ||
} | ||
|
||
// Generate default export | ||
const defaultExport = []; | ||
if (isRestorableCompiledEsm) { | ||
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`); | ||
} else if ( | ||
(wrapped || deconflictedDefaultExportName) && | ||
(defineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0) | ||
) { | ||
// eslint-disable-next-line no-param-reassign | ||
uses.commonjsHelpers = true; | ||
defaultExport.push( | ||
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});` | ||
); | ||
} else { | ||
defaultExport.push(`export default ${moduleName};`); | ||
} | ||
|
||
return `\n\n${defaultExport | ||
.concat(namedExportDeclarations) | ||
.concat(moduleExportsPropertyAssignments) | ||
.join('\n')}`; | ||
} |
Oops, something went wrong.