@adonisjs/inertia
Version:
Official Inertia.js adapter for AdonisJS
58 lines (55 loc) • 1.99 kB
JavaScript
import lodash from "@poppinss/utils/lodash";
const GLOB = {
vue3: ["**/*.vue"],
react: ["**/*.ts", "**/*.tsx"]
};
const SUPPORTED_FRAMEWORKS = Object.keys(GLOB);
const TYPES_EXTRACTION_HELPER = {
vue3: `import type { VNodeProps, AllowedComponentProps, ComponentInstance } from 'vue'
type ExtractProps<T> = Omit<
ComponentInstance<T>['$props'],
keyof VNodeProps | keyof AllowedComponentProps
>`,
react: `import type React from 'react'
import type { Prettify } from '@adonisjs/core/types/common'
type ExtractProps<T> =
T extends React.FC<infer Props>
? Prettify<Omit<Props, 'children'>>
: T extends React.Component<infer Props>
? Prettify<Omit<Props, 'children'>>
: never`
};
const indexPages = function(config) {
if (!SUPPORTED_FRAMEWORKS.includes(config.framework)) throw new Error(`Unsupported framework "${config.framework}". Types generation is available only for ${SUPPORTED_FRAMEWORKS.join(",")}`);
return { run(_, __, indexGenerator) {
indexGenerator.add("inertiaPages", {
source: config.source ?? "inertia/pages",
glob: GLOB[config.framework],
output: ".adonisjs/server/pages.d.ts",
as(vfs, buffer, ___, helpers) {
const filesList = vfs.asList();
buffer.writeLine(`import '@adonisjs/inertia/types'`);
buffer.writeLine(TYPES_EXTRACTION_HELPER[config.framework]);
buffer.write(`declare module '@adonisjs/inertia/types' {`).indent();
buffer.write(`export interface InertiaPages {`).indent();
Object.keys(filesList).forEach((key) => {
buffer.write(`'${key}': ExtractProps<(typeof import('${helpers.toImportPath(filesList[key])}'))['default']>`);
});
buffer.dedent().write(`}`);
buffer.dedent().write(`}`);
}
});
} };
};
function defineConfig(config) {
return lodash.merge({
rootView: "inertia_layout",
history: { encrypt: false },
ssr: {
enabled: false,
bundle: "ssr/ssr.js",
entrypoint: "inertia/ssr.tsx"
}
}, config);
}
export { indexPages as n, defineConfig as t };