Skip to content

Commit

Permalink
feat(taroize): 支持解析 config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2018
1 parent 484f226 commit efa50cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
32 changes: 32 additions & 0 deletions packages/taroize/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "t.js",
"request": "launch",
"args": [
"--runInBand"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"program": "${workspaceFolder}/t.js"
},
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"args": [
"--runInBand"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
]
}
4 changes: 3 additions & 1 deletion packages/taroize/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from 'babel-types'
import { parseWXML } from './wxml'
import { parseScript } from './script'
import { parseJSON } from './json'

interface Option {
json?: string,
Expand All @@ -10,5 +11,6 @@ interface Option {

export function parse (option: Option): t.File {
const wxml = parseWXML(option.wxml)
return parseScript(option.script, wxml as t.Expression)
const json = parseJSON(option.json)
return parseScript(option.script, wxml as t.Expression, json)
}
8 changes: 6 additions & 2 deletions packages/taroize/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { buildTemplate } from './utils'
import * as t from 'babel-types'

function parseJSON (json: string) {
return buildTemplate(json)
export function parseJSON (json?: string) {
if (!json) {
return
}
return buildTemplate(`(${json})`) as t.ObjectExpression
}
15 changes: 12 additions & 3 deletions packages/taroize/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { buildImportStatement, codeFrameError } from './utils'
import { usedComponents } from './wxml'
import { PageLifecycle } from './lifecycle'

export function parseScript (script: string, returned: t.Expression) {
export function parseScript (script: string, returned: t.Expression, json?: t.ObjectExpression) {
const { ast } = transform(script, {
parserOpts: {
sourceType: 'module',
Expand All @@ -32,7 +32,7 @@ export function parseScript (script: string, returned: t.Expression) {
CallExpression (path) {
const callee = path.get('callee')
if (callee.isIdentifier({ name: 'Page' })) {
classDecl = parsePage(path, returned)!
classDecl = parsePage(path, returned, json)!
path.insertAfter(
t.exportDefaultDeclaration(classDecl)
)
Expand Down Expand Up @@ -66,7 +66,7 @@ const defaultClassName = 'C'

const stateKeys: string[] = []

function parsePage (path: NodePath<t.CallExpression>, returned: t.Expression) {
function parsePage (path: NodePath<t.CallExpression>, returned: t.Expression, json?: t.ObjectExpression) {
const arg = path.get('arguments')[0]
if (!arg || !arg.isObjectExpression()) {
return
Expand Down Expand Up @@ -106,6 +106,15 @@ function parsePage (path: NodePath<t.CallExpression>, returned: t.Expression) {
return t.classProperty(t.identifier(name), value.isFunctionExpression() ? t.arrowFunctionExpression(value.node.params, value.node.body) : value.node)
})

if (json) {
classBody.push(
t.classProperty(
t.identifier('config'),
json
)
)
}

const renderFunc = buildRender(returned)

return t.classDeclaration(
Expand Down

0 comments on commit efa50cc

Please sign in to comment.