diff --git a/build/move-md-plugin.ts b/build/move-md-plugin.ts new file mode 100644 index 0000000..1cff4d8 --- /dev/null +++ b/build/move-md-plugin.ts @@ -0,0 +1,47 @@ +import fs from 'fs'; +import path from 'path'; +const SRC_DIR = path.resolve(process.cwd(), 'src'); +const PUBLIC_MD_DIR = path.resolve(process.cwd(), 'public/md'); + +const moveMarkdownFiles = (dir: string) => { + // 获取指定目录下的文件 + const files = fs.readdirSync(dir); + // 遍历文件 + files.forEach((file) => { + const filePath = path.join(dir, file); + const stat = fs.statSync(filePath); + + if (stat.isDirectory()) { + // 如果是目录,递归调用 + moveMarkdownFiles(filePath); + } else if (file.endsWith('.md')) { + // 如果是文件,且是.md文件,复制到public/md目录下 + const distPath = path.join(PUBLIC_MD_DIR, path.basename(filePath)); + + // 检查目标文件是否存在 + if (!fs.existsSync(distPath)) { + fs.copyFileSync(filePath, distPath); + console.log(`自定义vite插件,复制 ${filePath} 到 ${distPath}`); + } + } + }); +}; + +export default function () { + // 确保public/md目标目录存在 + if (!fs.existsSync(PUBLIC_MD_DIR)) { + fs.mkdirSync(PUBLIC_MD_DIR, { recursive: true }); + } + + return { + name: 'move-md-plugin', + // 开发服务器启动时执行 + // configureServer: (server) => { + // moveMarkdownFiles(SRC_DIR); + // }, + // 打包时执行(开发模式下也会执行,所以不需要configureServer) + buildStart: () => { + moveMarkdownFiles(SRC_DIR); + }, + }; +} diff --git a/build/plugins.ts b/build/plugins.ts index 2a089e1..7d2aa69 100644 --- a/build/plugins.ts +++ b/build/plugins.ts @@ -14,6 +14,7 @@ import removeConsole from 'vite-plugin-remove-console'; import { themePreprocessorPlugin } from '@pureadmin/theme'; import { genScssMultipleScopeVars } from '../src/layout/theme'; import { vitePluginFakeServer } from 'vite-plugin-fake-server'; +import moveMdPlugin from './move-md-plugin'; export function getPluginsList(VITE_CDN: boolean, VITE_COMPRESSION: ViteCompression): PluginOption[] { const lifecycle = process.env.npm_lifecycle_event; @@ -53,5 +54,7 @@ export function getPluginsList(VITE_CDN: boolean, VITE_COMPRESSION: ViteCompress removeConsole({ external: ['src/assets/iconfont/iconfont.js'] }), // 打包分析 lifecycle === 'report' ? visualizer({ open: true, brotliSize: true, filename: 'report.html' }) : (null as any), + // 移动src下的md文件到public/md目录 + moveMdPlugin(), ]; } diff --git a/public/md/css-module.md b/public/md/css-module.md new file mode 100644 index 0000000..aac5e5f --- /dev/null +++ b/public/md/css-module.md @@ -0,0 +1,55 @@ +CSS Modules 是一种局部样式的解决方案,确保样式只作用于特定组件,而不会影响全局。这是通过以下机制和原理来实现的: + +### 1. 局部样式的概念 + +CSS Modules 的设计目的是将 CSS 类名局部化,从而避免样式冲突。在使用 CSS Modules 时,类名会被自动生成唯一的标识符,以确保它们只在定义它们的组件中有效。 + +### 2. 如何实现局部样式 + +#### 生成唯一类名 + +当您使用 CSS Modules 时,每个类名都会被编译成一个唯一的名称。例如,如果您在 `styles.module.css` 中定义了一个类 `.item`,在编译后,它可能会被转换为类似 `styles_item__1A2B3` 的名称。这样,其他组件中相同的类名不会产生冲突。 + +#### 使用方式 + +在 Vue 3 中使用 CSS Modules 时,您需要通过 `import` 语句导入 CSS Module 文件,并通过导入的对象引用样式。这确保了样式只与特定组件关联。 + +```javascript +import styles from './styles.module.css'; // 导入 CSS Module + +