Skip to content

Commit

Permalink
Remove shebang (close #596)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 18, 2023
1 parent a546b8b commit cffa844
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
23 changes: 13 additions & 10 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ rebuild:

for _, file := range result.OutputFiles {
outputContent := file.Contents

if strings.HasSuffix(file.Path, ".js") {
buf := bytes.NewBufferString(fmt.Sprintf(
"/* esm.sh - esbuild bundle(%s) %s %s */\n",
Expand All @@ -571,6 +572,11 @@ rebuild:
eol = ""
}

// remove shebang
if bytes.HasPrefix(outputContent, []byte("#!/")) {
outputContent = outputContent[bytes.IndexByte(outputContent, '\n')+1:]
}

// replace external imports/requires
for depIndex, name := range externalDeps.Values() {
var importPath string
Expand Down Expand Up @@ -754,12 +760,10 @@ rebuild:
// right shift to strip the object `key`
shift := 0
for i, l := 1, len(p); i < l; i++ {
c := p[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '$' {
shift++
} else {
if !isJSIdentChar(p[i]) {
break
}
shift++
}
importName := string(p[1 : shift+1])
if includes(depESM.NamedExports, importName) {
Expand Down Expand Up @@ -788,16 +792,15 @@ rebuild:
}
}
if !marked {
cjsImportNames.Add("lazy")
cjsImportNames.Add("default?")
}
}
cjsContext = bytes.HasSuffix(p, []byte{'('}) && !bytes.HasSuffix(p, []byte("import("))
if cjsContext {
// left shift to strip the `require` ident generated by esbuild
shift := 0
for i := len(p) - 2; i >= 0; i-- {
c := p[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '$' {
if isJSIdentChar(p[i]) {
shift++
} else {
break
Expand Down Expand Up @@ -832,14 +835,14 @@ rebuild:
fmt.Fprintf(buf, `const __%s$ = ()=>!0;%s`, identifier, eol)
} else {
switch importName {
case "default":
fmt.Fprintf(buf, `import __%s$ from "%s";%s`, identifier, importPath, eol)
case "*":
fmt.Fprintf(buf, `import * as __%s$ from "%s";%s`, identifier, importPath, eol)
case "*?":
fmt.Fprintf(buf, `import * as _%s$ from "%s";%s`, identifier, importPath, eol)
fmt.Fprintf(buf, `const __%s$ = Object.assign({__esModule:!0},_%s$);%s`, identifier, identifier, eol)
case "lazy":
case "default":
fmt.Fprintf(buf, `import __%s$ from "%s";%s`, identifier, importPath, eol)
case "default?":
fmt.Fprintf(buf, `import * as _%s$ from "%s";%s`, identifier, importPath, eol)
if task.Target == "deno" || task.Target == "denonext" || task.Target == "node" || task.Target >= "es2020" {
fmt.Fprintf(buf, `const __%s$ = _%s$.default??_%s$;%s`, identifier, identifier, identifier, eol)
Expand Down
4 changes: 4 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func kill(pidFile string) (err error) {
return process.Kill()
}

func isJSIdentChar(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '$'
}

func validateJS(filename string) (isESM bool, namedExports []string, err error) {
data, err := os.ReadFile(filename)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions test/issue-596/issue-596.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";


Deno.test("issue #596", async () => {
const code = await fetch("http://localhost:8080/v115/[email protected]/deno/src/cli/index.js").then(res=>res.text())
assert(!code.includes("#!/usr/bin/env node"))
});

0 comments on commit cffa844

Please sign in to comment.