Skip to content

Commit 1952e01

Browse files
committed
fix(compiler): add index file resolution for style paths in getAbsolutePath function
1 parent ed260a9 commit 1952e01

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
4+
import os from 'node:os'
5+
6+
describe('目录组件样式路径', () => {
7+
let tempDir
8+
let originalTargetPath
9+
10+
beforeEach(() => {
11+
originalTargetPath = process.env.TARGET_PATH
12+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'component-index-style-path-'))
13+
})
14+
15+
afterEach(() => {
16+
if (originalTargetPath) {
17+
process.env.TARGET_PATH = originalTargetPath
18+
}
19+
else {
20+
delete process.env.TARGET_PATH
21+
}
22+
23+
if (fs.existsSync(tempDir)) {
24+
fs.rmSync(tempDir, { recursive: true, force: true })
25+
}
26+
})
27+
28+
it('应该编译目录组件的 index.less', async () => {
29+
fs.writeFileSync(path.join(tempDir, 'app.json'), JSON.stringify({
30+
pages: ['pages/home/index'],
31+
}))
32+
fs.writeFileSync(path.join(tempDir, 'project.config.json'), JSON.stringify({
33+
appid: 'test-app-id',
34+
}))
35+
36+
fs.mkdirSync(path.join(tempDir, 'pages/home'), { recursive: true })
37+
fs.writeFileSync(path.join(tempDir, 'pages/home/index.json'), JSON.stringify({
38+
usingComponents: {
39+
'c-nav': '/components/nav',
40+
},
41+
}))
42+
fs.writeFileSync(path.join(tempDir, 'pages/home/index.wxml'), '<c-nav />')
43+
fs.writeFileSync(path.join(tempDir, 'pages/home/index.wxss'), '')
44+
45+
fs.mkdirSync(path.join(tempDir, 'components/nav'), { recursive: true })
46+
fs.writeFileSync(path.join(tempDir, 'components/nav/index.json'), JSON.stringify({
47+
component: true,
48+
}))
49+
fs.writeFileSync(path.join(tempDir, 'components/nav/index.wxml'), '<view class="nav"></view>')
50+
fs.writeFileSync(path.join(tempDir, 'components/nav/index.less'), '.nav { display: flex; }')
51+
52+
const outputDir = path.join(tempDir, 'dist')
53+
fs.mkdirSync(outputDir, { recursive: true })
54+
process.env.TARGET_PATH = outputDir
55+
56+
const { storeInfo, getPages } = await import('../src/env.js')
57+
storeInfo(tempDir)
58+
59+
const { compileSS } = await import('../src/core/style-compiler.js')
60+
await compileSS(getPages().mainPages, null, { completedTasks: 0 })
61+
62+
const output = fs.readFileSync(path.join(outputDir, 'main/pages_home_index.css'), 'utf-8')
63+
expect(output).toContain('.nav')
64+
expect(output).toContain('display:flex')
65+
})
66+
})

fe/packages/compiler/src/core/style-compiler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ function getAbsolutePath(modulePath) {
264264
if (fs.existsSync(ssFullPath)) {
265265
return ssFullPath
266266
}
267+
268+
const indexSsFullPath = `${workPath}${src}/index${ssType}`
269+
if (fs.existsSync(indexSsFullPath)) {
270+
return indexSsFullPath
271+
}
267272
}
268273
}
269274

0 commit comments

Comments
 (0)