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

wip(uvue): setup

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