Skip to content

Commit 1fc43b9

Browse files
authored
Merge pull request #193 from charlotteForever/feat/20240426_import_styles
feat(plugin-compiler-web): 样式文件中@import内容可写入样式文件
2 parents 7f7c4cc + bf97608 commit 1fc43b9

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

packages/plugin-compiler-web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@morjs/plugin-compiler-alipay": "1.0.106",
3535
"@morjs/runtime-web": "1.0.108",
3636
"@morjs/utils": "1.0.71",
37-
"postcss": "7.0.39"
37+
"postcss": "7.0.39",
38+
"postcss-import-sync": "7.1.4"
3839
}
3940
}

packages/plugin-compiler-web/src/compiler/core/acss/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import path from 'path'
12
import postcss from 'postcss'
3+
import atImport from 'postcss-import-sync'
24
import { defCondition, isEndIf, isIfDef } from '../../utils/comment'
35
import { getExternalComponent, isEnableStyleScope } from '../../utils/index'
46
import { globalComponentName, randomHash } from '../option'
@@ -38,6 +40,13 @@ export default function (source: string, options: AcssOptions) {
3840
)
3941
}
4042

43+
if (options.enableCombineImportStyles) {
44+
plugins.unshift(
45+
PostcssStylePathPlugin({ path: options.resourcePath }),
46+
atImport()
47+
)
48+
}
49+
4150
if (syntax && typeof syntax === 'object') {
4251
config.syntax = syntax
4352
}
@@ -131,6 +140,23 @@ const ComponentNameMapPlugin = postcss.plugin(
131140
}
132141
)
133142

143+
interface PostcssStylePathOptions {
144+
path: string
145+
}
146+
const PostcssStylePathPlugin = postcss.plugin(
147+
'postcss-style-path-plugin',
148+
function (options: PostcssStylePathOptions) {
149+
return function (root) {
150+
root.nodes.forEach((rule) => {
151+
if (rule.type === 'atrule') {
152+
const dirname = path.dirname(options.path)
153+
return (rule.params = path.resolve(dirname, JSON.parse(rule.params)))
154+
}
155+
})
156+
}
157+
}
158+
)
159+
134160
function mapComponentName(selector, options: AcssOptions) {
135161
const arr = selector.split(':')
136162
const name = arr[0]

packages/plugin-compiler-web/src/compiler/core/acss/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface AcssOptions extends BuildOptions {
44
plugins?: string[]
55
needRpx?: boolean // 是否需要rpx 转换。默认true
66
syntax?: any // 自定义语法处理程序
7+
enableCombineImportStyles?: boolean // 是否需要解析@import导入的样式
78
}

packages/plugin-compiler-web/src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ export const UserConfigSchema = z.object({
162162
// 供运行时获取及消费
163163
appConfig: z
164164
.object({
165+
/**
166+
* 用于将样式文件中 @import 样式的内容写入到样式文件中
167+
*/
168+
enableCombineImportStyles: z.boolean().default(false),
165169
/**
166170
* 用于传递业务对某个组件的特殊配置, 比如 map 组件传递 key
167171
*/

packages/plugin-compiler-web/src/plugins/commonConfigPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class CommonConfigPlugin implements Plugin {
245245
.test(/\.(wx|a?c)ss$/)
246246
.use('web-style')
247247
.loader(WEB_COMPILER_LOADERS.style)
248-
.options(WEB_LOADER_OPTIONS)
248+
.options({ enableCombineImportStyles: web?.appConfig?.enableCombineImportStyles, ...WEB_LOADER_OPTIONS })
249249
.before('style')
250250
.end()
251251

0 commit comments

Comments
 (0)