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

Commit

Permalink
Only compile source with top-level return if the module itself does. [c…
Browse files Browse the repository at this point in the history
…loses #769]
  • Loading branch information
jdalton committed Mar 28, 2019
1 parent 353fb1e commit 07036ec
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/module/internal/compile-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ function init() {
}

function compileCJS(compileData, options) {
let { async } = options
let changed = compileData.transforms !== 0
let content = compileData.code

if (changed) {
const returnRun = compileData.firstReturnOutsideFunction !== null
const { runtimeName } = options

const returnRun = options.return === void 0
? true
: options.return
if (compileData.firstAwaitOutsideFunction === null) {
async = false
}

content =
"const " + runtimeName + "=exports;" +
Expand All @@ -40,14 +42,14 @@ function init() {
: ""
) +
runtimeName + ".r((" +
(options.async
(async
? "async "
: ""
) +
"function(exports,require){" +
content +
"\n}))"
} else if (options.async) {
} else if (async) {
changed = true
content =
"(async () => { " +
Expand All @@ -64,15 +66,8 @@ function init() {
}

function compileESM(compileData, options) {
const {
cjsVars,
runtimeName
} = options

const returnRun = options.return === void 0
? true
: options.return

const { cjsVars, runtimeName } = options
const returnRun = compileData.firstReturnOutsideFunction !== null
const yieldCode = "yield;" + runtimeName + ".s();"
const { yieldIndex } = compileData

Expand All @@ -94,7 +89,10 @@ function init() {
} else {
code =
code.slice(0, yieldIndex) +
(code.charCodeAt(yieldIndex - 1) === SEMICOLON ? "" : ";") +
(code.charCodeAt(yieldIndex - 1) === SEMICOLON
? ""
: ";"
) +
yieldCode +
code.slice(yieldIndex)
}
Expand Down

0 comments on commit 07036ec

Please sign in to comment.