-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent astro:content from depending on Node builtins (#6537)
* Prevent astro:content from depending on Node builtins * Right file * Move the plugin into test-plugins.js
- Loading branch information
Showing
10 changed files
with
77 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Prevent astro:content from depending on Node builtins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { z } from 'zod'; | ||
import { imageMetadata, type Metadata } from '../assets/utils/metadata.js'; | ||
|
||
export function createImage(options: { assetsDir: string; relAssetsDir: string }) { | ||
return () => { | ||
if (options.assetsDir === 'undefined') { | ||
throw new Error('Enable `experimental.assets` in your Astro config to use image()'); | ||
} | ||
|
||
return z.string().transform(async (imagePath) => { | ||
const fullPath = new URL(imagePath, options.assetsDir); | ||
return await getImageMetadata(fullPath); | ||
}); | ||
}; | ||
} | ||
|
||
async function getImageMetadata( | ||
imagePath: URL | ||
): Promise<(Metadata & { __astro_asset: true }) | undefined> { | ||
const meta = await imageMetadata(imagePath); | ||
|
||
if (!meta) { | ||
return undefined; | ||
} | ||
|
||
delete meta.orientation; | ||
return { ...meta, __astro_asset: true }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { | ||
createImage | ||
} from 'astro/content/runtime-assets'; | ||
|
||
const assetsDir = '@@ASSETS_DIR@@'; | ||
|
||
export const image = createImage({ | ||
assetsDir, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
export function preventNodeBuiltinDependencyPlugin() { | ||
// Verifies that `astro:content` does not have a hard dependency on Node builtins. | ||
// This is to verify it will run on Cloudflare and Deno | ||
return { | ||
name: 'verify-no-node-stuff', | ||
generateBundle() { | ||
const nodeModules = ['node:fs', 'node:url', 'node:worker_threads', 'node:path']; | ||
nodeModules.forEach(name => { | ||
const mod = this.getModuleInfo(name); | ||
if(mod) { | ||
throw new Error(`Node builtins snuck in: ${name}`) | ||
} | ||
}); | ||
} | ||
}; | ||
} |