Skip to content

Commit 59a762a

Browse files
committed
wip(web): customElements
1 parent 892a98c commit 59a762a

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

packages/uni-h5/src/view/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export {
4141
//#endif
4242
}
4343
//#if _X_
44-
export * from '../../x/view/components/easycom'
44+
export * from '../../x/view/components/customElements'
4545
//#endif
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// This file is automatically generated.
22
// Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
export {}

packages/uni-h5/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"paths": {
55
"@dcloudio/*": ["packages/*/src"],
6-
"@dcloudio/uni-platform": ["packages/uni-h5/src/platform/index.ts"]
6+
"@dcloudio/uni-platform": ["packages/uni-h5/src/platform/index.ts"],
7+
"@dcloudio/uni-ext-api/*": ["uni-ext-api/uni_modules/*"]
78
}
89
},
910
"include": ["src"]

packages/uni-h5/vite.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { isH5CustomElement, isH5NativeTag } from '@dcloudio/uni-shared'
2424
import { genApiJson } from './api'
2525
import {
2626
replacePagePaths,
27-
syncEasyComFile,
27+
syncCustomElementsFile,
2828
syncPagesFile,
2929
uts2ts,
3030
} from '../../scripts/ext-api'
@@ -51,7 +51,9 @@ if (isNewX) {
5151
apiDirs.push(process.env.UNI_APP_EXT_API_DCLOUD_DIR)
5252
}
5353
systemPagePaths = syncPagesFile(apiDirs, 'web')
54-
syncEasyComFile(apiDirs)
54+
if (process.env.UNI_APP_EXT_COMPONENT_DIR) {
55+
syncCustomElementsFile([process.env.UNI_APP_EXT_COMPONENT_DIR])
56+
}
5557
}
5658

5759
const rollupPlugins = [

scripts/ext-api.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -344,54 +344,74 @@ export function replacePagePaths(
344344
}
345345
}
346346

347-
export function syncEasyComFile(apiDirs: string[]) {
348-
const easyComCodes: string[] = []
347+
export function syncCustomElementsFile(apiDirs: string[]) {
348+
const customElementsImportCodes: string[] = []
349+
const customElementsExportCodes: string[] = []
349350
apiDirs.forEach((apiDir) => {
350351
if (apiDir && fs.existsSync(apiDir)) {
351352
fs.readdirSync(apiDir).forEach((module) => {
352353
const moduleDir = path.resolve(apiDir, module)
353354
// 目前仅限web端编译,所以没有utssdk目录
354355
if (!fs.existsSync(path.resolve(moduleDir, 'utssdk'))) {
355-
const componentsDir = path.resolve(moduleDir, 'components')
356+
const customElementsDir = path.resolve(moduleDir, 'customElements')
356357
if (
357-
fs.existsSync(componentsDir) &&
358+
fs.existsSync(customElementsDir) &&
358359
fs.existsSync(path.resolve(moduleDir, 'package.json'))
359360
) {
360361
const packageJson = require(path.resolve(moduleDir, 'package.json'))
361-
// 当前 easycom 平台禁用
362-
if (packageJson.uni_modules?.easycom?.web === false) {
363-
return
362+
// 当前 customElements 平台禁用
363+
if (packageJson.uni_modules?.customElements?.web === true) {
364+
fs.readdirSync(customElementsDir).forEach((component) => {
365+
if (
366+
fs.existsSync(
367+
path.resolve(
368+
customElementsDir,
369+
component,
370+
component + '.uts'
371+
)
372+
)
373+
) {
374+
const identElement =
375+
capitalize(camelize(component)) + 'Element'
376+
customElementsImportCodes.push(
377+
`// @ts-expect-error
378+
import { ${identElement} } from '@dcloudio/uni-ext-api/${module}/customElements/${component}/${component}.uts.ts'`
379+
)
380+
customElementsExportCodes.push(
381+
`export const ${capitalize(
382+
camelize(component.replace('uni-', ''))
383+
)}0 = /*#__PURE__*/ (() => {
384+
// @ts-expect-error
385+
customElements.define('${component}', ${identElement})
386+
return '${component}'
387+
})()`
388+
)
389+
}
390+
})
364391
}
365-
fs.readdirSync(componentsDir).forEach((component) => {
366-
if (
367-
fs.existsSync(
368-
path.resolve(componentsDir, component, component + '.vue')
369-
)
370-
) {
371-
easyComCodes.push(
372-
`export { default as ${capitalize(
373-
camelize(component)
374-
)} } from '@dcloudio/uni-ext-api/${module}/components/${component}/${component}.vue'`
375-
)
376-
}
377-
})
378392
}
379393
}
380394
})
381395
}
382396
})
383397

398+
const code = customElementsImportCodes.length
399+
? `${customElementsImportCodes.join('\n')}
400+
${customElementsExportCodes.join('\n')}
401+
`
402+
: 'export {}'
403+
384404
fs.outputFileSync(
385405
path.resolve(
386406
path.resolve(__dirname, '..', 'packages', 'uni-h5'),
387407
'src',
388408
'x',
389409
'view',
390410
'components',
391-
'easycom.ts'
411+
'customElements.ts'
392412
),
393413
`// This file is automatically generated.
394414
// Do not modify this file -- YOUR CHANGES WILL BE ERASED!
395-
${easyComCodes.length ? easyComCodes.join('\n') : ''}`
415+
${code}`
396416
)
397417
}

0 commit comments

Comments
 (0)