From 7fee56b5aaa72c10f548cbb966b6a3ae64cabd29 Mon Sep 17 00:00:00 2001 From: Xiaoli <544028951@qq.com> Date: Mon, 26 Aug 2019 23:31:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E4=BF=AE=E5=A4=8Dh5=E7=9A=84?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=96=87=E4=BB=B6=E4=B8=8D=E6=94=AF=E6=8C=81?= =?UTF-8?q?named=20export=E7=9A=84=E9=97=AE=E9=A2=98,=20close=20#4290?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/h5/index.ts | 59 +++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/taro-cli/src/h5/index.ts b/packages/taro-cli/src/h5/index.ts index 952f10e4ef6f..5e5b569da971 100644 --- a/packages/taro-cli/src/h5/index.ts +++ b/packages/taro-cli/src/h5/index.ts @@ -1,13 +1,13 @@ import { PageConfig } from '@tarojs/taro' import * as wxTransformer from '@tarojs/transformer-wx' import * as babel from 'babel-core' -import traverse, { NodePath } from 'babel-traverse' +import traverse, { NodePath, TraverseOptions } from 'babel-traverse' import * as t from 'babel-types' import generate from 'better-babel-generator' import * as chokidar from 'chokidar' import * as fs from 'fs-extra' import * as klaw from 'klaw' -import { findLastIndex, merge, get } from 'lodash' +import { findLastIndex, get, merge } from 'lodash' import * as path from 'path' import CONFIG from '../config' @@ -767,11 +767,14 @@ class Compiler { let importNervNode: t.ImportDeclaration let importTaroNode: t.ImportDeclaration let renderClassMethodNode: t.ClassMethod + let exportDefaultDeclarationNode: t.ExportDefaultDeclaration + let exportNamedDeclarationPath: NodePath const renderReturnStatementPaths: NodePath[] = [] ast = babel.transformFromAst(ast, '', { plugins: [ + [require('babel-plugin-preval')], [require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }] ] }).ast @@ -839,7 +842,37 @@ class Compiler { ) } - const defaultVisitor = { + const replaceExportNamedToDefault = (astPath: NodePath) => { + if (!astPath) return + + const node = astPath.node + if (t.isFunctionDeclaration(node.declaration)) { + + astPath.replaceWithMultiple([ + node.declaration, + t.exportDefaultDeclaration(node.declaration.id) + ]) + } else if (t.isClassDeclaration(node.declaration)) { + astPath.replaceWithMultiple([ + node.declaration, + t.exportDefaultDeclaration(node.declaration.id) + ]) + } else if (t.isVariableDeclaration(node.declaration)) { + const declarationId = node.declaration.declarations[0].id + if (t.isIdentifier(declarationId)) { + astPath.replaceWithMultiple([ + node.declaration, + t.exportDefaultDeclaration(declarationId) + ]) + } + } else if (node.specifiers && node.specifiers.length) { + astPath.replaceWithMultiple([ + t.exportDefaultDeclaration(node.specifiers[0].local) + ]) + } + } + + const defaultVisitor: TraverseOptions = { ClassExpression: ClassDeclarationOrExpression, ClassDeclaration: ClassDeclarationOrExpression, ImportDeclaration: { @@ -989,7 +1022,7 @@ class Compiler { } } - const pageVisitor = { + const pageVisitor: TraverseOptions = { ClassProperty: { enter (astPath: NodePath) { const node = astPath.node @@ -1089,6 +1122,16 @@ class Compiler { } } }, + ExportDefaultDeclaration: { + exit (astPath: NodePath) { + exportDefaultDeclarationNode = astPath.node + } + }, + ExportNamedDeclaration: { + exit (astPath: NodePath) { + exportNamedDeclarationPath = astPath + } + }, Program: { exit (astPath: NodePath) { if (hasOnPullDownRefresh) { @@ -1136,13 +1179,17 @@ class Compiler { ref={ref => { if (ref) this.pullDownRefreshRef = ref }}>{${varName}}`) as t.ExpressionStatement).expression - }) + }) + } + + if (!exportDefaultDeclarationNode && exportNamedDeclarationPath) { + replaceExportNamedToDefault(exportNamedDeclarationPath) } } } } - const visitor = mergeVisitors({}, defaultVisitor, isPage ? pageVisitor : {}) + const visitor: TraverseOptions = mergeVisitors({}, defaultVisitor, isPage ? pageVisitor : {}) traverse(ast, visitor)