Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return features from server #481

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const getDependencyTrackerPlugin = (
return { external: true }
}

const isLocalImport = specifier.startsWith(path.sep) || specifier.startsWith('.')
const isLocalImport = specifier.startsWith(path.sep) || specifier.startsWith('.') || path.isAbsolute(specifier)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes an issue where importing an absolute directory on Windows was not being treated as a local import.


// If this is a local import, return so that esbuild visits that path.
if (isLocalImport) {
Expand Down Expand Up @@ -161,10 +161,6 @@ export const vendorNPMSpecifiers = async ({
return
}

logger.user(
'You are using npm modules in Edge Functions, which is an experimental feature. Learn more at https://ntl.fyi/edge-functions-npm.',
)

// To bundle an entire module and all its dependencies, create a barrel file
// where we re-export everything from that specifier. We do this for every
// specifier, and each of these files will become entry points to esbuild.
Expand Down
3 changes: 2 additions & 1 deletion node/server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ test('Starts a server and serves requests for edge functions', async () => {
getFunctionsConfig: true,
}

const { functionsConfig, graph, success } = await server(
const { features, functionsConfig, graph, success } = await server(
functions,
{
very_secret_secret: 'i love netlify',
},
options,
)
expect(features).toEqual({})
expect(success).toBe(true)
expect(functionsConfig).toEqual([{ path: '/my-function' }, {}, { path: '/global-netlify' }])

Expand Down
30 changes: 21 additions & 9 deletions node/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef } from '../bridge.js'
import { getFunctionConfig, FunctionConfig } from '../config.js'
import type { EdgeFunction } from '../edge_function.js'
import type { FeatureFlags } from '../feature_flags.js'
import { generateStage2 } from '../formats/javascript.js'
import { ImportMap } from '../import_map.js'
import { getLogger, LogFunction, Logger } from '../logger.js'
Expand All @@ -18,6 +19,7 @@ interface PrepareServerOptions {
distDirectory: string
distImportMapPath?: string
entryPoint?: string
featureFlags?: FeatureFlags
flags: string[]
formatExportTypeError?: FormatFunction
formatImportError?: FormatFunction
Expand All @@ -36,6 +38,7 @@ const prepareServer = ({
deno,
distDirectory,
distImportMapPath,
featureFlags,
flags: denoFlags,
formatExportTypeError,
formatImportError,
Expand Down Expand Up @@ -64,17 +67,22 @@ const prepareServer = ({
formatImportError,
})

const features: Record<string, boolean> = {}
const importMap = baseImportMap.clone()
const vendor = await vendorNPMSpecifiers({
basePath,
directory: distDirectory,
functions: functions.map(({ path }) => path),
importMap,
logger,
})

if (vendor) {
importMap.add(vendor.importMap)
if (featureFlags?.edge_functions_npm_modules) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that you're planning on statically setting this flag from CLI, or do you want to roll this out via DevCycle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using DevCycle.

const vendor = await vendorNPMSpecifiers({
basePath,
directory: distDirectory,
functions: functions.map(({ path }) => path),
importMap,
logger,
})

if (vendor) {
features.npmModules = true
importMap.add(vendor.importMap)
}
}

try {
Expand Down Expand Up @@ -116,6 +124,7 @@ const prepareServer = ({
const success = await waitForServer(port, processRef.ps)

return {
features,
functionsConfig,
graph,
success,
Expand All @@ -141,6 +150,7 @@ interface ServeOptions {
certificatePath?: string
debug?: boolean
distImportMapPath?: string
featureFlags?: FeatureFlags
inspectSettings?: InspectSettings
importMapPaths?: string[]
onAfterDownload?: OnAfterDownloadHook
Expand All @@ -159,6 +169,7 @@ export const serve = async ({
debug,
distImportMapPath,
inspectSettings,
featureFlags,
formatExportTypeError,
formatImportError,
importMapPaths = [],
Expand Down Expand Up @@ -212,6 +223,7 @@ export const serve = async ({
deno,
distDirectory: servePath,
distImportMapPath,
featureFlags,
flags,
formatExportTypeError,
formatImportError,
Expand Down