Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new flag --experimental-enable-top-level-await (#742)
- Loading branch information
Showing
8 changed files
with
85 additions
and
8 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
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,63 @@ | ||
import { parse } from "acorn"; | ||
import MagicString from "magic-string"; | ||
import { simple as simpleWalk } from "acorn-walk"; | ||
|
||
/// Emit a block of javascript that enables top level awaits | ||
/// * removes any top-level exports | ||
/// * wraps source with async function and executes it | ||
export function enableTopLevelAwait(source, filename = "<input>") { | ||
const magicString = new MagicString(source, { | ||
filename, | ||
}); | ||
|
||
const ast = parse(source, { | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
}); | ||
|
||
function replaceExportWithDeclaration(node) { | ||
let body = ''; | ||
if (node.declaration != null) { | ||
// The following may have declarations: | ||
// ExportNamedDeclaration, e.g.: | ||
// export var i = 0; | ||
// export const y = foo(); | ||
// ExportDefaultDeclaration, e.g.: | ||
// export default 1; | ||
// export default foo(); | ||
body = magicString | ||
.snip(node.declaration.start, node.declaration.end) | ||
.toString(); | ||
} | ||
magicString.overwrite(node.start, node.end, body); | ||
} | ||
|
||
simpleWalk(ast, { | ||
ExportNamedDeclaration(node) { | ||
replaceExportWithDeclaration(node); | ||
}, | ||
ExportSpecifier(node) { | ||
replaceExportWithDeclaration(node); | ||
}, | ||
ExportDefaultDeclaration(node) { | ||
replaceExportWithDeclaration(node); | ||
}, | ||
ExportAllDeclaration(node) { | ||
replaceExportWithDeclaration(node); | ||
}, | ||
}); | ||
|
||
magicString.prepend(';((async()=>{'); | ||
magicString.append('})())'); | ||
|
||
// When we're ready to pipe in source maps: | ||
// const map = magicString.generateMap({ | ||
// source: 'source.js', | ||
// file: 'converted.js.map', | ||
// includeContent: true | ||
// }); | ||
|
||
return magicString.toString(); | ||
|
||
} | ||
|
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