提交 b2ad3848 编写于 作者: fxy060608's avatar fxy060608

wip(uvue): setup

上级 8a971170
...@@ -142,7 +142,7 @@ export function resolveParserPlugins( ...@@ -142,7 +142,7 @@ export function resolveParserPlugins(
} }
plugins.push(['typescript', { dts }]) plugins.push(['typescript', { dts }])
if (!userPlugins || !userPlugins.includes('decorators')) { if (!userPlugins || !userPlugins.includes('decorators')) {
plugins.push('decorators-legacy') plugins.push('decorators')
} }
if (userPlugins) { if (userPlugins) {
plugins.push(...userPlugins) plugins.push(...userPlugins)
......
import fs from 'node:fs'
import path from 'node:path' import path from 'node:path'
import type { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc' import type { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import type { import type {
...@@ -22,14 +21,12 @@ import { ...@@ -22,14 +21,12 @@ import {
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import type { CompilerError, Position } from '@vue/compiler-core' import type { CompilerError, Position } from '@vue/compiler-core'
import type { ImportSpecifier } from 'es-module-lexer' import type { ImportSpecifier } from 'es-module-lexer'
import { import { createDescriptor, setSrcDescriptor } from '../descriptorCache'
createDescriptor,
getDescriptor,
setSrcDescriptor,
} from '../descriptorCache'
import { resolveScript, scriptIdentifier } from './script' import { resolveScript, scriptIdentifier } from './script'
import type { ResolvedOptions } from './index' import type { ResolvedOptions } from './index'
import { import {
addAutoImports,
addExtApiComponents,
createResolveError, createResolveError,
genClassName, genClassName,
parseImports, parseImports,
...@@ -39,22 +36,22 @@ import { ...@@ -39,22 +36,22 @@ import {
} from '../../utils' } from '../../utils'
import { genTemplateCode } from '../code/template' import { genTemplateCode } from '../code/template'
import { generateCodeFrameColumns } from '@dcloudio/uni-cli-shared' import { generateCodeFrameColumns } from '@dcloudio/uni-cli-shared'
import { genComponentPublicInstanceImported } from '../compiler/utils'
export async function transformMain( export async function transformMain(
code: string, code: string,
filename: string, filename: string,
options: ResolvedOptions, options: ResolvedOptions,
pluginContext: TransformPluginContext pluginContext: TransformPluginContext,
isAppVue: boolean = false
) { ) {
if (!options.compiler) {
options.compiler = require('@vue/compiler-sfc')
}
const relativeFileName = parseUTSRelativeFilename(filename) const relativeFileName = parseUTSRelativeFilename(filename)
const { descriptor, errors } = createDescriptor(filename, code, options) const { descriptor, errors } = createDescriptor(filename, code, options)
if (fs.existsSync(filename)) {
// populate descriptor cache for HMR if it's not set yet
getDescriptor(filename, options, true)
}
if (errors.length) { if (errors.length) {
errors.forEach((error) => errors.forEach((error) =>
pluginContext.error(createRollupError('', filename, error, code)) pluginContext.error(createRollupError('', filename, error, code))
...@@ -69,14 +66,20 @@ export async function transformMain( ...@@ -69,14 +66,20 @@ export async function transformMain(
) )
const className = genClassName(relativeFileName) const className = genClassName(relativeFileName)
const inputRoot = normalizePath(options.root) let templateCode = ''
const templateStartLine = descriptor.template?.loc.start.line ?? 0 let templateMap = undefined
// template let templateImportsCode = ''
const { code: templateCode, map: templateMap } = await genTemplateCode( let templateImportEasyComponentsCode = ''
descriptor, let templateImportUTSComponentsCode = ''
{
if (!isAppVue) {
// template
const inputRoot = normalizePath(options.root)
const templateStartLine = descriptor.template?.loc.start.line ?? 0
const templateResult = await genTemplateCode(descriptor, {
...options, ...options,
mode: 'function', mode: 'function',
rootDir: options.root,
filename: relativeFileName, filename: relativeFileName,
className, className,
prefixIdentifiers: true, prefixIdentifiers: true,
...@@ -104,13 +107,50 @@ export async function transformMain( ...@@ -104,13 +107,50 @@ export async function transformMain(
onTemplateLog('error', error, code, relativeFileName, templateStartLine) onTemplateLog('error', error, code, relativeFileName, templateStartLine)
}, },
parseUTSComponent, parseUTSComponent,
})
templateCode = templateResult.code
templateMap = templateResult.map
const {
easyComponentAutoImports,
elements,
importEasyComponents,
importUTSComponents,
imports,
} = templateResult
templateImportEasyComponentsCode = importEasyComponents.join('\n')
templateImportUTSComponentsCode = importUTSComponents.join('\n')
templateImportsCode = imports.join('\n')
Object.keys(easyComponentAutoImports).forEach((source) => {
addAutoImports(source, easyComponentAutoImports[source])
})
if (process.env.NODE_ENV === 'production') {
addExtApiComponents(
elements.filter((element) => {
// 如果是UTS原生组件,则无需记录摇树
if (parseUTSComponent(element, 'kotlin')) {
return false
}
return true
})
)
} }
) }
// styles // styles
const stylesCode = await genStyleCode(descriptor, pluginContext) const stylesCode = await genStyleCode(descriptor, pluginContext)
const output: string[] = [scriptCode, templateCode, `/*${className}Styles*/`] const utsOutput: string[] = [
scriptCode ||
`
export default {}
`,
templateCode,
`/*${className}Styles*/`,
]
let resolvedMap: RawSourceMap | undefined = undefined let resolvedMap: RawSourceMap | undefined = undefined
if (options.sourceMap) { if (options.sourceMap) {
...@@ -155,7 +195,7 @@ export async function transformMain( ...@@ -155,7 +195,7 @@ export async function transformMain(
} }
// handle TS transpilation // handle TS transpilation
let resolvedCode = output.join('\n') const utsCode = utsOutput.join('\n')
if (resolvedMap) { if (resolvedMap) {
pluginContext.emitFile({ pluginContext.emitFile({
...@@ -165,11 +205,16 @@ export async function transformMain( ...@@ -165,11 +205,16 @@ export async function transformMain(
}) })
} }
const jsCodes = [] const jsCodes = [
if (resolvedCode) { templateImportEasyComponentsCode,
templateImportUTSComponentsCode,
templateImportsCode,
]
// 仅需要再解析script中的import,template上边已经加入了
if (scriptCode) {
jsCodes.push( jsCodes.push(
await parseImports( await parseImports(
resolvedCode, scriptCode,
resolvedMap resolvedMap
? createTryResolve(filename, pluginContext.resolve, resolvedMap) ? createTryResolve(filename, pluginContext.resolve, resolvedMap)
: undefined : undefined
...@@ -178,15 +223,21 @@ export async function transformMain( ...@@ -178,15 +223,21 @@ export async function transformMain(
pluginContext.emitFile({ pluginContext.emitFile({
type: 'asset', type: 'asset',
fileName: relativeFileName, fileName: relativeFileName,
source: resolvedCode, source: utsCode,
}) })
} }
if (stylesCode) { if (stylesCode) {
jsCodes.push(stylesCode) jsCodes.push(stylesCode)
} }
jsCodes.push(`export default "${className}"
export const ${genComponentPublicInstanceImported(
options.root,
relativeFileName
)} = {}`)
const jsCode = jsCodes.filter(Boolean).join('\n')
return { return {
code: jsCodes.join('\n'), code: jsCode,
map: { map: {
mappings: '', mappings: '',
} as SourceMapInput, } as SourceMapInput,
......
import path from 'node:path'
import { import {
MANIFEST_JSON_UTS, MANIFEST_JSON_UTS,
PAGES_JSON_UTS, PAGES_JSON_UTS,
...@@ -5,6 +6,8 @@ import { ...@@ -5,6 +6,8 @@ import {
UniVitePlugin, UniVitePlugin,
initI18nOptions, initI18nOptions,
injectAssetPlugin, injectAssetPlugin,
normalizeNodeModules,
normalizePath,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { compileI18nJsonStr } from '@dcloudio/uni-i18n' import { compileI18nJsonStr } from '@dcloudio/uni-i18n'
import { Plugin, ResolvedConfig } from 'vite' import { Plugin, ResolvedConfig } from 'vite'
...@@ -96,3 +99,12 @@ export function configResolved(config: ResolvedConfig, isAndroidX = false) { ...@@ -96,3 +99,12 @@ export function configResolved(config: ResolvedConfig, isAndroidX = false) {
config.build.assetsInlineLimit = 0 config.build.assetsInlineLimit = 0
injectAssetPlugin(config, { isAndroidX }) injectAssetPlugin(config, { isAndroidX })
} }
export function relativeInputDir(filename: string) {
const inputDir = process.env.UNI_INPUT_DIR
filename = normalizeNodeModules(filename)
if (filename.startsWith(inputDir)) {
return normalizePath(path.relative(inputDir, filename))
}
return filename
}
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
} from '@vue/compiler-core' } from '@vue/compiler-core'
import { ParserPlugin } from '@babel/parser' import { ParserPlugin } from '@babel/parser'
import { getPlatformDir } from './platform' import { getPlatformDir } from './platform'
import { isInHBuilderX } from './hbx'
export const version = require('../package.json').version export const version = require('../package.json').version
...@@ -76,6 +77,16 @@ export function normalizeNodeModules(str: string) { ...@@ -76,6 +77,16 @@ export function normalizeNodeModules(str: string) {
/.*\/plugins\/uniapp-cli-vite\/node[-_]modules/, /.*\/plugins\/uniapp-cli-vite\/node[-_]modules/,
'node-modules' 'node-modules'
) )
if (!isInHBuilderX()) {
// 内部测试
if (str.includes('uni-app-next/packages/')) {
str = str.replace(
/.*\/uni-app-next\/packages\//,
'node-modules/@dcloudio/'
)
}
}
if (process.env.UNI_PLATFORM === 'mp-alipay') { if (process.env.UNI_PLATFORM === 'mp-alipay') {
str = str.replace('node-modules/@', 'node-modules/npm-scope-') str = str.replace('node-modules/@', 'node-modules/npm-scope-')
} }
......
...@@ -93,7 +93,6 @@ ...@@ -93,7 +93,6 @@
complete ?: CompleteCallback, complete ?: CompleteCallback,
} }
@Suppress("UNCHECKED_CAST")
function cast_callback<T>(options : any | null) : T | null { function cast_callback<T>(options : any | null) : T | null {
return options as T | null return options as T | null
} }
......
...@@ -173,9 +173,9 @@ export function initPluginVueOptions( ...@@ -173,9 +173,9 @@ export function initPluginVueOptions(
if (!vueOptions.script.babelParserPlugins.includes('typescript')) { if (!vueOptions.script.babelParserPlugins.includes('typescript')) {
vueOptions.script.babelParserPlugins.push('typescript') vueOptions.script.babelParserPlugins.push('typescript')
} }
// TODO 是不是应该 decorators // decorators or decorators-legacy
if (!vueOptions.script.babelParserPlugins.includes('decorators-legacy')) { if (!vueOptions.script.babelParserPlugins.includes('decorators')) {
vueOptions.script.babelParserPlugins.push('decorators-legacy') vueOptions.script.babelParserPlugins.push('decorators')
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册