From 2582162cea2b3a59cd21c78f8b73cb03d0acad40 Mon Sep 17 00:00:00 2001 From: Pauline Date: Sat, 14 Oct 2023 22:27:27 -0400 Subject: refactor(trunk): refactor the entire project idk --- packages/config/vite/index.ts | 27 ++++++++++++++ packages/config/vite/relAlias.ts | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 packages/config/vite/index.ts create mode 100644 packages/config/vite/relAlias.ts (limited to 'packages/config/vite') diff --git a/packages/config/vite/index.ts b/packages/config/vite/index.ts new file mode 100644 index 0000000..a9306a6 --- /dev/null +++ b/packages/config/vite/index.ts @@ -0,0 +1,27 @@ +import react from '@vitejs/plugin-react'; +import { defineConfig } from 'vite'; +import { createHtmlPlugin } from 'vite-plugin-html'; +import svg from 'vite-plugin-svgr'; +import tsconfigPaths from 'vite-tsconfig-paths'; + +export default defineConfig({ + plugins: [ + tsconfigPaths(), + react(), + svg({ svgrOptions: { icon: true } }), + createHtmlPlugin({ + minify: true + }) + ], + css: { + modules: { + localsConvention: 'camelCaseOnly' + } + }, + root: 'src', + build: { + sourcemap: true, + outDir: '../dist', + assetsDir: '.' + } +}); diff --git a/packages/config/vite/relAlias.ts b/packages/config/vite/relAlias.ts new file mode 100644 index 0000000..1a502b5 --- /dev/null +++ b/packages/config/vite/relAlias.ts @@ -0,0 +1,77 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { Alias } from 'vite'; + +const projectPath = path.resolve(__dirname, '../../../'); +const pkgJsonCache = new Map(); + +const SRC_DIR_PATH = `${path.sep}src${path.sep}`; + +const resolver: Alias = { + find: /^(~\/.+)/, + replacement: '$1', + async customResolver(source, importer) { + let root: null | string = null; + + if (importer) importer = path.normalize(importer); + + const [_, sourcePath] = source.split('~/'); + + const relativeImporter = importer?.replace(projectPath, ''); + if (relativeImporter && relativeImporter.includes(SRC_DIR_PATH)) { + const [pkg] = relativeImporter.split(SRC_DIR_PATH); + root = path.join(projectPath, pkg, 'src'); + } else if (importer) { + const pathObj = path.parse(importer); + + let parent = pathObj.dir; + while (parent !== pathObj.root) { + parent = path.dirname(parent); + + let hasPkgJson = pkgJsonCache.get(parent); + + if (hasPkgJson === undefined) + try { + await fs.stat(path.join(parent, 'package.json')); + pkgJsonCache.set(parent, (hasPkgJson = true)); + } catch { + pkgJsonCache.set(parent, (hasPkgJson = false)); + } + + if (hasPkgJson) { + root = parent; + break; + } + } + + if (root === null) + throw new Error(`Failed to resolve import path ${source} in file ${importer}`); + } else { + throw new Error(`Failed to resolve import path ${source} in file ${importer}`); + } + + const absolutePath = path.join(root, sourcePath); + + const folderItems = await fs.readdir(path.join(absolutePath, '..')); + + const item = folderItems.find((i) => i.startsWith(sourcePath.split('/').at(-1)!))!; + + const fullPath = absolutePath + path.extname(item); + + const stats = await fs.stat(fullPath); + + if (stats.isDirectory()) { + const directoryItems = await fs.readdir(absolutePath + path.extname(item)); + + const indexFile = directoryItems.find((i) => i.startsWith('index')); + if (!indexFile) + throw new Error(`Failed to resolve import path ${source} in file ${importer}`); + + return path.join(absolutePath, indexFile); + } else { + return fullPath; + } + } +}; + +export default resolver; -- cgit