Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class V2 {
const imports: Record<string, string> = {};

for (const specifier of this.specifiers) {
if (new URL(specifier).protocol !== "file:") continue
const module = await this.parser.getModuleSource(specifier);
await write(join(dest, "source", url2path(specifier)), module);
// Track import
Expand All @@ -107,11 +108,8 @@ export async function loadESZIP(filename: string): Promise<ESZIP> {
return await V1.load(bytes);
}

function url2path(urlString: string) {
const url = new URL(urlString);
const tail = url.pathname.split("/").filter(Boolean);
const relativePath = tail.length === 0 ? [".root"] : tail;
return join(url.hostname, ...relativePath);
function url2path(url: string) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Reverting this to the original version.

return join(...(new URL(url).pathname.split("/").filter(Boolean)));
}

async function write(path: string, content: string) {
Expand Down
46 changes: 28 additions & 18 deletions packages/edge-bundler/node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { writeManifest } from './manifest.js'
import { vendorNPMSpecifiers } from './npm_dependencies.js'
import { ensureLatestTypes } from './types.js'
import { nonNullable } from './utils/non_nullable.js'
import { BundleError } from './bundle_error.js'

export interface BundleOptions {
basePath?: string
Expand Down Expand Up @@ -249,26 +250,35 @@ const getFunctionConfigs = async ({
throw err
}

// We failed to extract the configuration because there is an import assert
// in the function code, a deprecated feature that we used to support with
// Deno 1.x. To avoid a breaking change, we treat this error as a special
// case, using the generated ESZIP to extract the configuration. This works
// because import asserts are transpiled to import attributes.
const extractedESZIP = await extractESZIP(deno, eszipPath)
const configs = await Promise.all(
[...internalFunctions, ...userFunctions].map(async (func) => {
const relativePath = relative(basePath, func.path)
const functionPath = join(extractedESZIP.path, relativePath)

return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const
}),
)
try {
// We failed to extract the configuration because there is an import assert
// in the function code, a deprecated feature that we used to support with
// Deno 1.x. To avoid a breaking change, we treat this error as a special
// case, using the generated ESZIP to extract the configuration. This works
// because import asserts are transpiled to import attributes.
const extractedESZIP = await extractESZIP(deno, eszipPath)
const configs = await Promise.all(
[...internalFunctions, ...userFunctions].map(async (func) => {
const relativePath = relative(basePath, func.path)
const functionPath = join(extractedESZIP.path, relativePath)

await extractedESZIP.cleanup()
return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const
}),
)

return {
internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)),
userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)),
await extractedESZIP.cleanup()

return {
internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)),
userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)),
}
} catch (err) {
throw new BundleError(
new Error(
'An error occurred while building an edge function that uses an import assertion. Refer to https://ntl.fyi/import-assert for more information.',
),
{ cause: err },
)
}
}
}
Expand Down
Loading