-
Notifications
You must be signed in to change notification settings - Fork 289
refactor: 打包组件不同主题的 css 文件 #3189
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8504c5a
refactor: css 文件拆分
oasis-cloud 3703929
refactor: 主题拆分
oasis-cloud c1cb2b7
refactor: 主题拆分
oasis-cloud 8468ae2
docs: 主题使用的文档更新
oasis-cloud a78f8ae
chore: sideEffects 优化
oasis-cloud 8b68016
docs: 更新文档
oasis-cloud 5f28765
feat: 构建 jrkf 主题文件
oasis-cloud 9a86dfa
chore: 更新模板的跳转路径
oasis-cloud 4e1c313
chore: jrfk 变量补齐
oasis-cloud c4c7ca1
chore: time
oasis-cloud 9be4069
fix: review
oasis-cloud 0106866
fix: review
oasis-cloud ff354d5
fix: review
oasis-cloud 1d33edf
fix: review
oasis-cloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,44 @@ | ||
| module.exports = { | ||
| webpack: { | ||
| module: { | ||
| rules: [ | ||
| { | ||
| test: /\.s[ac]ss$/i, | ||
| use: [ | ||
| 'style-loader', | ||
| 'css-loader', | ||
| { | ||
| loader: "sass-loader", | ||
| options: { | ||
| additionalData: `@import '@nutui/nutui-react/dist/styles/variables.scss';` | ||
| // JDesign 主题 | ||
| // additionalData: `@import '@nutui/nutui-react/dist/styles/variables-jmapp.scss';` | ||
| // JRKF 主题 | ||
| // additionalData: `@import '@nutui/nutui-react/dist/styles/variables-jrkf.scss';` | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| babel: { | ||
| plugins: [ | ||
| [ | ||
| "import", | ||
| 'import', | ||
| { | ||
| "libraryName": "@nutui/nutui-react", | ||
| "libraryDirectory": "dist/esm", | ||
| "style": "css", | ||
| "camel2DashComponentName": false | ||
| libraryName: '@nutui/nutui-react', | ||
| camel2DashComponentName: false, | ||
| customName: (name, file) => { | ||
| return `@nutui/nutui-react/dist/es/packages/${name.toLowerCase()}` | ||
| }, | ||
| // 自动加载 scss 样式文件 | ||
| customStyleName: (name) => `@nutui/nutui-react/dist/es/packages/${name.toLowerCase()}/style` | ||
| // 自动加载 css 样式文件 | ||
| // customStyleName: (name) => `@nutui/nutui-react/dist/es/packages/${name.toLowerCase()}/style` | ||
| }, | ||
| "nutui-react" | ||
| ] | ||
| 'nutui-react', | ||
| ], | ||
| ] | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,26 +244,28 @@ async function buildUMD() { | |
| }) | ||
| } | ||
|
|
||
| async function buildAllCSS() { | ||
| // 针对不同主题构建全量的 style | ||
| async function buildAllCSS(themeName = '') { | ||
| // 拷贝styles | ||
| const themeStylePath = themeName ? `style-${themeName}` : 'style' | ||
| async function generateAllStyles() { | ||
| const projectID = process.env.VITE_APP_PROJECT_ID | ||
| const content = [ | ||
| `@import './styles/variables${projectID ? `-${projectID}` : ''}.scss';`, | ||
| `@import './styles/variables${themeName ? `-${themeName}` : ''}.scss';`, | ||
| `@import './styles/mixins/index.scss';`, | ||
| `@import './styles/animation/index.scss';`, | ||
| ] | ||
| const scssFiles = await glob([`${dist}/es/packages/**/*.scss`]) | ||
| const scssFiles = await glob([`${dist}/es/packages/**/${themeStylePath}/*.scss`]) | ||
| scssFiles.forEach((file) => { | ||
| content.push( | ||
| `@import '${relativePath('/' + file, `/${dist}/style.scss`)}';`, | ||
| `@import '${relativePath('/' + file, `/${dist}/${themeStylePath}.scss`)}';`, | ||
| ) | ||
| }) | ||
| dest(`${dist}/style.scss`, content.join('\n')) | ||
| await dest(`${dist}/${themeStylePath}.scss`, content.join('\n')) | ||
| } | ||
|
|
||
| await generateAllStyles() | ||
| await vite.build({ | ||
| configFile: false, | ||
| logLevel: 'error', | ||
| resolve: { | ||
| alias: [{ find: '@', replacement: resolve(__dirname, '../src') }], | ||
|
|
@@ -272,11 +274,16 @@ async function buildAllCSS() { | |
| emptyOutDir: false, | ||
| outDir: dist, | ||
| lib: { | ||
| entry: `./${dist}/style.scss`, | ||
| entry: `./${dist}/${themeStylePath}.scss`, | ||
| formats: ['es'], | ||
| name: 'style', | ||
| fileName: 'style', | ||
| }, | ||
| rollupOptions: { | ||
| output: { | ||
| assetFileNames: `${themeStylePath}.css`, // 资源文件名 | ||
| } | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
|
|
@@ -287,6 +294,11 @@ async function buildThemeCSS() { | |
| }) | ||
| const projectID = process.env.VITE_APP_PROJECT_ID | ||
| const inputFiles = {} | ||
| // nuitui 官方包包含全部主题文件,包括: | ||
| // default.css 默认明亮主题 | ||
| // dark.css 默认暗黑主题 | ||
| // jmapp.css、jrkf.css 主题 | ||
| // 例如:jmapp 包只包含 jmapp 的主题文件,且是默认主题文件。 | ||
| files.forEach(filePath => { | ||
| const themeName = basename(filePath, 'scss').replace('theme-', '') | ||
| if (!projectID) { | ||
|
|
@@ -331,16 +343,16 @@ async function copyStyles() { | |
| } | ||
|
|
||
| // 构建样式 | ||
| async function buildCSS(p) { | ||
| const cssFiles = await glob(['src/packages/**/*.scss'], { | ||
| async function buildCSS(themeName = '') { | ||
| const componentScssFiles = await glob(['src/packages/**/*.scss'], { | ||
|
Comment on lines
345
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 在 Sass 编译过程中添加错误处理日志。 当前仅调用 |
||
| ignore: ['src/packages/**/demo.scss'], | ||
| }) | ||
|
|
||
| const variables = await readFile( | ||
| join(__dirname, '../src/styles/variables.scss'), | ||
| join(__dirname, `../src/styles/variables${themeName ? `-${themeName}` : ''}.scss`), | ||
| ) | ||
| for (const file of cssFiles) { | ||
| const button = await readFile(join(__dirname, '../', file), { | ||
| for (const file of componentScssFiles) { | ||
| const scssContent = await readFile(join(__dirname, '../', file), { | ||
| encoding: 'utf8', | ||
| }) | ||
| // countup 是特例 | ||
|
|
@@ -350,30 +362,11 @@ async function buildCSS(p) { | |
| '../src/packages', | ||
| base.replace('.scss', ''), | ||
| ) | ||
| const code = sass.compileString(variables + '\n' + button, { | ||
| loadPaths: [loadPath], | ||
| }) | ||
| const cssPath = relative('src', loadPath) | ||
| // 写 css 文件 | ||
| await dest(join(`${dist}/es`, cssPath, 'style/style.css'), code.css) | ||
| await dest(join(`${dist}/es`, cssPath, 'style/css.js'), `import './style.css'`) | ||
|
|
||
| await dest(join(`${dist}/cjs`, cssPath, 'style/style.css'), code.css) | ||
| await dest( | ||
| join(`${dist}/cjs`, cssPath, 'style/css.js'), | ||
| `import './style.css'`, | ||
| ) | ||
|
|
||
| // copy harmonycss | ||
| if (file.indexOf('countup') === -1) { | ||
| await copy(join(__dirname, '../', file.replace('scss', 'harmony.css')), join(`${dist}/cjs`, cssPath, 'style/style.harmony.css')) | ||
| await copy(join(__dirname, '../', file.replace('scss', 'harmony.css')), join(`${dist}/es`, cssPath, 'style/style.harmony.css')) | ||
| } | ||
|
|
||
| // 删除 import | ||
| // 写入 style.scss | ||
| const atRules = [] | ||
| await postcss([ | ||
| const postcssRes = await postcss([ | ||
| { | ||
| postcssPlugin: 'remove-atrule', | ||
| AtRule(root) { | ||
|
|
@@ -391,13 +384,22 @@ async function buildCSS(p) { | |
| }, | ||
| }, | ||
| ]) | ||
| .process(button, { from: loadPath, syntax: scss }) | ||
| .process(scssContent, { from: loadPath, syntax: scss }) | ||
| .then((result) => { | ||
| dest(join(`${dist}/es`, cssPath, `style/${base}`), result.css) | ||
| dest(join(`${dist}/cjs`, cssPath, `style/${base}`), result.css) | ||
| return result | ||
| }) | ||
| const themeDir = themeName ? `style-${themeName}` : 'style' | ||
| await dest(join(`${dist}/es`, cssPath, `${themeDir}/${base}`), postcssRes.css) | ||
| await dest(join(`${dist}/cjs`, cssPath, `${themeDir}/${base}`), postcssRes.css) | ||
|
|
||
| const code = sass.compileString(variables + '\n' + postcssRes.css.replaceAll('../../../../', '../../'), { | ||
| loadPaths: [loadPath], | ||
| }) | ||
| await dest(join(`${dist}/es`, cssPath, `${themeDir}/style.css`), code.css) | ||
| await dest(join(`${dist}/cjs`, cssPath, `${themeDir}/style.css`), code.css) | ||
|
|
||
| const jsContent = [] | ||
| const cssContent = [] | ||
| atRules.forEach((rule) => { | ||
| rule = rule.replaceAll('\'', '') | ||
| if (rule.indexOf('../styles/') > -1) { | ||
|
|
@@ -407,18 +409,33 @@ async function buildCSS(p) { | |
| const base = basename(rule) | ||
| const ext = extname(base) | ||
| const name = base.replace(ext, '') | ||
| jsContent.push(`import '../../${name}/style';`) | ||
| jsContent.push(`import '../../${name}/${themeDir}';`) | ||
| cssContent.push(`import '../../${name}/${themeDir}/css';`) | ||
| } | ||
| }) | ||
| jsContent.push(`import './${base}';`) | ||
| cssContent.push(`import './style.css';`) | ||
|
|
||
| await dest( | ||
| join(`${dist}/cjs`, cssPath, `style/index.js`), | ||
| join(`${dist}/cjs`, cssPath, `${themeDir}/index.js`), | ||
| jsContent.join('\n'), | ||
| ) | ||
| await dest(join(`${dist}/es`, cssPath, `style/index.js`), jsContent.join('\n')) | ||
| } | ||
| await dest(join(`${dist}/es`, cssPath, `${themeDir}/index.js`), jsContent.join('\n')) | ||
|
|
||
| // 写 css 文件 | ||
| await dest(join(`${dist}/es`, cssPath, `${themeDir}/css.js`), cssContent.join('\n')) | ||
| await dest( | ||
| join(`${dist}/cjs`, cssPath, `${themeDir}/css.js`), | ||
| cssContent.join('\n'), | ||
| ) | ||
|
|
||
| // copy harmonycss | ||
| if (file.indexOf('countup') === -1) { | ||
| const harmonyCss = join(__dirname, '../', file.replace('scss', 'harmony.css')) | ||
| await copy(harmonyCss, join(`${dist}/cjs`, cssPath, 'style/style.harmony.css')) | ||
| await copy(harmonyCss, join(`${dist}/es`, cssPath, 'style/style.harmony.css')) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function generateReleasePackageJson() { | ||
|
|
@@ -474,18 +491,36 @@ console.time('build UMD') | |
| await buildUMD() | ||
| console.timeEnd('build UMD') | ||
|
|
||
| console.time('Build CSS') | ||
| await buildCSS() | ||
| console.timeEnd('Build CSS') | ||
|
|
||
| console.time('Copy Styles') | ||
| await copyStyles() | ||
| console.timeEnd('Copy Styles') | ||
|
|
||
| console.time('Build CSS') | ||
| await buildCSS() | ||
| console.timeEnd('Build CSS') | ||
|
|
||
| console.time('Build jmapp CSS') | ||
| await buildCSS('jmapp') | ||
| console.timeEnd('Build jmapp CSS') | ||
|
|
||
| console.time('Build jrkf CSS') | ||
| await buildCSS('jrkf') | ||
| console.timeEnd('Build jrkf CSS') | ||
|
|
||
| console.time('Build All CSS') | ||
| await buildAllCSS() | ||
| console.timeEnd('Build All CSS') | ||
|
|
||
| console.time('Build All jrkf CSS') | ||
| await buildAllCSS('jrkf') | ||
| console.timeEnd('Build All jrkf CSS') | ||
|
|
||
| console.time('Build All jmapp CSS') | ||
| await buildAllCSS('jmapp') | ||
| console.timeEnd('Build All jmapp CSS') | ||
|
|
||
|
|
||
| console.time('Build Theme CSS') | ||
| await buildThemeCSS() | ||
| console.timeEnd('Build Theme CSS') | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
更新和添加依赖以支持多主题构建
添加了sass-embedded依赖并更新了TypeScript和Vite版本,以支持多主题CSS文件的构建和处理。这些更新对于实现主题变量的预处理和动态样式路径解析非常必要。
请确认这些依赖版本是否与项目中的其他模块兼容,特别是升级到Vite 6可能需要确保所有插件都支持新版本。
🏁 Script executed:
Length of output: 10386
请更新 Vite 相关插件以支持 Vite 6 版本
在
packages/nutui-templates/vite-demo/package.json中已将 Vite 升级至^6.0.5,但通过npm ls | grep vite验证发现项目实际安装的 Vite 版本仍为5.4.11,并且存在多项未满足依赖,可能导致构建失败或插件不兼容:请针对以下几点进行修正:
^5.x);npm ls | grep vite,确保无 UNMET DEPENDENCY 报错。