diff --git a/packages/taro-mini-runner/package.json b/packages/taro-mini-runner/package.json index da1fd62b2cdf..2fe925a116d3 100644 --- a/packages/taro-mini-runner/package.json +++ b/packages/taro-mini-runner/package.json @@ -38,6 +38,7 @@ "babel-generator": "^6.26.1", "babel-loader": "^8.0.6", "babel-plugin-danger-remove-unused-import": "^1.1.2", + "babel-plugin-preval": "1.6.4", "babel-plugin-transform-define": "^1.3.1", "babel-traverse": "^6.26.0", "babel-types": "^6.26.0", diff --git a/packages/taro-mini-runner/src/utils/parseAst.ts b/packages/taro-mini-runner/src/utils/parseAst.ts index ce8a1bd9c8b6..6791d06370b3 100644 --- a/packages/taro-mini-runner/src/utils/parseAst.ts +++ b/packages/taro-mini-runner/src/utils/parseAst.ts @@ -1,6 +1,7 @@ import { Config as IConfig } from '@tarojs/taro' import * as t from 'babel-types' import traverse from 'babel-traverse' +import { transformFromAst } from 'babel-core' import { BUILD_TYPES, taroJsComponents, QUICKAPP_SPECIAL_COMPONENTS } from './constants' import { traverseObjectNode, isNpmPkg } from '../utils' @@ -19,7 +20,13 @@ export default function parseAst ( const taroSelfComponents = new Set() const isQuickApp = buildAdapter === BUILD_TYPES.QUICKAPP - traverse(ast, { + const newAst = transformFromAst(ast, '', { + plugins: [ + [require('babel-plugin-preval')] + ] + }).ast as t.File + + traverse(newAst, { ClassDeclaration (astPath) { const node = astPath.node let hasCreateData = false