Skip to content

Commit 27d35b1

Browse files
authored
feat: support config treeshaking (#1317)
* feat: support config treeshaking * fix: async import get default export
1 parent 65cb092 commit 27d35b1

6 files changed

Lines changed: 79 additions & 31 deletions

File tree

designer-demo/registry.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@
1010
*
1111
*/
1212
import { META_SERVICE } from '@opentiny/tiny-engine-meta-register'
13-
// import { META_SERVICE } from '@opentiny/tiny-engine'
1413
import engineConfig from './engine.config'
1514
import { HttpService } from './src/composable'
1615
import scriptPlugin from './src/plugins/script'
17-
import removedRegistry from './removedRegistry.json'
1816

1917
export default {
2018
[META_SERVICE.Http]: HttpService,
2119
'engine.config': {
2220
...engineConfig
2321
},
24-
...removedRegistry,
2522
// 配置 false 隐藏工具栏清空按钮
26-
// 'engine.toolbars.clean': false,
27-
// 配置 false 隐藏大纲树
28-
// 'engine.plugins.outlinetree': false,
29-
// 替换整个页面JS插件
23+
'engine.toolbars.clean': false,
24+
// 配置 false 隐藏大纲树,手动配置 tree-shaking 为 false,仍然不会被 tree-shaking
25+
// #__TINY_ENGINE_TREE_SHAKING__: false
26+
'engine.plugins.outlinetree': false,
27+
// 替换整个页面JS插件,手动配置 tree-shaking 为 true
28+
/* #__TINY_ENGINE_TREE_SHAKING__: true */
3029
'engine.plugins.pagecontroller': scriptPlugin,
3130
// 换了个id,代表新增模块
3231
'engine.plugins.script': {

designer-demo/removedRegistry.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

designer-demo/src/main.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
import { defineEntry, tryGetAndDefineHotfixRegistry } from '@opentiny/tiny-engine-meta-register'
1313
import { configurators } from './configurators/'
14-
import registry from '../registry'
1514

1615
import 'virtual:svg-icons-register'
1716

@@ -25,15 +24,13 @@ async function startApp() {
2524
const hotfixRegistry = (await tryGetAndDefineHotfixRegistry({ url: 'http://localhost:8090/hotfixRegistry.js', request: fetchHotfixRegistry })) || {}
2625

2726
// 导入@opentiny/tiny-engine时,内部的依赖包也会逐个导入,可能会执行useComplie,此时需要templateHashMap。所以需要先执行一次defineEntry
28-
defineEntry(registry)
29-
// TODO: 问题一:提前引入 @opentiny/tiny-engine 的依赖,会导致覆盖失败?
30-
// TODO: 问题二:如果 registry 是异步的,会导致模板覆盖失败?
31-
// const registry = await import('../registry')
27+
const registry = await import('../registry')
28+
defineEntry(registry.default)
3229
const { init } = await import('@opentiny/tiny-engine')
3330

3431
init({
3532
// TODO: 这里支持数组,传入多个注册表
36-
registry: [registry, hotfixRegistry],
33+
registry: [registry.default, hotfixRegistry],
3734
configurators,
3835
createAppSignal: ['global_service_init_finish']
3936
})

designer-demo/vite.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'node:path'
22
import { defineConfig, mergeConfig } from 'vite'
33
import { useTinyEngineBaseConfig } from '@opentiny/tiny-engine-vite-config'
4-
import removedRegistry from './removedRegistry.json'
54

65
export default defineConfig((configEnv) => {
76
const baseConfig = useTinyEngineBaseConfig({
@@ -10,7 +9,7 @@ export default defineConfig((configEnv) => {
109
iconDirs: [path.resolve(__dirname, './node_modules/@opentiny/tiny-engine/assets/')],
1110
useSourceAlias: true,
1211
envDir: './env',
13-
removedRegistry
12+
registryPath: './registry.js'
1413
})
1514

1615
const customConfig = {

packages/build/vite-config/src/default-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export function useTinyEngineBaseConfig(engineConfig) {
152152
const config = getDefaultConfig(engineConfig)
153153

154154
config.plugins.push(
155-
treeShakingPlugin(engineConfig.removedRegistry),
155+
treeShakingPlugin(engineConfig.registryPath),
156156
createSvgIconsPlugin({
157157
iconDirs: engineConfig.iconDirs || [],
158158
symbolId: 'icon-[name]',
Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,72 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
13
import replace from '@rollup/plugin-replace'
4+
import { parse } from '@babel/parser'
5+
6+
export function treeShakingPlugin(registryPath) {
7+
if (!registryPath) {
8+
return null
9+
}
210

3-
export function treeShakingPlugin(removedRegistry) {
411
const envReplace = {}
5-
Object.entries(removedRegistry).forEach(([key, value]) => {
6-
envReplace[`__TINY_ENGINE_REMOVED_REGISTRY["${key}"]`] = value
7-
})
8-
return replace({
9-
values: {
10-
...envReplace
11-
},
12-
delimiters: ['', '']
13-
})
12+
const filePath = path.resolve(process.cwd(), registryPath)
13+
14+
if (!fs.existsSync(filePath)) {
15+
// TODO: 测试可否返回 null
16+
return null
17+
}
18+
19+
try {
20+
const fileContent = fs.readFileSync(filePath, 'utf-8')
21+
const ast = parse(fileContent, { sourceType: 'module' })
22+
const commentPattern = /#__TINY_ENGINE_TREE_SHAKING__:\s*(?<value>true|false)/
23+
24+
ast.program.body.forEach((item) => {
25+
// 过滤默认导出 且导出类型为 object
26+
if (item.type === 'ExportDefaultDeclaration' && item.declaration?.type === 'ObjectExpression') {
27+
item.declaration.properties.forEach((propertyItem) => {
28+
// 是属性 且 key 为 string
29+
if (propertyItem.type === 'ObjectProperty' && propertyItem.key?.type === 'StringLiteral') {
30+
const key = propertyItem.key.value
31+
// 有 comment,解析 comment。
32+
// 通过 comment 指定 treeshaking,优先以 comment 为标准
33+
if (propertyItem.leadingComments?.length) {
34+
// TODO: 测试多行匹配
35+
// TODO: 需要确认是仅解析前面一个注释,还是解析所有注释
36+
for (const commentItem of propertyItem.leadingComments) {
37+
const match = commentItem.value.match(commentPattern)
38+
39+
if (!match) {
40+
continue
41+
}
42+
43+
if (match.groups.value === 'true') {
44+
envReplace[`__TINY_ENGINE_REMOVED_REGISTRY["${key}"]`] = false
45+
}
46+
47+
return
48+
}
49+
}
50+
51+
// 注册表注释了插件,默认 tree-shaking,
52+
if (propertyItem.value.type === 'BooleanLiteral' && propertyItem.value.value === false) {
53+
envReplace[`__TINY_ENGINE_REMOVED_REGISTRY["${key}"]`] = false
54+
}
55+
}
56+
})
57+
}
58+
})
59+
60+
return replace({
61+
values: {
62+
...envReplace
63+
},
64+
delimiters: ['', '']
65+
})
66+
} catch (error) {
67+
const logger = console
68+
logger.warn('[]')
69+
}
70+
71+
return null
1472
}

0 commit comments

Comments
 (0)