Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix-typescript-plugin-types
- Loading branch information
Showing
120 changed files
with
2,094 additions
and
1,581 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.