-
Notifications
You must be signed in to change notification settings - Fork 284
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
refactor(theme-tool): Reimplementing the theme-tool method #2369
Changes from 5 commits
46ea9ed
73c9891
b1e6e75
9a61f5b
b025277
4684771
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ function concatIndex(cb) { | |
} | ||
// 2、拼接所有组件的 old-theme.less 到一起 old-theme-index.less | ||
function concatOldTheme(cb) { | ||
_concatFiles('../src/*/old-theme.less', '../src/old-theme.less') | ||
_concatFiles('../src/*/old-theme.less', '../src/old-theme-index.less') | ||
cb() | ||
} | ||
// 3、编译 | ||
|
@@ -41,7 +41,7 @@ gulp.task('compile', () => { | |
.src([ | ||
`${source}/**/index.less`, // 编译默认主题 | ||
`${source}/index.less`, | ||
`${source}/old-theme.less` // 编译旧主题 | ||
`${source}/old-theme-index.less` // 编译旧主题 | ||
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 Consider adding source maps for better debugging. While the path update is correct, the build process could benefit from source maps to aid debugging in development. Add source map generation to the build pipeline: gulp.task('compile', () => {
return gulp
.src([
`${source}/**/index.less`,
`${source}/index.less`,
`${source}/old-theme-index.less`
])
+ .pipe(sourcemaps.init())
.pipe(svgInline(svgInlineOption))
.pipe(less())
.pipe(prefixer({
borwsers: ['last 1 version', '> 1%', 'not ie <= 8'],
cascade: true,
remove: true
}))
.pipe(svgInline(svgInlineOption))
.pipe(cssmin())
+ .pipe(sourcemaps.write('./'))
.pipe(gulp.dest(dist))
}) This would require adding import sourcemaps from 'gulp-sourcemaps' |
||
]) | ||
.pipe(svgInline(svgInlineOption)) | ||
.pipe(less()) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,31 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
import fs from 'node:fs' | ||||||||||||||||||||||||||||||||||||||||||||||
import path from 'node:path' | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// 复制package.json/README.md到dist目录 | ||||||||||||||||||||||||||||||||||||||||||||||
const root = path.resolve('./') | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// 1、复制 theme-tool.js /README.md 到dist目录 | ||||||||||||||||||||||||||||||||||||||||||||||
fs.copyFileSync('README.md', path.join('dist', 'README.md')) | ||||||||||||||||||||||||||||||||||||||||||||||
fs.copyFileSync('src/theme-tool.js', path.join('dist', 'theme-tool.js')) | ||||||||||||||||||||||||||||||||||||||||||||||
fs.copyFileSync('src/theme-tool.d.ts', path.join('dist', 'theme-tool.d.ts')) | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+6
to
+9
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. Add error handling for file operations. The synchronous file operations could throw errors if source files don't exist. Consider adding try-catch blocks and file existence checks. -fs.copyFileSync('README.md', path.join('dist', 'README.md'))
-fs.copyFileSync('src/theme-tool.js', path.join('dist', 'theme-tool.js'))
-fs.copyFileSync('src/theme-tool.d.ts', path.join('dist', 'theme-tool.d.ts'))
+const filesToCopy = [
+ ['README.md', 'dist/README.md'],
+ ['src/theme-tool.js', 'dist/theme-tool.js'],
+ ['src/theme-tool.d.ts', 'dist/theme-tool.d.ts']
+];
+
+filesToCopy.forEach(([src, dest]) => {
+ try {
+ if (!fs.existsSync(src)) {
+ throw new Error(`Source file ${src} does not exist`);
+ }
+ fs.copyFileSync(src, dest);
+ } catch (error) {
+ console.error(`Failed to copy ${src} to ${dest}:`, error);
+ process.exit(1);
+ }
+}); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
const root = path.resolve('./') | ||||||||||||||||||||||||||||||||||||||||||||||
const content = await fs.promises.readFile(path.resolve(root, 'package.json'), 'utf8') | ||||||||||||||||||||||||||||||||||||||||||||||
// 2、读取 old-theme-index.js , dist/old-theme-index.less, 合并后写入 dist/ old-theme-index.js | ||||||||||||||||||||||||||||||||||||||||||||||
let jsStr = ` | ||||||||||||||||||||||||||||||||||||||||||||||
export default { | ||||||||||||||||||||||||||||||||||||||||||||||
id: 'tiny-old-theme', | ||||||||||||||||||||||||||||||||||||||||||||||
name: 'OldTheme', | ||||||||||||||||||||||||||||||||||||||||||||||
cnName: '旧的主题', | ||||||||||||||||||||||||||||||||||||||||||||||
css: \`#CSS#\` | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
` | ||||||||||||||||||||||||||||||||||||||||||||||
let cssStr = fs.readFileSync(path.resolve(root, 'dist/old-theme-index.css'), 'utf8') | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
jsStr = jsStr.replace('#CSS#', cssStr) | ||||||||||||||||||||||||||||||||||||||||||||||
fs.writeFileSync(path.resolve(root, 'src/old-theme-index.js'), jsStr) // 供开发时(pnpm site), 可以访问到最新的定制主题变量 | ||||||||||||||||||||||||||||||||||||||||||||||
fs.writeFileSync(path.resolve(root, 'dist/old-theme-index.js'), jsStr) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// 3、复制 package.json | ||||||||||||||||||||||||||||||||||||||||||||||
const content = fs.readFileSync(path.resolve(root, 'package.json'), 'utf8') | ||||||||||||||||||||||||||||||||||||||||||||||
const packageJson = JSON.parse(content) | ||||||||||||||||||||||||||||||||||||||||||||||
delete packageJson.exports | ||||||||||||||||||||||||||||||||||||||||||||||
delete packageJson.private | ||||||||||||||||||||||||||||||||||||||||||||||
await fs.promises.writeFile(path.resolve(root, 'dist/package.json'), JSON.stringify(packageJson, null, 2)) | ||||||||||||||||||||||||||||||||||||||||||||||
fs.writeFileSync(path.resolve(root, 'dist/package.json'), JSON.stringify(packageJson, null, 2)) | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+26
to
+31
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. Add error handling for package.json operations. While the static analysis flags the delete operator, its usage here is acceptable as this is a build script where performance is not critical, and we're modifying a parsed JSON object, not an object with prototypes. However, the code should handle potential errors: -const content = fs.readFileSync(path.resolve(root, 'package.json'), 'utf8')
-const packageJson = JSON.parse(content)
-delete packageJson.exports
-delete packageJson.private
-fs.writeFileSync(path.resolve(root, 'dist/package.json'), JSON.stringify(packageJson, null, 2))
+try {
+ const content = fs.readFileSync(path.resolve(root, 'package.json'), 'utf8')
+ const packageJson = JSON.parse(content)
+ delete packageJson.exports
+ delete packageJson.private
+ fs.writeFileSync(
+ path.resolve(root, 'dist/package.json'),
+ JSON.stringify(packageJson, null, 2)
+ )
+} catch (error) {
+ console.error('Failed to process package.json:', error)
+ process.exit(1)
+} 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,3 @@ | |
@import './reset.less'; | ||
@import './vars.less'; | ||
@import './transition.less'; | ||
@import '../svg/index.less'; |
Large diffs are not rendered by default.
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,14 +2,13 @@ interface ThemeData { | |||||||||
id: string | ||||||||||
name: string | ||||||||||
cnName: string | ||||||||||
/** 全局css变量覆盖的对象,比如: { 'tv-base-color-brand' : '#1476ff' }*/ | ||||||||||
data: object | ||||||||||
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 Consider using a more specific type for the The generic - data: object
+ data: Record<string, string> This change would:
📝 Committable suggestion
Suggested change
|
||||||||||
/** 需要追加的样式表 */ | ||||||||||
css: string | ||||||||||
} | ||||||||||
|
||||||||||
export default class TinyThemeTool { | ||||||||||
currentTheme: string | ||||||||||
contentElement: HTMLElement | ||||||||||
styleSheetId: string | ||||||||||
defaultTheme: object | ||||||||||
changeTheme: (theme: ThemeData) => void | ||||||||||
constructor(theme: ThemeData, styleSheetId: string) | ||||||||||
constructor(theme: ThemeData) | ||||||||||
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. 💡 Codebase verification Based on the gathered information, I can now generate the final response as there are clear issues that need to be addressed. Constructor parameter is required but not being provided The new
This will cause the constructor to fall back to the default theme, but it's better to explicitly provide the initial theme. Additionally, the implementation in Required changes:
🔗 Analysis chainLGTM! Verify theme-related functionality remains intact. The simplified class interface with better encapsulation looks good. The removal of implementation details from the public interface and the addition of the Let's verify that all theme-related functionality is properly handled: Let's gather more context about the usage patterns to ensure we have a complete picture. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check for any direct references to removed properties that might break
rg -l "styleSheetId|currentTheme|contentElement|defaultTheme" --type ts --type js
# Check for proper theme initialization patterns
ast-grep --pattern 'new TinyThemeTool($$$)'
Length of output: 604 Script: #!/bin/bash
# Check the content of files that initialize TinyThemeTool to see if they're passing required theme parameter
rg -A 5 -B 5 "new TinyThemeTool" examples/docs/newsrc/uses/useTheme.js examples/sites/src/tools/useTheme.js
# Check how the removed properties are being used in the files
rg "styleSheetId|currentTheme|contentElement|defaultTheme" --type ts --type js -C 3
Length of output: 14595 |
||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,37 @@ | ||||||||||||||
export default class TinyThemeTool {} | ||||||||||||||
import OldTheme from './old-theme-index.js' | ||||||||||||||
|
||||||||||||||
export { OldTheme } | ||||||||||||||
|
||||||||||||||
export default class TinyThemeTool { | ||||||||||||||
constructor(theme) { | ||||||||||||||
if (theme) { | ||||||||||||||
this.changeTheme(theme) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
changeTheme(theme) { | ||||||||||||||
if (this._loadedSheet) { | ||||||||||||||
document.adoptedStyleSheets = document.adoptedStyleSheets.filter((t) => t !== this._loadedSheet) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
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 Consider handling browsers that do not support The use of |
||||||||||||||
if (theme && (theme.data || theme.css)) { | ||||||||||||||
let cssContent = '' | ||||||||||||||
if (theme.data && typeof theme.data === 'object') { | ||||||||||||||
cssContent = Object.keys(theme.data) | ||||||||||||||
.map((key) => `--${key}: ${theme.data[key]}; `) | ||||||||||||||
.join('') | ||||||||||||||
|
||||||||||||||
Comment on lines
+27
to
+29
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. Sanitize theme data to prevent CSS injection vulnerabilities. Currently, the keys and values from Apply this diff to sanitize the keys and values: cssContent = Object.keys(theme.data)
- .map((key) => `--${key}: ${theme.data[key]}; `)
+ .map((key) => `--${CSS.escape(key)}: ${CSS.escape(theme.data[key])}; `)
.join(''); 📝 Committable suggestion
Suggested change
|
||||||||||||||
cssContent = `:root{${cssContent}}` | ||||||||||||||
} | ||||||||||||||
if (theme.css && typeof theme.css === 'string') { | ||||||||||||||
cssContent += theme.css | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Comment on lines
+33
to
+35
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. Ensure Appending |
||||||||||||||
const sheet = new CSSStyleSheet() | ||||||||||||||
sheet.replaceSync(cssContent) | ||||||||||||||
|
||||||||||||||
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet] | ||||||||||||||
this._loadedSheet = sheet | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
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.
💡 Codebase verification
Theme files have been relocated, not removed
The verification reveals that the theme-related files and directories have been moved from
packages/theme/
topackages/theme-saas/
:theme.json
exists atpackages/theme-saas/theme/theme.json
packages/theme-saas/src/theme/
This suggests a restructuring of theme-related code rather than removal. The
.gitignore
entries should be updated to reflect the new locations if these files still need to be ignored.Recommended actions:
.gitignore
entries to point to the new locations underpackages/theme-saas/
if these files should still be ignored🔗 Analysis chain
Verify removal of theme-related files from codebase.
The removal of these entries from
.gitignore
suggests these files are no longer part of the codebase. Let's verify this to prevent potential issues.Let me generate additional scripts to verify if the theme-related directories and files exist in any form within the repository, as the previous script showed that the specific paths don't exist but we should check if they were moved elsewhere.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 1473
Script:
Length of output: 884