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 91a154b
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/module/internal/compile-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ function init() {
}

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

if (compileData.firstAwaitOutsideFunction === null) {
async = false
}

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

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

content =
"const " + runtimeName + "=exports;" +
(returnRun
? "return "
: ""
) +
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 91a154b

Please sign in to comment.