2023-10-24 10:09:12 +00:00
|
|
|
import { addPlugin, addPluginTemplate, addTemplate, createResolver, defineNuxtModule } from '@nuxt/kit';
|
|
|
|
import { register } from './register';
|
|
|
|
|
|
|
|
export default defineNuxtModule({
|
|
|
|
meta: {
|
|
|
|
name: 'nuxt-primevue',
|
|
|
|
configKey: 'primevue',
|
|
|
|
compatibility: {
|
|
|
|
nuxt: '^3.0.0'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
usePrimeVue: true,
|
|
|
|
resolvePath: undefined,
|
|
|
|
layerOrder: 'tailwind-base, primevue, tailwind-utilities',
|
|
|
|
importPT: undefined,
|
2024-03-05 09:22:33 +00:00
|
|
|
importTheme: undefined,
|
2023-10-24 10:09:12 +00:00
|
|
|
options: {},
|
|
|
|
components: {
|
|
|
|
prefix: '',
|
|
|
|
name: undefined,
|
|
|
|
include: undefined,
|
|
|
|
exclude: undefined
|
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
prefix: '',
|
|
|
|
name: undefined,
|
|
|
|
include: undefined,
|
|
|
|
exclude: undefined
|
|
|
|
},
|
|
|
|
composables: {
|
|
|
|
prefix: '',
|
|
|
|
name: undefined,
|
|
|
|
include: undefined,
|
|
|
|
exclude: undefined
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hooks: {},
|
|
|
|
setup(moduleOptions, nuxt) {
|
|
|
|
const resolver = createResolver(import.meta.url);
|
|
|
|
const registered = register(moduleOptions);
|
2024-03-05 09:22:33 +00:00
|
|
|
const { importPT, importTheme, options } = moduleOptions;
|
2023-10-24 10:09:12 +00:00
|
|
|
|
|
|
|
nuxt.options.runtimeConfig.public.primevue = {
|
|
|
|
...moduleOptions,
|
|
|
|
...registered
|
|
|
|
};
|
|
|
|
|
|
|
|
//nuxt.options.build.transpile.push('nuxt');
|
|
|
|
nuxt.options.build.transpile.push('primevue');
|
|
|
|
|
|
|
|
const styleContent = () => `
|
|
|
|
${registered.styles.map((style) => `import ${style.as} from '${style.from}';`).join('\n')}
|
2024-03-05 09:22:33 +00:00
|
|
|
${importTheme ? `import ${importTheme.as} from '${importTheme.from}';\n` : ''}
|
|
|
|
|
2024-01-24 12:08:37 +00:00
|
|
|
const styleProps = {
|
|
|
|
${options?.csp?.nonce ? `nonce: ${options?.csp?.nonce}` : ''}
|
|
|
|
}
|
2023-10-24 10:09:12 +00:00
|
|
|
const styles = [
|
|
|
|
${registered.injectStylesAsString.join('')},
|
2024-01-24 12:08:37 +00:00
|
|
|
${registered.styles.map((item) => `${item.as} && ${item.as}.getStyleSheet ? ${item.as}.getStyleSheet(undefined, styleProps) : ''`).join(',')}
|
2023-10-24 10:09:12 +00:00
|
|
|
].join('');
|
|
|
|
|
2024-03-05 09:22:33 +00:00
|
|
|
const themes = [
|
2024-03-05 11:08:00 +00:00
|
|
|
${importTheme ? `${registered.styles[0].as} && ${registered.styles[0].as}.getCommonThemeStyleSheet ? ${registered.styles[0].as}.getCommonThemeStyleSheet(${importTheme?.as}, undefined, styleProps) : ''` : ''},
|
2024-03-05 09:22:33 +00:00
|
|
|
${importTheme ? registered.styles.map((item) => `${item.as} && ${item.as}.getThemeStyleSheet ? ${item.as}.getThemeStyleSheet(${importTheme?.as}, undefined, styleProps) : ''`).join(',') : ''}
|
|
|
|
].join('');
|
|
|
|
|
|
|
|
export { styles, themes };
|
2023-10-24 10:09:12 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
nuxt.options.alias['#primevue-style'] = addTemplate({
|
|
|
|
filename: 'primevue-style.mjs',
|
|
|
|
getContents: styleContent
|
|
|
|
}).dst;
|
|
|
|
|
|
|
|
addPlugin(resolver.resolve('./runtime/plugin.client'));
|
|
|
|
|
|
|
|
addPluginTemplate({
|
|
|
|
filename: 'primevue-plugin.mjs',
|
|
|
|
getContents() {
|
|
|
|
return `
|
|
|
|
import { defineNuxtPlugin, useRuntimeConfig } from '#imports';
|
|
|
|
${registered.config.map((config) => `import ${config.as} from '${config.from}';`).join('\n')}
|
|
|
|
${registered.services.map((service) => `import ${service.as} from '${service.from}';`).join('\n')}
|
|
|
|
${registered.directives.map((directive) => `import ${directive.as} from '${directive.from}';`).join('\n')}
|
|
|
|
${importPT ? `import ${importPT.as} from '${importPT.from}';\n` : ''}
|
2024-03-05 09:22:33 +00:00
|
|
|
${importTheme ? `import ${importTheme.as} from '${importTheme.from}';\n` : ''}
|
2023-10-24 10:09:12 +00:00
|
|
|
|
|
|
|
export default defineNuxtPlugin(({ vueApp }) => {
|
|
|
|
const runtimeConfig = useRuntimeConfig();
|
|
|
|
const config = runtimeConfig?.public?.primevue ?? {};
|
|
|
|
const { usePrimeVue = true, options = {} } = config;
|
|
|
|
const pt = ${importPT ? `{ pt: ${importPT.as} }` : `{}`};
|
2024-03-05 09:22:33 +00:00
|
|
|
const theme = ${importTheme ? `{ theme: ${importTheme.as} }` : `{}`};
|
2023-10-24 10:09:12 +00:00
|
|
|
|
2024-03-05 09:22:33 +00:00
|
|
|
usePrimeVue && vueApp.use(PrimeVue, { ...options, ...pt, ...theme });
|
2023-10-24 10:09:12 +00:00
|
|
|
${registered.services.map((service) => `vueApp.use(${service.as});`).join('\n')}
|
|
|
|
${registered.directives.map((directive) => `vueApp.directive('${directive.name}', ${directive.as});`).join('\n')}
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
nuxt.hook('nitro:config', async (config) => {
|
|
|
|
config.externals = config.externals || {};
|
|
|
|
config.externals.inline = config.externals.inline || [];
|
|
|
|
config.externals.inline.push(resolver.resolve('./runtime/plugin.server'));
|
|
|
|
config.virtual = config.virtual || {};
|
|
|
|
config.virtual['#primevue-style'] = styleContent;
|
|
|
|
config.plugins = config.plugins || [];
|
|
|
|
config.plugins.push(resolver.resolve('./runtime/plugin.server'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|