Skip to content

Commit

Permalink
feat: support esm (#613)
Browse files Browse the repository at this point in the history
* feat: support esm

CommonJS and ES Modules could be imported using `import()`.

fix #612

* fix: convert path for windows
  • Loading branch information
abetomo authored Oct 24, 2022
1 parent 9a7051c commit 7997cfd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Edit the .env, deploy.env, ${program.contextFile}, \
event_sources.json and ${program.eventFile} files as needed.`)
}

run (program) {
async run (program) {
if (!['nodejs14.x', 'nodejs16.x'].includes(program.runtime)) {
console.error(`Runtime [${program.runtime}] is not supported.`)
process.exit(254)
Expand All @@ -67,7 +67,18 @@ event_sources.json and ${program.eventFile} files as needed.`)
this._setRunTimeEnvironmentVars(program)
}

const handler = require(path.join(process.cwd(), filename))[handlername]
const handlerFilePath = (() => {
const filePath = path.join(process.cwd(), filename)
if (path.sep === '\\') {
// Convert because of error in Windows.
// Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]:
// Only URLs with a scheme in: file, data are supported by the default ESM loader.
// On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
return 'file:///' + filePath.split(path.sep).join('/')
}
return filePath
})()
const handler = (await import(handlerFilePath))[handlername]
const event = require(path.join(process.cwd(), program.eventFile))
const context = require(path.join(process.cwd(), program.contextFile))
const enableRunMultipleEvents = (() => {
Expand Down

0 comments on commit 7997cfd

Please sign in to comment.