Skip to content

Commit

Permalink
feat(taroize): 支持 wxs
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2018
1 parent 1369688 commit 061311c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 83 deletions.
2 changes: 1 addition & 1 deletion packages/taro-transformer-wx/__tests__/loop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2747,7 +2747,7 @@ describe('loop', () => {
])
expect(
instance.state.loopArray0.map(i =>
i.$anonymousCallee__7.map(a => a.$original)
i.$anonymousCallee__1.map(a => a.$original)
)
).toEqual(Object.keys(keys).map(key => Object.keys(keys[key]).map(i => i)))
expect(template).toMatch(
Expand Down
2 changes: 2 additions & 0 deletions packages/taroize/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/lib
/index.js
4 changes: 2 additions & 2 deletions packages/taroize/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Option {
}

export function parse (option: Option): t.File {
const wxml = parseWXML(option.wxml)
const { wxml, wxses } = parseWXML(option.wxml)
const json = parseJSON(option.json)
return parseScript(option.script, wxml as t.Expression, json)
return parseScript(option.script, wxml as t.Expression, json, wxses)
}
16 changes: 13 additions & 3 deletions packages/taroize/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import * as t from 'babel-types'
import traverse, { NodePath } from 'babel-traverse'
import { transform } from 'babel-core'
import { buildImportStatement, codeFrameError } from './utils'
import { usedComponents } from './wxml'
import { usedComponents, WXS } from './wxml'
import { PageLifecycle } from './lifecycle'

const buildDecorator = (type: string) => t.decorator(t.callExpression(
t.identifier('withWeapp'),
[t.stringLiteral(type)]
))

export function parseScript (script?: string, returned?: t.Expression, json?: t.ObjectExpression) {
export function parseScript (
script?: string,
returned?: t.Expression,
json?: t.ObjectExpression,
wxses: WXS[] = []
) {
script = script || 'Page({})'
const { ast } = transform(script, {
parserOpts: {
Expand Down Expand Up @@ -82,7 +87,12 @@ export function parseScript (script?: string, returned?: t.Expression, json?: t.
[],
'withWeapp'
)
ast.program.body.unshift(taroComponentsImport, taroImport, withWeappImport)
ast.program.body.unshift(
taroComponentsImport,
taroImport,
withWeappImport,
...wxses.map(wxs => buildImportStatement(wxs.src, [], wxs.module))
)

return ast
}
Expand Down
Loading

0 comments on commit 061311c

Please sign in to comment.