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

Commit

Permalink
feat(ponyfill): Strip shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoreis committed Aug 8, 2022
1 parent 45cd6e1 commit 1bee6c1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as esbuild from 'https://deno.land/x/esbuild@v0.14.53/wasm.js'
import { denoPlugin } from 'https://deno.land/x/esbuild_deno_loader@0.5.2/mod.ts'
import stripShebang from 'https://esm.sh/strip-shebang@2.0.0'

const AsyncFunction = (async function () {}).constructor

export async function importModule(
moduleName: string
): Promise<Record<'default' | string, any>> {
try {
return await import(moduleName)
} catch {
// try {
// return await import(moduleName)
// } catch {
esbuild.initialize({ worker: false })

const result = await esbuild.build({
Expand All @@ -26,11 +27,10 @@ export async function importModule(
const [ before, after ] = text.split('export {')

const body =
before
stripShebang(before)
+ (after
? 'return {' + after.replaceAll(/(\w+) (as) (\w+)/gi, '$3: $1')
: '')

return AsyncFunction(body)()
}
}

0 comments on commit 1bee6c1

Please sign in to comment.