| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import { Plugin } from 'vite';
- import { CompileResult } from './utils/compiler.js';
- import { getDashboardPaths } from './utils/get-dashboard-paths.js';
- import { ConfigLoaderApi, getConfigLoaderApi } from './vite-plugin-config-loader.js';
- /**
- * This Vite plugin transforms the `app/styles.css` file to include a `@source` directive
- * for each dashboard extension's source directory. This allows Tailwind CSS to
- * include styles from these extensions when processing the CSS.
- */
- export function dashboardTailwindSourcePlugin(): Plugin {
- let configLoaderApi: ConfigLoaderApi;
- let loadVendureConfigResult: CompileResult;
- return {
- name: 'vendure:dashboard-tailwind-source',
- // Ensure this plugin runs before Tailwind CSS processing
- enforce: 'pre',
- configResolved({ plugins }) {
- configLoaderApi = getConfigLoaderApi(plugins);
- },
- async transform(src, id) {
- if (/app\/styles.css$/.test(id)) {
- if (!loadVendureConfigResult) {
- loadVendureConfigResult = await configLoaderApi.getVendureConfig();
- }
- const { pluginInfo } = loadVendureConfigResult;
- const dashboardExtensionDirs = getDashboardPaths(pluginInfo);
- const sources = dashboardExtensionDirs
- .map(extension => {
- return `@source '${extension}';`;
- })
- .join('\n');
- // Find the line with the specific comment and insert sources after it
- const lines = src.split('\n');
- const sourceCommentIndex = lines.findIndex(line =>
- line.includes(
- '/* @source rules from extensions will be added here by the dashboardTailwindSourcePlugin */',
- ),
- );
- if (sourceCommentIndex !== -1) {
- // Insert the sources after the comment line
- lines.splice(sourceCommentIndex + 1, 0, sources);
- const modifiedSrc = lines.join('\n');
- return {
- code: modifiedSrc,
- };
- }
- // If the comment is not found, append sources at the end
- return {
- code: src + '\n' + sources,
- };
- }
- },
- };
- }
|