Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
chore: added missing components for auto improt
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowko committed Feb 1, 2024
1 parent f8cfbc9 commit d481220
Showing 1 changed file with 115 additions and 90 deletions.
205 changes: 115 additions & 90 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { join } from 'node:path'
import { existsSync } from 'node:fs'
import { addComponent, addComponentsDir, addImportsSources, addServerHandler, addTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
import { defu } from 'defu'
import sirv from 'sirv'
import type { I18n } from 'vue-email'
import { join } from "node:path";
import { existsSync } from "node:fs";
import {
addComponent,
addComponentsDir,
addImportsSources,
addServerHandler,
addTemplate,
createResolver,
defineNuxtModule,
} from "@nuxt/kit";
import { defu } from "defu";
import sirv from "sirv";
import type { I18n } from "vue-email";

const components = [
'EBody',
'EButton',
'EColumn',
'EContainer',
'EFont',
'EHead',
'EStyle',
'EHeading',
'EHr',
'EHtml',
'EImg',
'ELink',
'EPreview',
'ERow',
'ESection',
'EText',
'ETailwind',
'EMarkdown',
]
const PATH = '/__vue_email__'
const PATH_PLAYGROUND = `${PATH}/client`
"EBody",
"EButton",
"ECodeBlock",
"ECodeInline",
"EColumn",
"EContainer",
"EFont",
"EHead",
"EStyle",
"EHeading",
"EHr",
"EHtml",
"EImg",
"ELink",
"EPreview",
"ERow",
"ESection",
"EText",
"ETailwind",
"EMarkdown",
];
const PATH = "/__vue_email__";
const PATH_PLAYGROUND = `${PATH}/client`;

// Module options TypeScript interface definition
export interface ModuleOptions {
Expand All @@ -38,113 +48,129 @@ export interface ModuleOptions {

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'vue-email',
configKey: 'vueEmail',
name: "vue-email",
configKey: "vueEmail",
compatibility: {
nuxt: '^3.0.0',
nuxt: "^3.0.0",
bridge: false,
},
},
// Default configuration options of the Nuxt module
defaults(nuxt) {
const isDev = process.env.NODE_ENV === 'development' || nuxt.options.dev
const isDev = process.env.NODE_ENV === "development" || nuxt.options.dev;

return {
baseUrl: null,
playground: isDev,
autoImport: false,
}
};
},
async setup (options, nuxt) {
const { resolve } = createResolver(import.meta.url)
const playgroundDir = resolve('../dist/client')


nuxt.options.runtimeConfig.public.vueEmail = defu(nuxt.options.runtimeConfig.public.vueEmail, options)
async setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url);
const playgroundDir = resolve("../dist/client");

let tempaltesDir = '/emails';
nuxt.options.runtimeConfig.public.vueEmail = defu(
nuxt.options.runtimeConfig.public.vueEmail,
options
);

for(const layer of nuxt.options._layers) {
let tempaltesDir = "/emails";

const templatePath = join(layer.cwd, '/emails')
const pathFound = existsSync(templatePath)
for (const layer of nuxt.options._layers) {
const templatePath = join(layer.cwd, "/emails");
const pathFound = existsSync(templatePath);

if(!pathFound) continue;
if (!pathFound) continue;
tempaltesDir = templatePath;
break;
}

nuxt.options.nitro.alias = nuxt.options.nitro.alias || {}
nuxt.options.nitro.externals = defu(typeof nuxt.options.nitro.externals === 'object' ? nuxt.options.nitro.externals : {}, {
inline: [resolve('./runtime')],
})
nuxt.options.nitro.alias = nuxt.options.nitro.alias || {};
nuxt.options.nitro.externals = defu(
typeof nuxt.options.nitro.externals === "object"
? nuxt.options.nitro.externals
: {},
{
inline: [resolve("./runtime")],
}
);

nuxt.options.nitro.alias = defu(nuxt.options.nitro.alias, {
'#vue-email': resolve('./runtime/server/nitro'),
})
"#vue-email": resolve("./runtime/server/nitro"),
});

nuxt.options.nitro.serverAssets = nuxt.options.nitro.serverAssets || []
nuxt.options.nitro.serverAssets = nuxt.options.nitro.serverAssets || [];
nuxt.options.nitro.serverAssets.push({
baseName: 'emails',
baseName: "emails",
dir: tempaltesDir,
})
});

// Setup playground. Only available in development

if (options.playground) {
addServerHandler({
handler: resolve('./runtime/server/api/emails.get'),
route: '/api/emails',
method: 'get',
handler: resolve("./runtime/server/api/emails.get"),
route: "/api/emails",
method: "get",
lazy: true,
})
});
addServerHandler({
handler: resolve('./runtime/server/api/render/[file].get'),
route: '/api/render/:file',
method: 'get',
handler: resolve("./runtime/server/api/render/[file].get"),
route: "/api/render/:file",
method: "get",
lazy: true,
})
});

nuxt.hook('vite:serverCreated', async (server) => {
server.middlewares.use(PATH_PLAYGROUND, sirv(playgroundDir, { single: true, dev: true }))
})
nuxt.hook("vite:serverCreated", async (server) => {
server.middlewares.use(
PATH_PLAYGROUND,
sirv(playgroundDir, { single: true, dev: true })
);
});

// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore runtime type
nuxt.hook('devtools:customTabs', (iframeTabs) => {
nuxt.hook("devtools:customTabs", (iframeTabs) => {
iframeTabs.push({
name: 'vueemail',
title: 'Vue Email',
icon: 'twemoji:incoming-envelope',
name: "vueemail",
title: "Vue Email",
icon: "twemoji:incoming-envelope",
view: {
type: 'iframe',
type: "iframe",
src: PATH_PLAYGROUND,
},
})
})
});
});
}

addTemplate({
filename: 'types/vue-email.d.ts',
getContents: () => ["declare module '#vue-email' {", ` const useCompiler: typeof import('${resolve('./runtime/server/nitro')}').useCompiler`, '}'].join('\n'),
})

nuxt.hook('prepare:types', (options) => {
filename: "types/vue-email.d.ts",
getContents: () =>
[
"declare module '#vue-email' {",
` const useCompiler: typeof import('${resolve(
"./runtime/server/nitro"
)}').useCompiler`,
"}",
].join("\n"),
});

nuxt.hook("prepare:types", (options) => {
options.references.push({
path: resolve(nuxt.options.buildDir, 'types/vue-email.d.ts'),
})
})
path: resolve(nuxt.options.buildDir, "types/vue-email.d.ts"),
});
});

// addPlugin(resolve('./runtime/plugins/vue-email'))

if(options.autoImport) {
if (options.autoImport) {
components.forEach((component) => {
addComponent({
name: component,
export: component,
filePath: 'vue-email',
})
})
filePath: "vue-email",
});
});

await addComponentsDir({
// TODO: add options to add a custom path and indicate if is absolute or relative
Expand All @@ -154,15 +180,14 @@ export default defineNuxtModule<ModuleOptions>({
// custom:
// path: options?.emailsDir || tempaltesDir,
path: tempaltesDir,
extensions: ['vue'],
extensions: ["vue"],
global: true,
})
});

addImportsSources({
from: 'vue-email',
imports: ['useRender'],
})
from: "vue-email",
imports: ["useRender"],
});
}

}
})
},
});

0 comments on commit d481220

Please sign in to comment.