diff --git a/packages/uni-cli-shared/src/vue/parse.ts b/packages/uni-cli-shared/src/vue/parse.ts index 7c8bd9ef270d2cd265f4febd2af68e894a6f9c38..f72b3679fc90d1e7debe589da354618695dada1c 100644 --- a/packages/uni-cli-shared/src/vue/parse.ts +++ b/packages/uni-cli-shared/src/vue/parse.ts @@ -13,7 +13,7 @@ const BLOCK_RE = /<\/block>/ const WXS_LANG_RE = /lang=["|'](renderjs|wxs)["|']/ const WXS_ATTRS = ['wxs', 'renderjs'] -export function parseVueCode(code: string) { +export function parseVueCode(code: string, isNVue = false) { const hasBlock = BLOCK_RE.test(code) const hasWxs = WXS_LANG_RE.test(code) if (!hasBlock && !hasWxs) { @@ -27,7 +27,7 @@ export function parseVueCode(code: string) { // 重新解析新的 code ast = parseVue(code, errors) } - if (hasWxs) { + if (!isNVue && hasWxs) { const wxsNodes = parseWxsNodes(ast) code = parseWxsCode(wxsNodes, code) // add watch diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts b/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts index 8f06a53a8449d9cbe969caacad2c8c2cf5378aff..ded5aab532ae761dd45fc33a0dea8e8a8bbc825f 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts @@ -1,34 +1,15 @@ import path from 'path' -import debug from 'debug' import { Plugin } from 'vite' -import { - RootNode, - NodeTypes, - ParentNode, - ElementNode, - AttributeNode, - TemplateChildNode, -} from '@vue/compiler-core' -import { MagicString } from '@vue/compiler-sfc' import { clearMiniProgramTemplateFilter, EXTNAME_VUE, normalizeMiniProgramFilename, parseVueRequest, removeExt, - isElementNode, - parseVue, + parseVueCode, } from '@dcloudio/uni-cli-shared' -const debugPreVue = debug('uni:pre-vue') - -const BLOCK_RE = /<\/block>/ - -const WXS_LANG_RE = /lang=["|'](renderjs|wxs)["|']/ - -const WXS_ATTRS = ['wxs', 'renderjs'] - export function uniPreVuePlugin(): Plugin { let isNVue = false return { @@ -48,159 +29,10 @@ export function uniPreVuePlugin(): Plugin { clearMiniProgramTemplateFilter( removeExt(normalizeMiniProgramFilename(id, process.env.UNI_INPUT_DIR)) ) - const hasBlock = BLOCK_RE.test(code) - // nvue 不支持 renderjs,wxs - const hasWxs = !isNVue && WXS_LANG_RE.test(code) - if (!hasBlock && !hasWxs) { - return - } - debugPreVue(id) - const watchFiles: string[] = [] - const errors: SyntaxError[] = [] - let ast = parseVue(code, errors) - if (hasBlock) { - code = parseBlockCode(ast, code) - } - if (hasWxs) { - if (hasBlock) { - // 重新解析新的 code - ast = parseVue(code, errors) - } - const wxsNodes = parseWxsNodes(ast) - code = parseWxsCode(wxsNodes, code) - // add watch - for (const wxsNode of wxsNodes) { - const srcProp = wxsNode.props.find( - (prop) => prop.type === NodeTypes.ATTRIBUTE && prop.name === 'src' - ) as AttributeNode | undefined - if (srcProp && srcProp.value) { - const resolveId = await this.resolve(srcProp.value.content, id) - if (resolveId) { - watchFiles.push(resolveId.id) - } - } - } - } - // if (errors.length) { - // this.error(errors.join('\n')) - // } - watchFiles.forEach((file) => this.addWatchFile(file)) return { - code, // 暂不提供sourcemap,意义不大 + code: parseVueCode(code, isNVue).code, // 暂不提供sourcemap,意义不大 map: null, } }, } } - -function traverseChildren({ children }: ParentNode, blockNodes: ElementNode[]) { - children.forEach((node) => traverseNode(node, blockNodes)) -} - -function traverseNode( - node: RootNode | TemplateChildNode, - blockNodes: ElementNode[] -) { - if (isElementNode(node) && node.tag === 'block') { - blockNodes.push(node) - } - if ( - node.type === NodeTypes.IF_BRANCH || - node.type === NodeTypes.FOR || - node.type === NodeTypes.ELEMENT || - node.type === NodeTypes.ROOT - ) { - traverseChildren(node, blockNodes) - } -} - -export function parseBlockCode(ast: RootNode, code: string) { - const blockNodes: ElementNode[] = [] - traverseNode(ast, blockNodes) - if (blockNodes.length) { - return parseBlockNode(code, blockNodes) - } - return code -} - -const BLOCK_END_LEN = ''.length -const BLOCK_START_LEN = ' { - const startOffset = loc.start.offset - const endOffset = loc.end.offset - magicString.overwrite( - startOffset, - startOffset + BLOCK_START_LEN, - '') - }) - return magicString.toString() -} - -export function parseWxsNodes(ast: RootNode) { - return ast.children.filter( - (node) => - node.type === NodeTypes.ELEMENT && - node.tag === 'script' && - node.props.find( - (prop) => - prop.name === 'lang' && - prop.type === NodeTypes.ATTRIBUTE && - prop.value && - WXS_ATTRS.includes(prop.value.content) - ) - ) as ElementNode[] -} - -export function parseWxsCode(wxsNodes: ElementNode[], code: string) { - if (wxsNodes.length) { - code = parseWxsNode(code, wxsNodes) - } - return code -} - -const SCRIPT_END_LEN = ''.length -const SCRIPT_START_LEN = ' { - const langAttr = props.find((prop) => prop.name === 'lang') as AttributeNode - const moduleAttr = props.find( - (prop) => prop.name === 'module' - ) as AttributeNode - const startOffset = loc.start.offset - const endOffset = loc.end.offset - const lang = langAttr.value!.content - const langStartOffset = langAttr.loc.start.offset - magicString.overwrite( - startOffset, - startOffset + SCRIPT_START_LEN, - '<' + lang - ) // ' - ) // or - - if (moduleAttr) { - const moduleStartOffset = moduleAttr.loc.start.offset - magicString.overwrite( - moduleStartOffset, - moduleStartOffset + 'module'.length, - 'name' - ) // module="echarts" => name="echarts" - } - }) - return magicString.toString() -}