From 2ae105213788ab06a7848cdc58425ff742304b30 Mon Sep 17 00:00:00 2001 From: tngflx Date: Tue, 17 Sep 2024 20:28:41 +0800 Subject: [PATCH] added multi entryfiles for content-ui + internalize tailwindcss bundle output --- packages/hmr/lib/plugins/tailwind-bundler.ts | 28 ++++++++++ pages/content-ui/vite.config.mts | 56 ++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 packages/hmr/lib/plugins/tailwind-bundler.ts diff --git a/packages/hmr/lib/plugins/tailwind-bundler.ts b/packages/hmr/lib/plugins/tailwind-bundler.ts new file mode 100644 index 000000000..f48dc9b7b --- /dev/null +++ b/packages/hmr/lib/plugins/tailwind-bundler.ts @@ -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); + } + }; +} \ No newline at end of file diff --git a/pages/content-ui/vite.config.mts b/pages/content-ui/vite.config.mts index 21570149c..a96c8e87c 100644 --- a/pages/content-ui/vite.config.mts +++ b/pages/content-ui/vite.config.mts @@ -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 = {}; + + 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} + */ +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'), },