Skip to content

Commit

Permalink
feat: 生成的scss使用@use@forward ,弃用@import
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanStrangeHelium committed Dec 25, 2024
1 parent 9a80bc2 commit a605d65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion scripts/generate-nutui-taro-vue.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ config.nav.map((item) => {
}.vue'\n`
}
}
importScssStr += `import './__VUE/${name.toLowerCase()}/index.scss'\n`
importScssStr += `@use './__VUE/${name.toLowerCase()}/index.scss' as ${name.toLowerCase()}\n`
if (exclude !== true) {
packages.push(name)
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-nutui.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ config.nav.map((item) => {
methods.push(`show${name}`)
}
}
importScssStr += `import './__VUE/${name.toLowerCase()}/index.scss'\n`
importScssStr += `@use './__VUE/${name.toLowerCase()}/index.scss' as ${name.toLowerCase()}\n`
if (exclude != true) {
packages.push(name)
}
Expand Down
26 changes: 14 additions & 12 deletions scripts/generate-themes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ const sass = require('sass')

let sassFileStr = ``
let tasks = []
if (!target) {
sassFileStr += `@import '@nutui/icons-vue/dist/style_icon.css';\n`
}

config.nav.map((item) => {
item.packages.forEach((element) => {
let folderName = element.name.toLowerCase()
if (element.exclude != true) {
sassFileStr += `@import '../../packages/${folderName}/index.scss';\n`
sassFileStr += `@use '../../packages/${folderName}/index.scss' as ${folderName};\n`
}
tasks.push(
fs
Expand All @@ -27,6 +25,10 @@ config.nav.map((item) => {
)
})
})
// use指令必须在前面,否则会报错
if (!target) {
sassFileStr += `@import '@nutui/icons-vue/dist/style_icon.css';\n`
}

tasks.push(fs.copy(path.resolve(__dirname, '../src/packages/styles'), path.resolve(__dirname, '../dist/styles')))

Expand All @@ -49,7 +51,7 @@ const sassTocss = () => {
// 写入main.scss,引入变量文件variables.scss和组件样式index.scss
fs.outputFile(
filePath,
`@import '../../styles/variables.scss';\n@import './index.scss';\n`,
`@use '../../styles/variables.scss' as vars;\n@use './index.scss';\n`,
'utf8',
(error) => {
if (error) return console.error(error)
Expand Down Expand Up @@ -101,11 +103,11 @@ const parseFile = (filename, theme = 'default') => {
}
})

let fileContent = `@import './${themesEnum[theme]}.scss';\n:root {\n`
let fileContent = `@use './${themesEnum[theme]}.scss' as vars;\n:root {\n`
for (const key in variables) {
if (Object.prototype.hasOwnProperty.call(variables, key)) {
const variableName = key.slice(1)
fileContent += ` --nut-${variableName}: #{$${variableName}};\n`
fileContent += ` --nut-${variableName}: #{vars.$${variableName}};\n` // 使用 vars.$variableName
}
}
fileContent += `}`
Expand Down Expand Up @@ -138,10 +140,10 @@ const variablesResolver = () => {

Promise.all(tasks).then(() => {
let themes = [
{ file: 'default.scss', sourcePath: `@import '../variables.scss';` },
{ file: 'jdt.scss', sourcePath: `@import '../variables-jdt.scss';` },
{ file: 'jdb.scss', sourcePath: `@import '../variables-jdb.scss';` },
{ file: 'jddkh.scss', sourcePath: `@import '../variables-jddkh.scss';` }
{ file: 'default.scss', sourcePath: `@use '../variables.scss' as vars;` },
{ file: 'jdt.scss', sourcePath: `@use '../variables-jdt.scss' as vars;` },
{ file: 'jdb.scss', sourcePath: `@use '../variables-jdb.scss' as vars;` },
{ file: 'jddkh.scss', sourcePath: `@use '../variables-jddkh.scss' as vars;` }
]
tasks = []
themes.forEach((item) => {
Expand All @@ -161,4 +163,4 @@ Promise.all(tasks).then(() => {
sassTocss()
variablesResolver()
})
})
})

0 comments on commit a605d65

Please sign in to comment.