Skip to content

Commit

Permalink
Added plugin option
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 20, 2022
1 parent f86720f commit 446f2da
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-typed-router",
"version": "1.2.4",
"version": "1.2.5",
"description": "Provide autocompletion for pages route names generated by Nuxt router",
"type": "module",
"main": "./dist/module.cjs",
Expand All @@ -21,7 +21,7 @@
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"build:test": "cross-env NUXT_BUILD_TYPE=stub yarn prepack && yarn dev:build",
"test": "yarn build:test && vitest run",
"test": "yarn dev:prepare && yarn build:test && vitest run",
"test:watch": "yarn build:test && vitest"
},
"publishConfig": {
Expand Down
41 changes: 24 additions & 17 deletions src/generators/nuxtHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Nuxt } from '@nuxt/schema/dist/index';
import { NuxtRouteConfig } from '@nuxt/types/config/router';
import chalk from 'chalk';
import logSymbols from 'log-symbols';
import { ModuleOptions } from '../types';
import { saveRouteFiles } from '../utils';
import { constructRouteMap } from './main.generator';
import {
Expand All @@ -13,31 +14,37 @@ import {
createRuntimeRoutesFile,
} from './output.generator';

export function routeHook(outDir: string, routesObjectName: string, srcDir: string, nuxt: Nuxt) {
export function routeHook(
{ outDir, plugin, routesObjectName }: ModuleOptions,
srcDir: string,
nuxt: Nuxt
) {
try {
extendPages(async (routes: NuxtRouteConfig[]) => {
if (routes.length) {
const { routesDeclTemplate, routesList, routesObjectTemplate, routesParams } =
constructRouteMap(routes);

const pluginName = '__typed-router.ts';
// const runtimeDir = resolve(
// __dirname,
// process.env.NUXT_BUILD_TYPE === 'stub' ? '../../dist/runtime' : './runtime'
// );
// const pluginPath = resolve(runtimeDir, pluginName);
if (plugin) {
const pluginName = '__typed-router.ts';
// const runtimeDir = resolve(
// __dirname,
// process.env.NUXT_BUILD_TYPE === 'stub' ? '../../dist/runtime' : './runtime'
// );
// const pluginPath = resolve(runtimeDir, pluginName);

// `addPlugin` not working, workaround with creating plugin in user srcDir `plugins` folder
// `addPlugin` not working, workaround with creating plugin in user srcDir `plugins` folder

nuxt.hook('build:done', async () => {
const pluginFolder = `${srcDir}/plugins`;
await saveRouteFiles(
pluginFolder,
srcDir,
pluginName,
createRuntimePluginFile(routesDeclTemplate)
);
});
nuxt.hook('build:done', async () => {
const pluginFolder = `${srcDir}/plugins`;
await saveRouteFiles(
pluginFolder,
srcDir,
pluginName,
createRuntimePluginFile(routesDeclTemplate)
);
});
}

await Promise.all([
saveRouteFiles(
Expand Down
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineNuxtModule } from '@nuxt/kit';
import { Nuxt } from '@nuxt/schema';
import { routeHook } from './generators/nuxtHook';
import type { ModuleOptions } from './types';

Expand All @@ -14,11 +15,10 @@ export default defineNuxtModule<ModuleOptions>({
outDir: `./generated`,
routesObjectName: 'routerPagesNames',
},
setup(moduleOptions, nuxt: any) {
setup(moduleOptions, nuxt: Nuxt) {
const srcDir = nuxt.options.srcDir;
const { outDir, routesObjectName } = moduleOptions;
const { plugin = true, ...otherOptions } = moduleOptions;

nuxt.hook('pages:extend', () => routeHook(outDir!, routesObjectName!, srcDir, nuxt));
routeHook(outDir!, routesObjectName!, srcDir, nuxt);
nuxt.hook('pages:extend', () => routeHook({ ...otherOptions, plugin }, srcDir, nuxt));
},
});
7 changes: 5 additions & 2 deletions src/types/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface ModuleOptions {
* @default "routerPagesNames"
* */
routesObjectName?: string;
/** @deprecated */
stripAtFromName?: boolean;
/**
* Set to false if you don't want a plugin generated
* @default true
*/
plugin?: boolean;
}

0 comments on commit 446f2da

Please sign in to comment.