Skip to content

Commit ee7d48f

Browse files
committed
fix: toggle DENO_NO_PACKAGE_JSON conditionally
1 parent 891a5df commit ee7d48f

File tree

9 files changed

+38
-10
lines changed

9 files changed

+38
-10
lines changed

internal/functions/deploy/bundle.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewDockerBundler(fsys afero.Fs) function.EszipBundler {
2525
return &dockerBundler{fsys: fsys}
2626
}
2727

28-
func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (function.FunctionDeployMetadata, error) {
28+
func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (function.FunctionDeployMetadata, error) {
2929
meta := function.NewMetadata(slug, entrypoint, importMap, staticFiles)
3030
fmt.Fprintln(os.Stderr, "Bundling Function:", utils.Bold(slug))
3131
cwd, err := os.Getwd()
@@ -62,9 +62,18 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
6262
cmd = append(cmd, function.BundleFlags...)
6363

6464
env := []string{}
65+
denoNoPackageJsonValue := "1"
66+
if usePackageJson {
67+
denoNoPackageJsonValue = "0"
68+
}
6569
if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" {
6670
env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry)
6771
}
72+
if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" {
73+
env = append(env, "DENO_NO_PACKAGE_JSON="+deno_no_package_json)
74+
} else {
75+
env = append(env, "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue)
76+
}
6877
// Run bundle
6978
if err := utils.DockerRunOnceWithConfig(
7079
ctx,

internal/functions/deploy/bundle_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestDockerBundle(t *testing.T) {
5959
filepath.Join("hello", "index.ts"),
6060
filepath.Join("hello", "deno.json"),
6161
[]string{filepath.Join("hello", "data.pdf")},
62+
false,
6263
&body,
6364
)
6465
// Check error
@@ -86,6 +87,7 @@ func TestDockerBundle(t *testing.T) {
8687
"hello/index.ts",
8788
"",
8889
nil,
90+
false,
8991
nil,
9092
)
9193
// Check error

internal/functions/deploy/deploy.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
136136
functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name)
137137
}
138138
}
139+
packageJsonPath := filepath.Join(functionDir, "package.json")
140+
packageJsonExists := false
141+
if _, err := fsys.Stat(packageJsonPath); err == nil {
142+
packageJsonExists = true
143+
}
144+
function.UsePackageJson = len(function.ImportMap) == 0 && packageJsonExists
139145
if noVerifyJWT != nil {
140146
function.VerifyJWT = !*noVerifyJWT
141147
}

pkg/config/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ type (
200200
FunctionConfig map[string]function
201201

202202
function struct {
203-
Enabled bool `toml:"enabled" json:"-"`
204-
VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"`
205-
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
206-
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
207-
StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"`
203+
Enabled bool `toml:"enabled" json:"-"`
204+
UsePackageJson bool `toml:"-"`
205+
VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"`
206+
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
207+
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
208+
StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"`
208209
}
209210

210211
analytics struct {

pkg/function/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type FunctionDeployMetadata struct {
2424
}
2525

2626
type EszipBundler interface {
27-
Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error)
27+
Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error)
2828
}
2929

3030
func NewEdgeRuntimeAPI(project string, client api.ClientWithResponses, opts ...withOption) EdgeRuntimeAPI {

pkg/function/batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ OUTER:
6060
}
6161
}
6262
var body bytes.Buffer
63-
meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, &body)
63+
meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, function.UsePackageJson, &body)
6464
if errors.Is(err, ErrNoDeploy) {
6565
fmt.Fprintln(os.Stderr, "Skipping undeployable Function:", slug)
6666
continue

pkg/function/batch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
type MockBundler struct {
1919
}
2020

21-
func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) {
21+
func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) {
2222
if staticFiles == nil {
2323
staticFiles = []string{}
2424
}

pkg/function/bundle.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
}
4949
)
5050

51-
func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) {
51+
func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) {
5252
meta := NewMetadata(slug, entrypoint, importMap, staticFiles)
5353
outputPath := filepath.Join(b.tempDir, slug+".eszip")
5454
// TODO: make edge runtime write to stdout
@@ -65,9 +65,17 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
6565
defer cancel() // release resources if command exits before timeout
6666
ctx = timeoutCtx
6767
}
68+
denoNoPackageJsonValue := "1"
69+
if usePackageJson {
70+
denoNoPackageJsonValue = "0"
71+
}
72+
6873
cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...)
6974
cmd.Stderr = os.Stderr
7075
cmd.Stdout = os.Stdout
76+
if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" {
77+
cmd.Env = append(os.Environ(), "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue)
78+
}
7179
if err := cmd.Run(); err != nil {
7280
return meta, errors.Errorf("failed to bundle function: %w", err)
7381
}

pkg/function/bundle_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestBundleFunction(t *testing.T) {
4848
"hello/index.ts",
4949
"hello/deno.json",
5050
[]string{"hello/data.pdf"},
51+
true,
5152
&body,
5253
)
5354
// Check error
@@ -78,6 +79,7 @@ func TestBundleFunction(t *testing.T) {
7879
"hello/index.ts",
7980
"",
8081
nil,
82+
true,
8183
&body,
8284
)
8385
// Check error

0 commit comments

Comments
 (0)