本文整理汇总了TypeScript中babel-types.importDefaultSpecifier函数的典型用法代码示例。如果您正苦于以下问题:TypeScript importDefaultSpecifier函数的具体用法?TypeScript importDefaultSpecifier怎么用?TypeScript importDefaultSpecifier使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了importDefaultSpecifier函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: transform
//.........这里部分代码省略.........
}
}
}
// @TODO: bind 的处理待定
}
},
ImportDeclaration (path) {
const source = path.node.source.value
if (importSources.has(source)) {
throw codeFrameError(path.node, '无法在同一文件重复 import 相同的包。')
} else {
importSources.add(source)
}
const names: string[] = []
if (source === TARO_PACKAGE_NAME) {
isImportTaro = true
path.node.specifiers.push(
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_GET_ORIGNAL), t.identifier(INTERNAL_GET_ORIGNAL)),
t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
)
}
if (
source === REDUX_PACKAGE_NAME || source === MOBX_PACKAGE_NAME
) {
path.node.specifiers.forEach((s, index, specs) => {
if (s.local.name === 'Provider') {
specs.splice(index, 1)
specs.push(
t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
)
}
})
}
path.traverse({
ImportDefaultSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
},
ImportSpecifier (path) {
const name = path.node.imported.name
DEFAULT_Component_SET.has(name) || names.push(name)
if (source === TARO_PACKAGE_NAME && name === 'Component') {
path.node.local = t.identifier('__BaseComponent')
}
}
})
componentSourceMap.set(source, names)
}
})
if (!isImportTaro) {
ast.program.body.unshift(
t.importDeclaration([
t.importDefaultSpecifier(t.identifier('Taro')),
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_GET_ORIGNAL), t.identifier(INTERNAL_GET_ORIGNAL)),
t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
], t.stringLiteral('@tarojs/taro'))
)
}
if (!mainClass) {
throw new Error('未找到 Taro.Component 的类定义')
}
mainClass.node.body.body.forEach(handleThirdPartyComponent)
const storeBinding = mainClass.scope.getBinding(storeName)
mainClass.scope.rename('Component', '__BaseComponent')
if (storeBinding) {
const statementPath = storeBinding.path.getStatementParent()
if (statementPath) {
ast.program.body.forEach((node, index, body) => {
if (node === statementPath.node) {
body.splice(index + 1, 0, t.expressionStatement(
t.callExpression(t.identifier('setStore'), [
t.identifier(storeName)
])
))
}
})
}
}
resetTSClassProperty(mainClass.node.body.body)
if (options.isApp) {
renderMethod.replaceWith(
t.classMethod('method', t.identifier('_createData'), [], t.blockStatement([]))
)
return { ast } as TransformResult
}
result = new Transformer(mainClass, options.sourcePath, componentProperies).result
result.code = generate(ast).code
result.ast = ast
result.template = prettyPrint(result.template, {
max_char: 0
})
result.imageSrcs = Array.from(imageSource)
return result
}
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:index.ts
示例2: parseAst
//.........这里部分代码省略.........
},
CallExpression (astPath) {
const node = astPath.node
const calleePath = astPath.get('callee')
const callee = calleePath.node
if (callee.type === 'Identifier') {
if (callee.name === 'require') {
const args = node.arguments as Array<t.StringLiteral>
const value = args[0].value
analyzeImportUrl(sourceFilePath, scriptFiles, args[0], value)
} else if (WX_GLOBAL_FN.has(callee.name)) {
calleePath.replaceWith(
t.memberExpression(t.identifier('Taro'), callee as t.Identifier)
)
needInsertImportTaro = true
}
} else if (callee.type === 'MemberExpression') {
const object = callee.object as t.Identifier
if (object.name === 'wx') {
(calleePath.get('object') as NodePath).replaceWith(t.identifier('Taro'))
needInsertImportTaro = true
}
}
}
})
},
exit (astPath) {
const bodyNode = astPath.get('body') as NodePath<t.Node>[]
const lastImport = bodyNode.filter(p => p.isImportDeclaration()).pop()
const hasTaroImport = bodyNode.some(p => p.isImportDeclaration() && p.node.source.value === '@tarojs/taro')
if (needInsertImportTaro && !hasTaroImport) {
(astPath.node as t.Program).body.unshift(
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier('Taro'))],
t.stringLiteral('@tarojs/taro')
)
)
}
astPath.traverse({
StringLiteral (astPath) {
const value = astPath.node.value
const extname = path.extname(value)
if (extname && REG_IMAGE.test(extname) && !REG_URL.test(value)) {
let imageRelativePath: string
let sourceImagePath: string
let outputImagePath: string
if (path.isAbsolute(value)) {
sourceImagePath = path.join(self.root, value)
} else {
sourceImagePath = path.resolve(sourceFilePath, '..', value)
}
imageRelativePath = promoteRelativePath(path.relative(sourceFilePath, sourceImagePath))
outputImagePath = self.getDistFilePath(sourceImagePath)
if (fs.existsSync(sourceImagePath)) {
self.copyFileToTaro(sourceImagePath, outputImagePath)
printLog(processTypeEnum.COPY, '图片', self.generateShowPath(outputImagePath))
} else {
printLog(processTypeEnum.ERROR, '图片不存在', self.generateShowPath(sourceImagePath))
}
if (astPath.parentPath.isVariableDeclarator()) {
astPath.replaceWith(t.callExpression(t.identifier('require'), [t.stringLiteral(imageRelativePath)]))
} else if (astPath.parentPath.isJSXAttribute()) {
astPath.replaceWith(t.jSXExpressionContainer(t.callExpression(t.identifier('require'), [t.stringLiteral(imageRelativePath)])))
}
}
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:index.ts
示例3:
defaultSpec ? [defaultSpec, ...specifiers].map((spec, index) => {
if (index === 0) {
return t.importDefaultSpecifier(t.identifier(defaultSpec))
}
return t.importSpecifier(t.identifier(spec), t.identifier(spec))
}) : specifiers.map(s => t.importSpecifier(t.identifier(s), t.identifier(s))),
开发者ID:YangShaoQun,项目名称:taro,代码行数:6,代码来源:utils.ts
示例4: parseJSCode
export function parseJSCode ({code, filePath, isEntryFile, projectConfig}) {
let ast
try {
ast = getJSAst(code, filePath)
} catch (e) {
throw e
}
const styleFiles: string[] = []
const pages: string[] = [] // app.js 里面的config 配置里面的 pages
const iconPaths: string[] = [] // app.js 里面的config 配置里面的需要引入的 iconPath
let hasAddReactImportDefaultName = false
let providorImportName
let storeName
let hasAppExportDefault
let classRenderReturnJSX
let hasConstructor = false
let hasComponentDidMount = false
let hasComponentDidShow = false
let hasComponentDidHide = false
let hasComponentWillUnmount = false
traverse(ast, {
ClassExpression: ClassDeclarationOrExpression,
ClassDeclaration: ClassDeclarationOrExpression,
ImportDeclaration (astPath) {
const node = astPath.node as t.ImportDeclaration
const source = node.source
let value = source.value
const valueExtname = path.extname(value)
const specifiers = node.specifiers
const pathAlias = projectConfig.alias || {}
if (Util.isAliasPath(value, pathAlias)) {
source.value = value = Util.replaceAliasPath(filePath, value, pathAlias)
}
// 引入的包为 npm 包
if (!Util.isNpmPkg(value)) {
// import 样式处理
if (REG_STYLE.test(valueExtname)) {
const stylePath = path.resolve(path.dirname(filePath), value)
if (styleFiles.indexOf(stylePath) < 0) {
styleFiles.push(stylePath)
}
}
if (value.indexOf('.') === 0) {
const pathArr = value.split('/')
if (pathArr.indexOf('pages') >= 0) {
astPath.remove()
} else if (REG_SCRIPTS.test(value) || path.extname(value) === '') {
const absolutePath = path.resolve(filePath, '..', value)
const dirname = path.dirname(absolutePath)
const extname = path.extname(absolutePath)
const realFilePath = Util.resolveScriptPath(path.join(dirname, path.basename(absolutePath, extname)))
const removeExtPath = realFilePath.replace(path.extname(realFilePath), '')
node.source = t.stringLiteral(Util.promoteRelativePath(path.relative(filePath, removeExtPath)).replace(/\\/g, '/'))
}
}
return
}
if (value === PACKAGES['@tarojs/taro']) {
const specifier = specifiers.find(item => item.type === 'ImportDefaultSpecifier')
if (specifier) {
hasAddReactImportDefaultName = true
taroImportDefaultName = specifier.local.name
specifier.local.name = reactImportDefaultName
} else if (!hasAddReactImportDefaultName) {
hasAddReactImportDefaultName = true
node.specifiers.unshift(
t.importDefaultSpecifier(t.identifier(reactImportDefaultName))
)
}
// 删除从@tarojs/taro引入的 React
specifiers.forEach((item, index) => {
if (item.type === 'ImportDefaultSpecifier') {
specifiers.splice(index, 1)
}
})
const taroApisSpecifiers: t.ImportSpecifier[] = []
specifiers.forEach((item, index) => {
if ((item as t.ImportSpecifier).imported && taroApis.indexOf((item as t.ImportSpecifier).imported.name) >= 0) {
taroApisSpecifiers.push(
t.importSpecifier(t.identifier((item as t.ImportSpecifier).local.name), t.identifier((item as t.ImportSpecifier).imported.name)))
specifiers.splice(index, 1)
}
})
source.value = PACKAGES['@tarojs/taro-rn']
// insert React
astPath.insertBefore(template(`import React from 'react'`, babylonConfig as any)())
if (taroApisSpecifiers.length) {
astPath.insertBefore(t.importDeclaration(taroApisSpecifiers, t.stringLiteral(PACKAGES['@tarojs/taro-rn'])))
}
if (!specifiers.length) {
astPath.remove()
}
} else if (value === PACKAGES['@tarojs/redux']) {
const specifier = specifiers.find(item => {
return t.isImportSpecifier(item) && item.imported.name === providerComponentName
})
if (specifier) {
//.........这里部分代码省略.........
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:transformJS.ts
注:本文中的babel-types.importDefaultSpecifier函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论