Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added multi entryfiles for content-ui + internalize tailwindcss bundle #749

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/hmr/lib/plugins/tailwind-bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Plugin } from 'vite';
import fs from 'fs/promises';
import path from 'path';
import postcss from 'postcss';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
import postcssImport from 'postcss-import';

export function processCssPlugin(): Plugin {
return {
name: 'process-css',
async configResolved(config) {
const inputFile = path.resolve(__dirname, '../../../../ui/lib/global.css');
const outputFile = path.resolve(__dirname, '../../../../ui/dist/global-out.css');

const css = await fs.readFile(inputFile, 'utf-8');
const result = await postcss([
postcssImport(),
tailwindcss,
autoprefixer,
]).process(css, { from: inputFile, to: outputFile });
await fs.mkdir(path.dirname(outputFile), { recursive: true });

await fs.writeFile(outputFile, result.css);
console.log('CSS processed and saved to', outputFile);
}
};
}
56 changes: 52 additions & 4 deletions pages/content-ui/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@
import { resolve } from 'node:path';
import { makeEntryPointPlugin } from '@extension/hmr';
import { isDev, withPageConfig } from '@extension/vite-config';
import { readdirSync, existsSync, mkdirSync, writeFileSync, Dirent } from 'fs';
import { processCssPlugin } from '@extension/hmr';

const rootDir = resolve(__dirname);
const srcDir = resolve(rootDir, 'src');

const getEntryPoints = (options: { include?: string[], exclude?: string[], createRecursively?: boolean }) => {
const entryPoints: Record<string, string> = {};

const foldersToProcess = options.include ||
readdirSync(srcDir, { withFileTypes: true })
.filter((item): item is Dirent => item.isDirectory())
.map(item => item.name);

foldersToProcess.forEach(folderName => {
const shouldExclude = options.exclude && options.exclude.includes(folderName);

if (!shouldExclude) {
const entryFolder = resolve(srcDir, folderName);
const entryFileTS = resolve(entryFolder, 'index.ts');
const entryFileTSX = resolve(entryFolder, `${folderName}.tsx`);

if (!existsSync(entryFolder) && options.createRecursively) {
console.log('Creating folder', entryFolder);
mkdirSync(entryFolder, { recursive: true });
writeFileSync(entryFileTS, `export * from "./${folderName}.tsx";`);
writeFileSync(entryFileTSX, '');
}

if (existsSync(entryFolder)) {
entryPoints[folderName] = entryFileTS;
} else {
console.warn(`Folder "${folderName}" does not exist and was not created.`);
}
}
});

return entryPoints;
}
/**
* Here is the place to add new entry points, u can straight away add new folders here to include
* without creating folders in src dir manually.
* @param {string[]} options.include - Folders to include !!MODIFY ME!!
* @param {string[]} options.exclude - Folders to exclude !!MODIFY ME!!
* @param {boolean} options.createRecursively - Create folders recursively
* @returns {Record<string, string>}
*/
const entryPoints = getEntryPoints({
include: ['page1', 'page2'], createRecursively: true
});

export default withPageConfig({
resolve: {
alias: {
'@src': srcDir,
},
},
plugins: [isDev && makeEntryPointPlugin()],
plugins: [processCssPlugin()],
publicDir: resolve(rootDir, 'public'),
build: {
lib: {
entry: resolve(srcDir, 'index.tsx'),
entry: entryPoints,
name: 'contentUI',
formats: ['iife'],
fileName: 'index',
formats: ['es'],
fileName: (format, entryName) =>
isDev ? `${entryName}/index_dev.js` : `${entryName}/index.js`
},
outDir: resolve(rootDir, '..', '..', 'dist', 'content-ui'),
},
Expand Down