From 1b8ed86db59346de86923f0e0f8ecc9ae39eb463 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 12:28:40 -0500 Subject: [PATCH 1/6] Add fallback for hosts without import.meta.url --- .changeset/lucky-shoes-mate.md | 5 +++++ packages/astro/src/assets/utils/metadata.ts | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .changeset/lucky-shoes-mate.md diff --git a/.changeset/lucky-shoes-mate.md b/.changeset/lucky-shoes-mate.md new file mode 100644 index 000000000000..cfa14b6be15d --- /dev/null +++ b/.changeset/lucky-shoes-mate.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add fallback for hosts without import.meta.url diff --git a/packages/astro/src/assets/utils/metadata.ts b/packages/astro/src/assets/utils/metadata.ts index d1bc37bad205..e16a5463df37 100644 --- a/packages/astro/src/assets/utils/metadata.ts +++ b/packages/astro/src/assets/utils/metadata.ts @@ -1,8 +1,8 @@ import { createRequire } from 'module'; import fs from 'node:fs/promises'; -import { fileURLToPath } from 'node:url'; +import { fileURLToPath, pathToFileURL } from 'node:url'; import { ImageMetadata, InputFormat } from '../types.js'; -const require = createRequire(import.meta.url); +const require = createRequire(getModuleURL(import.meta.url)); const sizeOf = require('image-size'); export interface Metadata extends ImageMetadata { @@ -37,3 +37,16 @@ export async function imageMetadata( orientation, }; } + +/** +* On certain serverless hosts, our ESM bundle is transpiled to CJS before being run, which means +* import.meta.url is undefined, so we'll fall back to __dirname in those cases +* We should be able to remove this once https://github.com/netlify/zip-it-and-ship-it/issues/750 is fixed +*/ +export function getModuleURL(url: string | undefined): string { + if (!url) { + return pathToFileURL(__dirname).toString(); + } + + return url +} From d3ccc9cc8b13089c4b03e7ef66f94e6b2496f84b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 13:07:29 -0500 Subject: [PATCH 2/6] Try using an import --- packages/astro/src/assets/utils/metadata.ts | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/packages/astro/src/assets/utils/metadata.ts b/packages/astro/src/assets/utils/metadata.ts index e16a5463df37..1e96cbe97ec0 100644 --- a/packages/astro/src/assets/utils/metadata.ts +++ b/packages/astro/src/assets/utils/metadata.ts @@ -1,9 +1,7 @@ -import { createRequire } from 'module'; import fs from 'node:fs/promises'; -import { fileURLToPath, pathToFileURL } from 'node:url'; +import { fileURLToPath } from 'node:url'; import { ImageMetadata, InputFormat } from '../types.js'; -const require = createRequire(getModuleURL(import.meta.url)); -const sizeOf = require('image-size'); +import sizeOf from 'image-size'; export interface Metadata extends ImageMetadata { orientation?: number; @@ -37,16 +35,3 @@ export async function imageMetadata( orientation, }; } - -/** -* On certain serverless hosts, our ESM bundle is transpiled to CJS before being run, which means -* import.meta.url is undefined, so we'll fall back to __dirname in those cases -* We should be able to remove this once https://github.com/netlify/zip-it-and-ship-it/issues/750 is fixed -*/ -export function getModuleURL(url: string | undefined): string { - if (!url) { - return pathToFileURL(__dirname).toString(); - } - - return url -} From d5c784f9ee5b517cac4526ff41c25745c0c6fc36 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 13:22:13 -0500 Subject: [PATCH 3/6] Add image-size as external --- packages/astro/src/core/create-vite.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 7308a56dff1f..fcec917d24f0 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -165,7 +165,7 @@ export async function createVite( // shiki is imported by Code.astro, which is no-externalized (processed by Vite). // However, shiki's deps are in CJS and trips up Vite's dev SSR transform, externalize // shiki to load it with node instead. - external: [...(mode === 'dev' ? ['shiki'] : []), ...astroPkgsConfig.ssr.external], + external: [...(mode === 'dev' ? ['shiki', 'image-size'] : []), ...astroPkgsConfig.ssr.external], }, }; From 89fd76b2b5cf9f36028c87912ec158d1aa4180a8 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 13:53:06 -0500 Subject: [PATCH 4/6] Make image-size external --- packages/astro/src/core/sync/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 914783331c5a..82575f0837a6 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -67,6 +67,7 @@ export async function sync( { server: { middlewareMode: true, hmr: false }, optimizeDeps: { entries: [] }, + ssr: { external: ['image-size'] }, logLevel: 'silent', }, { settings, logging, mode: 'build', command: 'build', fs } From 35844a940ff080f498914b833db908fb86b627db Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 14:43:15 -0500 Subject: [PATCH 5/6] Apply suggestions from code review --- .changeset/lucky-shoes-mate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/lucky-shoes-mate.md b/.changeset/lucky-shoes-mate.md index cfa14b6be15d..32ddde78d8af 100644 --- a/.changeset/lucky-shoes-mate.md +++ b/.changeset/lucky-shoes-mate.md @@ -2,4 +2,4 @@ 'astro': patch --- -Add fallback for hosts without import.meta.url +Remove use of createRequire breaking non-Node hosts. From 68b6209db472446a1f9916a8eefc87c819453dc8 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Mar 2023 15:23:53 -0500 Subject: [PATCH 6/6] leave image-size alone --- packages/astro/src/core/create-vite.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 7bd4d9db6a8d..f4316bc2c110 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -57,7 +57,6 @@ const ONLY_DEV_EXTERNAL = [ 'shiki', // Imported by `@astrojs/prism` which exposes `` that is processed by Vite 'prismjs/components/index.js', - 'image-size', ]; /** Return a common starting point for all Vite actions */ @@ -173,7 +172,7 @@ export async function createVite( }, ssr: { noExternal: [...ALWAYS_NOEXTERNAL, ...astroPkgsConfig.ssr.noExternal], - external: [...(mode === 'dev' ? ONLY_DEV_EXTERNAL : ['image-size']), ...astroPkgsConfig.ssr.external], + external: [...(mode === 'dev' ? ONLY_DEV_EXTERNAL : []), ...astroPkgsConfig.ssr.external], }, };