Skip to content

Commit

Permalink
Merge branch 'main' into feat/rework-options
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Feb 14, 2024
2 parents b5807b6 + f81aeed commit cf6ca5d
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 35 deletions.
4 changes: 3 additions & 1 deletion docs/src/content/docs/getting-started/showcase.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Want to see Astro Integration Kit in real projects? See by yourself!
- [Astro Loading Indicator](https://github.com/florian-lefebvre/astro-loading-indicator) - by Florian Lefebvre
- [Astro Tailwind Config Viewer](https://github.com/florian-lefebvre/astro-tailwind-config-viewer) - by Florian Lefebvre
- [Astro Commerce JS](https://github.com/adammatthiesen/astro-commercejs) - by Adam Matthiesen
- [Astro Qwik](https://github.com/QwikDev/astro) - by Jack Shelton
- [Astro Page Insight](https://github.com/ktym4a/astro-page-insight) - by ktym4a

:::note
Want to add yours? [Open an issue](https://github.com/florian-lefebvre/astro-integration-kit/issues/new) on the Github repository!
Want to add yours? Click on "Edit page" below to open a PR!
:::
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "root",
"private": true,
"packageManager": "[email protected].1",
"packageManager": "[email protected].2",
"engines": {
"node": ">=18.19.0"
},
Expand Down
6 changes: 6 additions & 0 deletions package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# astro-integration-kit

## 0.1.2

### Patch Changes

- 79200b4: Prevents virtual imports name from starting with "astro:" (reserved for Astro core)

## 0.1.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-integration-kit",
"version": "0.1.1",
"version": "0.1.2",
"description": "A package that contains utilities to help you build Astro integrations.",
"author": {
"email": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion package/src/core/define-integration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AstroIntegration, HookParameters } from "astro";
import type { z } from "astro/zod";
import { DEFAULT_HOOK_NAMES } from "../internal/constants.js";
import type { AnyOptions, AnyPlugin, ExtendedHooks } from "./types.js";
import type { AnyPlugin, ExtendedHooks } from "./types.js";

/**
* A powerful wrapper around the standard Astro Integrations API. It allows to provide extra hooks, functionality
Expand Down
6 changes: 3 additions & 3 deletions package/src/core/define-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import type { Hooks, Plugin } from "./types.js";
* @param {Function} plugin.implementation - The actual function definition. Refer to docs for usage
*
* @see https://astro-integration-kit.netlify.app/utilities/define-plugin/
*
*
* ```ts
* import { definePlugin } from "../core/define-plugin.js";
* import { addVitePlugin } from "../utilities/add-vite-plugin.js";
* import type { Plugin as VitePlugin } from "vite"
*
*
* export const addVitePluginPlugin = definePlugin({
* name: "addVitePlugin",
* hook: "astro:config:setup",
Expand All @@ -32,4 +32,4 @@ export const definePlugin = <
) => (...args: Array<any>) => any,
>(
plugin: Plugin<TName, THook, TImplementation>,
) => plugin;
) => plugin;
2 changes: 1 addition & 1 deletion package/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type AssertPluginsArray<T extends Array<unknown>> = T extends Array<AnyPlugin>

// When we extend the params, we really only want to have this shape:
// {
// test: (param: string) => void
// test: (param: string) => void
// }
// So this type maps over the object to actually return what we want.
type PluginsToImplementation<TPlugins extends Record<string, AnyPlugin>> = {
Expand Down
2 changes: 1 addition & 1 deletion package/src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export const DEFAULT_HOOK_NAMES = [
"astro:build:generated",
"astro:build:ssr",
"astro:build:done",
] as const satisfies Array<keyof Hooks>;
] as const satisfies Array<keyof Hooks>;
2 changes: 1 addition & 1 deletion package/src/plugins/add-vite-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin as VitePlugin } from "vite";
import { definePlugin } from "../core/define-plugin.js";
import { addVitePlugin } from "../utilities/add-vite-plugin.js";
import type { Plugin as VitePlugin } from "vite"

export const addVitePluginPlugin = definePlugin({
name: "addVitePlugin",
Expand Down
7 changes: 7 additions & 0 deletions package/src/utilities/add-virtual-import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { HookParameters } from "astro";
import { AstroError } from "astro/errors";
import type { Plugin } from "vite";
import { addVitePlugin } from "./add-vite-plugin.js";

Expand Down Expand Up @@ -65,6 +66,12 @@ export const addVirtualImport = ({
content: string;
updateConfig: HookParameters<"astro:config:setup">["updateConfig"];
}) => {
if (name.startsWith("astro:")) {
throw new AstroError(
`Virtual import name prefix can't be "astro:" (for "${name}") because it's reserved for Astro core.`,
);
}

addVitePlugin({
plugin: createVirtualModule(name, content),
updateConfig,
Expand Down
6 changes: 3 additions & 3 deletions package/tests/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, AddedParam } from "../src/core/types.js";
import type { AddedParam, Plugin } from "../src/core/types.js";
import { addDtsPlugin } from "../src/plugins/add-dts.js";

type A = Plugin<"a", "astro:config:setup", () => (param: string) => boolean>;
Expand All @@ -15,6 +15,6 @@ type Plugins = [A, B, C, AOverride, D];
type X1 = AddedParam<Plugins, "astro:config:setup">;
type X2 = AddedParam<Plugins, "astro:config:done">;

const plugins = [addDtsPlugin]
const plugins = [addDtsPlugin];

type X3 = AddedParam<typeof plugins, "astro:config:setup">
type X3 = AddedParam<typeof plugins, "astro:config:setup">;
2 changes: 1 addition & 1 deletion package/tests/unit/add-dts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
import type { AstroIntegrationLogger } from "astro";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { addDts } from "../../src/utilities/add-dts.js";
import { createResolver } from "../../src/core/create-resolver.js";
import { addDts } from "../../src/utilities/add-dts.js";

const tempFolderName = ".TMP_ADDDTS/";
const { resolve } = createResolver(import.meta.url);
Expand Down
32 changes: 25 additions & 7 deletions package/tests/unit/add-virtual-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { AstroError } from "astro/errors";
import { type Mock, afterEach, describe, expect, test, vi } from "vitest";
import { addVirtualImport } from "../../src/utilities/add-virtual-import.js";
import { addVitePlugin } from "../../src/utilities/add-vite-plugin.js";

vi.mock("../../src/utils/add-vite-plugin.js");
vi.mock("../../src/utilities/add-vite-plugin.js");

const pluginNameStub = <T extends string>(name: T): `vite-plugin-${T}` =>
`vite-plugin-${name}`;

describe("add-virtual-import", () => {
const name = "test-module";
const content = "export default {}";
const updateConfig = vi.fn();

afterEach(() => {
vi.clearAllMocks();
});

test("It should call `addVitePlugin`", () => {
const updateConfig = vi.fn();

addVirtualImport({
name,
content,
Expand All @@ -28,8 +28,6 @@ describe("add-virtual-import", () => {
});

test("`addVitePlugin` should get called with the correct plugin name", () => {
const updateConfig = vi.fn();

addVirtualImport({
name,
content,
Expand All @@ -44,8 +42,6 @@ describe("add-virtual-import", () => {
});

test("Virtual module should resolve correct name", () => {
const updateConfig = vi.fn();

addVirtualImport({
name,
content,
Expand All @@ -58,4 +54,26 @@ describe("add-virtual-import", () => {

expect(resolvedVirtualModuleId).toEqual(`\0${name}`);
});

test("It should throw an error if you try and prefix your virtual import with 'astro:'", () => {
const testFunction = () =>
addVirtualImport({
name: `astro:${name}`,
content,
updateConfig,
});

expect(testFunction).toThrowError();
});

test("It should throw an AstroError if you try and prefix your virtual import with 'astro:'", () => {
const testFunction = () =>
addVirtualImport({
name: `astro:${name}`,
content,
updateConfig,
});

expect(testFunction).toThrowError(AstroError);
});
});
2 changes: 1 addition & 1 deletion package/tests/unit/add-vite-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HookParameters } from "astro";
import type { Plugin } from "vite";
import { describe, expect, test, vi } from "vitest";
import { addVitePlugin } from "../../src/utilities/add-vite-plugin.js";
import type { HookParameters } from "astro";

describe("addVitePlugin", () => {
test("Should run", () => {
Expand Down
2 changes: 1 addition & 1 deletion package/tests/unit/create-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("createResolver", () => {
// TODO: make it work
test.skip("`resolve()` should return the `import.meta.url`", () => {
const resolver = createResolver(import.meta.url);

expect(resolver.resolve()).toEqual(directory);
});

Expand Down
20 changes: 10 additions & 10 deletions package/tests/unit/define-integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import type {
HookParameters,
} from "astro";
import { type Mock, afterEach, describe, expect, test, vi } from "vitest";
import type { ExtendedHooks as _ExtendedHooks } from "../../src/core/types.js";
import { defineIntegration } from "../../src/core/define-integration.js";
import { defineOptions } from "../../src/core/define-options.js";
import type { ExtendedHooks as _ExtendedHooks } from "../../src/core/types.js";
import { corePlugins } from "../../src/plugins/index.js";
import { addDts as mockAddDts } from "../../src/utilities/add-dts.js";
import { addVirtualImport as mockAddVirtualImport } from "../../src/utilities/add-virtual-import.js";
import { addVitePlugin as mockAddVitePlugin } from "../../src/utilities/add-vite-plugin.js";
import { hasIntegration as mockHasIntegration } from "../../src/utilities/has-integration.js";
import { watchIntegration as mockWatchIntegration } from "../../src/utilities/watch-integration.js";
import { corePlugins } from "../../src/plugins/index.js";
import { defineOptions } from "../../src/core/define-options.js";

vi.mock("../../src/utils/add-virtual-import.js");
vi.mock("../../src/utils/add-vite-plugin.js");
vi.mock("../../src/utils/add-dts.js");
vi.mock("../../src/utils/has-integration.js");
vi.mock("../../src/utils/watch-integration.js");
vi.mock("../../src/utilities/add-virtual-import.js");
vi.mock("../../src/utilities/add-vite-plugin.js");
vi.mock("../../src/utilities/add-dts.js");
vi.mock("../../src/utilities/has-integration.js");
vi.mock("../../src/utilities/watch-integration.js");

const astroConfigSetupParamsStub = (
params?: HookParameters<"astro:config:setup">,
Expand All @@ -39,7 +39,7 @@ const astroConfigSetupParamsStub = (
...(params || {}),
});

const plugins = [...corePlugins]
const plugins = [...corePlugins];

type ExtendedHooks = _ExtendedHooks<typeof plugins>;

Expand All @@ -50,7 +50,7 @@ describe("defineIntegration", () => {

test("Should run", () => {
const name = "my-integration";
const setup = () => ({})
const setup = () => ({});

expect(() =>
defineIntegration({
Expand Down
9 changes: 7 additions & 2 deletions playground/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { readFileSync } from "node:fs";
import {
createResolver,
defineIntegration,
defineOptions,
} from "astro-integration-kit";
import { corePlugins } from "astro-integration-kit/plugins";
import { z } from "astro/zod";
Expand All @@ -19,7 +18,7 @@ const OptionsSchema = z.object({

const testIntegration = defineIntegration({
name: "test-integration",
options: OptionsSchema,
optionsSchema: OptionsSchema,
plugins: [...corePlugins],
setup: ({ options }) => {
const { resolve } = createResolver(import.meta.url);
Expand Down Expand Up @@ -66,6 +65,12 @@ const testIntegration = defineIntegration({
if (hasIntegration("integration-b", "after", "integration-a")) {
console.log("Integration B is installed after Integration A");
}

// Test addVirtualImport disallowed list
// addVirtualImport({
// name: "astro:test",
// content: "export default {}"
// });
},
};
},
Expand Down

0 comments on commit cf6ca5d

Please sign in to comment.