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

fix(mp): support main.ts (#3019)

上级 ca8ff7b8
......@@ -51,6 +51,44 @@ export function createApp() {
'component-b': '/components/component-b',
})
})
test(`ts`, async () => {
const source = `
import { createSSRApp } from 'vue'
import ComponentA from './components/component-a.vue'
import ComponentB from './components/component-b.vue'
export function createApp() {
const app = createSSRApp(App)
app.provide('UseRequestConfigContext', {
requestMethod: (param: any) => {},
})
app.component('component-a',ComponentA)
app.component('component-b',ComponentB)
return {
app
}
}
`
const { code, usingComponents } = await transformVueComponentImports(
source,
importer.replace('.js', '.ts'),
{
root,
global: true,
resolve,
dynamicImport,
}
)
expect(code).toContain(
`const ComponentA = ()=>import('${root}/components/component-a.vue')`
)
expect(code).toContain(
`const ComponentB = ()=>import('${root}/components/component-b.vue')`
)
expect(usingComponents).toMatchObject({
'component-a': '/components/component-a',
'component-b': '/components/component-b',
})
})
})
describe('local', () => {
const importer = '/usr/xxx/projects/test/src/pages/index/index.vue'
......
export const PUBLIC_DIR = 'static'
export const EXTNAME_JS = ['.js', '.ts', '.jsx', '.tsx']
export const EXTNAME_TS = ['.ts', '.tsx']
export const EXTNAME_VUE = ['.vue', '.nvue']
export const EXTNAME_VUE_RE = /\.(vue|nvue)$/
export const EXTNAME_JS_RE = /\.[jt]sx?$/
export const EXTNAME_TS_RE = /\.tsx?$/
export const ASSETS_INLINE_LIMIT = 40 * 1024
......
......@@ -5,7 +5,14 @@ import { extend } from '@vue/shared'
import { isImportDeclaration, isImportDefaultSpecifier } from '@babel/types'
import { parse } from '@babel/parser'
import { EXTNAME_VUE, EXTNAME_VUE_RE } from '../constants'
import { normalizeParsePlugins } from '../utils'
/**
* 暂时没用
* @param source
* @param importer
* @param resolve
* @returns
*/
export async function findVueComponentImports(
source: string,
importer: string,
......@@ -44,6 +51,7 @@ export async function findVueComponentImports(
}
if (EXTNAME_VUE_RE.test(res.id)) {
const expr = parse(source.slice(importSpecifier.ss, importSpecifier.se), {
plugins: normalizeParsePlugins(res.id),
sourceType: 'module',
}).program.body[0]
if (isImportDeclaration(expr) && expr.specifiers.length === 1) {
......
......@@ -20,7 +20,11 @@ import MagicString from 'magic-string'
import { PluginContext } from 'rollup'
import { M } from '../messages'
import { BINDING_COMPONENTS } from '../constants'
import { normalizeMiniProgramFilename, removeExt } from '../utils'
import {
normalizeMiniProgramFilename,
normalizeParsePlugins,
removeExt,
} from '../utils'
import { addLeadingSlash } from '@dcloudio/uni-shared'
interface TransformVueComponentImportsOptions {
......@@ -47,9 +51,10 @@ export async function transformVueComponentImports(
if (!global && !code.includes(BINDING_COMPONENTS)) {
return { code, usingComponents: {} }
}
const s = new MagicString(code)
const scriptAst = parse(code, {
plugins: [...(babelParserPlugins || [])],
plugins: normalizeParsePlugins(importer, babelParserPlugins),
sourceType: 'module',
}).program
......
......@@ -3,7 +3,7 @@ import os from 'os'
import path from 'path'
import { camelize, capitalize } from '@vue/shared'
export { default as hash } from 'hash-sum'
import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants'
import { EXTNAME_TS_RE, PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants'
import {
NodeTypes,
......@@ -11,6 +11,7 @@ import {
RootNode,
TemplateChildNode,
} from '@vue/compiler-core'
import { ParserPlugin } from '@babel/parser'
export let isRunningWithYarnPnp: boolean
try {
......@@ -76,3 +77,17 @@ export function normalizeMiniProgramFilename(
}
return normalizeNodeModules(path.relative(inputDir, filename))
}
export function normalizeParsePlugins(
importer: string,
babelParserPlugins?: ParserPlugin[]
) {
const isTS = EXTNAME_TS_RE.test(importer.split('?')[0])
const plugins: ParserPlugin[] = []
if (isTS) {
plugins.push('jsx')
}
if (babelParserPlugins) plugins.push(...babelParserPlugins)
if (isTS) plugins.push('typescript', 'decorators-legacy')
return plugins
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册